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)控制器介绍已经完毕,程序运行效果图如下:

   





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值