天天看点

ios-使用iPad专用API(UIPopoverController)控制器

iPhone和iPad都使用同一个操作系统——ios,因此,它们的API基本上是一样的,但有一些是iPad专用的,比如UIPopoverController控制器,UIPopoverController控制器用于呈现“漂浮”类型的视图。

代码实现如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    _popViewCtl = [[[PopViewController alloc] init] autorelease];
    
    UINavigationController *navCtl = [[[UINavigationController alloc] initWithRootViewController:_popViewCtl] autorelease];
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.rootViewController = navCtl;
    [self.window makeKeyAndVisible];
    return YES;
}
           
//
// PopViewController.h
//
#import <UIKit/UIKit.h>

@interface PopViewController : UIViewController

@end
           
//
// PopViewController.m
//
#import "PopViewController.h"
#import "KFTableViewController.h"

@interface PopViewController ()

@end

@implementation PopViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initNavItem];
}

- (void)initNavItem
{
    self.title = @"Pop视图";
    
    UIBarButtonItem *barBtnLeft = [[UIBarButtonItem alloc] initWithTitle:@"Print" style:UIBarButtonItemStylePlain target:self action:@selector(printOnClick:)];
    UIBarButtonItem *barBtnRight = [[UIBarButtonItem alloc] initWithTitle:@"Color" style:UIBarButtonItemStylePlain target:self action:@selector(colorOnClick:)];
    
    self.navigationItem.leftBarButtonItem = barBtnLeft;
    self.navigationItem.rightBarButtonItem = barBtnRight;
}

- (void)printOnClick:(id)sender
{
    // 初始化表视图控制器,如果要在UITableViewController中做一些事情,需要自定义UITableViewController一个控制器
    UITableViewController *tableViewCtl = [[UITableViewController alloc] init];
    tableViewCtl.title = @"表视图";
    
    // 初始化导航控制器
    UINavigationController *navCtl = [[UINavigationController alloc] initWithRootViewController:tableViewCtl];
    
    // 初始化“漂浮”控制器
    UIPopoverController *popoverCtl = [[UIPopoverController alloc] initWithContentViewController:navCtl];
    [popoverCtl presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
    
    [tableViewCtl release];
    [navCtl release];
    
    /*
     presentPopoverFromBarButtonItem: permittedArrowDirections: animated: 指定一个按钮作为锚点来呈现“漂浮”视图
     dismissPopoverAnimated: 关闭“漂浮”视图
     popoverVisible 判断“漂浮”视图是否可见
     popoverArrowDirection 判断“漂浮”视图箭头的方向
     */
}

- (void)colorOnClick:(id)sender
{
    // 初始化表视图控制器,如果要在UITableViewController中做一些事情,需要自定义UITableViewController一个控制器
    KFTableViewController *tableViewCtl = [[KFTableViewController alloc] init];
    tableViewCtl.title = @"选择你喜欢的颜色";
    
    // 初始化导航控制器
    UINavigationController *navCtl = [[UINavigationController alloc] initWithRootViewController:tableViewCtl];
    
    // 初始化“漂浮”控制器
    UIPopoverController *popoverCtl = [[UIPopoverController alloc] initWithContentViewController:navCtl];
    [popoverCtl presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
    
    [tableViewCtl release];
    [navCtl release];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
           
//
//  KFTableViewController.h
//
#import <UIKit/UIKit.h>

@interface KFTableViewController : UITableViewController

@end
           
//
//  KFTableViewController.m
//
#import "KFTableViewController.h"

@interface KFTableViewController ()

@end

@implementation KFTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self)
    {

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (NSInteger)numberOfSectionsInTableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *indentifer = @"myCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifer];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifer] autorelease];
    }
    
    int iRow = [indexPath row];
    NSString *str = @"";
    if (iRow == 0)
    {
        str = @"红色";
    }
    else if (iRow == 1)
    {
        str = @"蓝色";
    }
    else if (iRow == 2)
    {
        str = @"黄色";
    }
    cell.textLabel.text = str;
    
    return cell;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
           

至此,iPad专用API(UIPopoverController)控制器介绍已经完毕,程序运行效果图如下:

ios-使用iPad专用API(UIPopoverController)控制器
ios-使用iPad专用API(UIPopoverController)控制器