2012年12月22日 星期六

Popover在iPad上的使用(三)

一個動態產生的Popover

實作如下

1. 開啓一個專案

2. 在storyboard加上一個button

3. 在mainViewController.h加上一個popover的變數

#import <UIKit/UIKit.h>

@interface mainViewController : UIViewController
{
    UIPopoverController  *popover;
}

@end

4. 在mainViewController.m加上程式碼
#import "mainViewController.h"

@interface mainViewController ()

@end

@implementation mainViewController

- (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)startPopover:(id)sender {
   
    UIView *anchor = sender;
   
    UIViewController *showView1 = [[UIViewController alloc] init];
   
    showView1.view.backgroundColor = [UIColor redColor];
   
   
    UILabel *testLabel =[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 150)];
    testLabel.backgroundColor = [UIColor clearColor];
    testLabel.textColor = [UIColor blackColor];
    testLabel.text = @"Hello";
   
    [showView1.view addSubview:testLabel ];
   
   
    popover = [[UIPopoverController alloc]
               initWithContentViewController:showView1];
   
    [popover setPopoverContentSize:CGSizeMake(300, 300)];
   
    [popover presentPopoverFromRect:anchor.frame inView:anchor.superview permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
   
}

@end

5. 執行結果,因為popover不能設定x方向的位置,所以x位置為0。

沒有留言: