首页 > 代码库 > QQ空间客户端(1)-UIPopoverController基本使用

QQ空间客户端(1)-UIPopoverController基本使用

 

UIPopoverController 是iPad的专属API

ViewController.m

#import "ViewController.h"
#import "DJMenuViewController.h"

@interface ViewController ()

- (IBAction)leftItemDidClick:(id)sender;

@end

@implementation ViewController

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

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

- (IBAction)leftItemDidClick:(id)sender {

    DJMenuViewController *menuVC = [[DJMenuViewController alloc] init];
    UINavigationController *menuNavVC = [[UINavigationController alloc] initWithRootViewController:menuVC];
    
    // 创建一个popoverController,注意!只能使用initWithContentViewController创建!
    // 在iOS7以下的系统中,需要将popoverVC当做一个成员变量,使用一个强指针进行引用,否则在这段代码执行完毕后,popoverVC将被销毁
    UIPopoverController *popoverVC = [[UIPopoverController alloc] initWithContentViewController:menuNavVC];
    // 设置popoverController的内部尺寸
    popoverVC.popoverContentSize = CGSizeMake(320, 44*6);
    // 显示popoverController,有点类似于android里面的popview的showAsDropDown方法
    [popoverVC presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    
}
@end

DJMenuViewController.m

#import "DJMenuViewController.h"
#import "DJTestViewController.h"


@interface DJMenuViewController()

@property (nonatomic,strong) NSArray *titles;

@end


@implementation DJMenuViewController


- (void)viewDidLoad {

    [super viewDidLoad];
    self.titles = @[@"发起群聊",@"添加朋友",@"扫一扫",@"收付款"];
    
}


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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *ID = @"title";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    cell.textLabel.text = self.titles[indexPath.row];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    DJTestViewController *testVC = [[DJTestViewController alloc] init];
    [self.navigationController pushViewController:testVC animated:YES];

}

@end

DJTestViewController.m

#import "DJTestViewController.h"

@implementation DJTestViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor redColor];
}

@end

Main.stroyboard

技术分享

 

最终运行效果:

技术分享

技术分享

 

QQ空间客户端(1)-UIPopoverController基本使用