iOS.iPad.02.UIPopoverViewController

1、案例介绍:

如图01,02,点击openByView弹出由故事版直接连接的popover视图;点击openByCoding弹出由代码创建的popover视图。

图01图02

2、案例步骤:

2.1、创建案例工程,新增iPadC02SelectTableViewController类,如图03,04

图03图04

2.2、代码

iPadC02ViewController.h

#import <UIKit/UIKit.h>

@interface iPadC02ViewController : UIViewController

// 点击openByCoding按钮后弹出的popoverView
@property (strong,nonatomic) UIPopoverController *poc;

// 点击openByCoding按钮事件
- (IBAction)show:(id)sender;

@end

iPadC02ViewController.m

#import "iPadC02ViewController.h"
#import "iPadC02SelectTableViewController.h"

@interface iPadC02ViewController ()

@end

@implementation iPadC02ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)show:(id)sender
{
    // 1、根据storyboardId加载表视图
    iPad02SelectTableViewController *selectTableViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"selectTableViewController"];
    
    // 2、根据表视图加载popoverView
    if (self.poc == nil) {
        
        // title
        selectTableViewController.title = @"选择你喜欢的颜色";
        
        // 通过UINavigationController显示title
        UINavigationController *nav = [[UINavigationController alloc]
                                       initWithRootViewController:selectTableViewController];
        
        // 创建UIPopoverController实例,然后赋值
        self.poc = [[UIPopoverController alloc] initWithContentViewController:nav];
    }
    // 3、制定openBycoding按钮来呈现popover视图
    [self.poc presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}

@end

iPadC02SelectTableViewController.h

#import <UIKit/UIKit.h>

@interface iPadC02SelectTableViewController : UITableViewController

@property (strong,nonatomic) NSArray *listData;

@property (nonatomic, strong) NSIndexPath* lastIndexPath;

@end

iPadC02SelectTableViewController.m

#import "iPadC02SelectTableViewController.h"

@interface iPadC02SelectTableViewController ()

@end

@implementation iPadC02SelectTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 单元格数据
    self.listData = [[NSArray alloc] initWithObjects:@"红色",@"蓝色",@"黄色", nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.listData.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuseIdentifier"];
    }
    
    // Configure the cell...
    NSUInteger row = [indexPath row];
    cell.textLabel.text = [self.listData objectAtIndex:row];
    
    return cell;
}
#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    int newRow = [indexPath row];
    
    int oldRow = (self.lastIndexPath != nil) ? [self.lastIndexPath row] : -1;
    
    if (newRow != oldRow) {
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;
        
        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:self.lastIndexPath];
        oldCell.accessoryType = UITableViewCellAccessoryNone;
        
        self.lastIndexPath = indexPath;
    }
    
}

@end

2.3、故事版Main.storyboard(细节忽略)

 

 

 

转载于:https://www.cnblogs.com/cqchen/p/3773773.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值