ios7 uipopovercontroller

####iOS7 Programming Cookbook 第一章学习笔记 UIPopoverController

#####Displaying Popovers with UIPopoverController

#####ViewController.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
@interface  () <UIAlertViewDelegate>//private delegate

@property (nonatomic, strong) UIPopoverController *myPopoverController;
@property (nonatomic, strong) UIBarButtonItem *barButtonAdd;
@end

@implementation

- (NSString *) photoButtonTitle{
return @"Photo";
}

- (NSString *) audioButtonTitle{
return @"Audio";
}


- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];

if ([buttonTitle isEqualToString:[self photoButtonTitle]]){
NSLog(@"Photo");
/* Adding a photo ... */
}
else if ([buttonTitle isEqualToString:[self audioButtonTitle]]){
NSLog(@"Audio");
/* Adding an audio... */
}

}

- (void) performAddWithAlertView:(id)paramSender{
//iPhone弹警告
[[[UIAlertView alloc] initWithTitle:nil
message:@"Add..."
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:
[self photoButtonTitle],
[self audioButtonTitle], nil] show];

}

- (void) performAddWithPopover:(id)paramSender{
//popover展示barButtonAdd
//UIPopoverArrowDirectionAny剪头方向
[self.myPopoverController presentPopoverFromBarButtonItem:self.barButtonAdd permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

}

- (void)viewDidLoad{
[super viewDidLoad];

/* See if this class exists on the iOS running the app */
//声明popoverClass = UIPopoverController
Class popoverClass = NSClassFromString(@"UIPopoverController");
//如果有UIPopoverController 并且是iPad
if (popoverClass != nil && UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
//创建content
PopoverContentViewController *content = [[PopoverContentViewController alloc] initWithNibName:nil bundle:nil];
self.myPopoverController = [[UIPopoverController alloc] initWithContentViewController:content];
//content类的myPopoverController
content.myPopoverController = self.myPopoverController;
self.barButtonAdd =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(performAddWithPopover:)];
} else {
//iPhone
self.barButtonAdd =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(performAddWithAlertView:)];
}
//nav右边按钮设置成barButtonAdd
[self.navigationItem setRightBarButtonItem:self.barButtonAdd animated:NO];

}

@end

#####PopoverContentViewController.h

1
@property (nonatomic, weak) UIPopoverController *myPopoverController;

#####PopoverContentViewController.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
@interface PopoverContentViewController ()
//私有变量
@property (nonatomic, strong) UIButton *buttonPhoto;
@property (nonatomic, strong) UIButton *buttonAudio;
@end

@implementation PopoverContentViewController

- (BOOL) isInPopover{
//声明popoverClass 为UIPopoverController类
Class popoverClass = NSClassFromString(@"UIPopoverController");
if (popoverClass != nil &&
UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad &&
self.myPopoverController != nil){
return YES;
} else {
return NO;
}

}

- (void) gotoAppleWebsite:(id)paramSender{

if ([self isInPopover]){
/* Go to website and then dismiss popover */
[self.myPopoverController dismissPopoverAnimated:YES];
} else {
/* Handle case for iPhone */
}

}

- (void) gotoAppleStoreWebsite:(id)paramSender{

if ([self isInPopover]){
/* Go to website and then dismiss popover */
[self.myPopoverController dismissPopoverAnimated:YES];
} else {
/* Handle case for iPhone */
}

}

- (void)viewDidLoad{
[super viewDidLoad];
//childView 宽,高
self.preferredContentSize = CGSizeMake(200.0f, 125.0f);
//按钮大小
CGRect buttonRect = CGRectMake(20.0f,20.0f,
160.0f,37.0f);
//type:系统标准按钮
self.buttonPhoto = [UIButton buttonWithType:UIButtonTypeSystem];
//设置button title
[self.buttonPhoto setTitle:@"Photo" forState:UIControlStateNormal];
//为buttonPhoto添加事件 dismiss
[self.buttonPhoto addTarget:self
action:@selector(gotoAppleWebsite:)
forControlEvents:UIControlEventTouchUpInside];
//坐标
self.buttonPhoto.frame = buttonRect;
//添加到view
[self.view addSubview:self.buttonPhoto];
//y轴 +50坐标
buttonRect.origin.y += 50.0f;

//type:系统标准按钮
self.buttonAudio = [UIButton buttonWithType:UIButtonTypeSystem];
//设置button title
[self.buttonAudio setTitle:@"Audio"
forState:UIControlStateNormal];
//为buttonAudio添加事件 dismiss
[self.buttonAudio addTarget:self
action:@selector(gotoAppleStoreWebsite:)
forControlEvents:UIControlEventTouchUpInside];
//坐标
self.buttonAudio.frame = buttonRect;
//添加到view
[self.view addSubview:self.buttonAudio];

}

@end

Reference