UIPopoverController 简单用法(全代码)

 AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    UINavigationController *nav =[[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]];

    self.window.rootViewController =nav;

    [self.window makeKeyAndVisible];

    return YES;

}
View Code

 

ViewController.m

#import "ViewController.h"

#import "View2Controller.h"

@interface ViewController ()<UIPopoverControllerDelegate>

@property (nonatomic,strong) UIPopoverController *popoverButtonItem;

@property (nonatomic,strong) UIPopoverController *popoverButton;

@end

@implementation ViewController

 

//popoverButtonItem懒加载

-(UIPopoverController *) popoverButtonItem{

    if (!_popoverButtonItem) {

        UINavigationController *nav= [[UINavigationController alloc]initWithRootViewController: [[View2Controller alloc]init]];

     _popoverButtonItem= [[UIPopoverController alloc] initWithContentViewController:nav];

     _popoverButtonItem.delegate = self;

    }

    return _popoverButtonItem;

}

 //popoverButton懒加载

-(UIPopoverController *) popoverButton{

    if (!_popoverButton) {

        _popoverButton= [[UIPopoverController alloc] initWithContentViewController:[[View2Controller alloc]init]];

        _popoverButton.delegate = self;

    }

    return _popoverButton;

}

 

//1. 从BarButtonItem点击弹出

-(void) touchButtonItem:(UIBarButtonItem *)btn {

    /**

     *  从BarButtonItem上点击

   *  @param item:围绕着哪个UIBarButtonItem显示

     *  @param permittedArrowDirections:箭头指的方向

     *  @param animated:动画

     */

    [self.popoverButtonItem presentPopoverFromBarButtonItem:btn permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

}

//2从界面上的某个按钮或者某片区域弹出popOver,排除navigationBar或者TabBar上的点击

-(void) touchButton:(UIButton *)btn {

    /**

     * 从界面上的某个按钮或者某片区域弹出popOver

     *  @param presentPopoverFromRect: 指定箭头所指区域的矩形框范围(位置和尺寸)

     *  @param inView: rect参数是以view的左上角为坐标原点(0,0)

     *  @param permittedArrowDirections:箭头指的方向

     *  @param animated:动画

     */

    [self.popoverButton presentPopoverFromRect:btn.bounds

                                  inView:btn

                permittedArrowDirections:UIPopoverArrowDirectionLeft

                                animated:YES];

}

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor grayColor];

    self.navigationItem.title = @"导航栏";

    self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc]initWithTitle:@"点我" style:UIBarButtonItemStyleDone target:self action:@selector(touchButtonItem:)];

    //********************************************************************************

    UIButton *btn =[[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 50)];

    [btn setTitle:@"点我啊" forState:UIControlStateNormal];

    [btn setTintColor:[UIColor blueColor]];

    [btn setBackgroundColor:[UIColor redColor]];

    [btn addTarget:self action:@selector(touchButton:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

}

// UIPopoverControllerDelegate --在ARC环境下正确的做法是保持popover的生命周期。让popover是strong类型的成员变量或属性。 当收到UIPopoverController dismiss的消息的时候将popover=nil;这样就可以正常便用了。

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController

{

    self.popoverButton = nil;

    self.popoverButtonItem = nil;

}
View Code

 

View2Controller.m

- (void)viewDidLoad

{

    [super viewDidLoad];

    // 设置控制器在popover中显示的尺寸 跟 view 一样

    self.preferredContentSize = self.view.frame.size;

}
View Code

 

posted @ 2015-08-19 13:39  zakers  阅读(267)  评论(0编辑  收藏  举报