zoukankan      html  css  js  c++  java
  • 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(细节忽略)

     

  • 相关阅读:
    线性代数思维导图——3.向量
    微分中值定理的基础题型总结
    构造函数
    Python课程笔记(七)
    0241. Different Ways to Add Parentheses (M)
    0014. Longest Common Prefix (E)
    0013. Roman to Integer (E)
    0011. Container With Most Water (M)
    0010. Regular Expression Matching (H)
    0012. Integer to Roman (M)
  • 原文地址:https://www.cnblogs.com/cqchen/p/3773773.html
Copyright © 2011-2022 走看看