UIPopoverController的使用
有些時候需要用到彈出窗口,這個時候UIPopoverController就有用武之地了,下面介紹一個使用UIPopoverController的示例,以供參考。實現效果如下:
為了實現這一效果并使用UIPopoverController,這里搞了兩個controller,具體定義如下。
//? Created by Chelsea Wang 4209898762 on 11-10-4.
//? Copyright 2011年?__MyCompanyName__. All rights reserved.
#import?<UIKit/UIKit.h>
@interface?UIActionSheetDemoViewController :?UIViewController<UIPopoverControllerDelegate>{
? ??UIPopoverController* _popover;
}
//? UIActionSheetDemoViewController.m
//? UIActionSheetDemo
//
//? Created by Chelsea Wang 4209898762 on 11-10-4.
//? Copyright 2011年?__MyCompanyName__. All rights reserved.
//
#import?"UIActionSheetDemoViewController.h"
#import?"popController.h"
@implementation?UIActionSheetDemoViewController
@synthesize?popover =?_popover;
- (void)didReceiveMemoryWarning
{
? ??// Releases the view if it doesn't have a superview.
? ? [super?didReceiveMemoryWarning];
?? ?
? ??// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
? ? [super?viewDidLoad];
?? ?
? ??UISegmentedControl* settingView = [[UISegmentedControl?alloc]?initWithItems:[NSArrayarrayWithObjects:@"FootballCF",@"BasketballCF",nil]];
? ? [settingView?setSegmentedControlStyle:UISegmentedControlStyleBar];
? ? [settingView?setFrame:CGRectMake(500,?900,?160,?75)];
? ? [settingView?setNeedsLayout];
? ? [settingView?addTarget:self?action:@selector(segmentAction:)?forControlEvents:UIControlEventValueChanged];
? ? [self.view?addSubview:settingView];
? ? [settingView?release];
}
- (void)viewDidUnload
{
? ? [super?viewDidUnload];
? ??// Release any retained subviews of the main view.
? ??// e.g. self.myOutlet = nil;
?? ?
? ??if?(self.popover?!=?nil) {
? ? ? ? [self.popover?release];
? ? ? ??self.popover?=?nil;
? ? }
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
? ??// Return YES for supported orientations
? ??return?YES;
}
-(void)segmentAction:(UISegmentedControl*)seg{
? ??//清除已經顯示的彈出窗口
? ??if?(self.popover.popoverVisible) {
? ? ? ? [self.popover?dismissPopoverAnimated:YES];
? ? }
? ??//初始化待顯示控制器
? ??popController* controller = [[popController?alloc]?init];
? ??//設置待顯示控制器的范圍
? ? [controller.view?setFrame:CGRectMake(550,?490,?350,?450)];
? ??//設置待顯示控制器視圖的尺寸
? ? controller.contentSizeForViewInPopover?=?CGSizeMake(350,?450);
? ??//初始化彈出窗口
? ??UIPopoverController* pop = [[UIPopoverController?alloc]?initWithContentViewController:controller];
? ? controller.popover?= pop;
? ??self.popover?= pop;
? ??self.popover.delegate?=?self;
? ??//設置彈出窗口尺寸
? ??self.popover.popoverContentSize?=?CGSizeMake(350,?450);
?? ?
? ??//顯示,其中坐標為箭頭的坐標以及尺寸
? ? [self.popover?presentPopoverFromRect:CGRectMake(550,?890,?10,?10)?inView:self.viewpermittedArrowDirections:UIPopoverArrowDirectionDown?animated:NO];
?? ?
? ? [controller?release];
? ? [pop?release];
?? ?
? ??//[self.popover dismissPopoverAnimated:YES];
}
-(void)popoverControllerDidDismissPopover:(UIPopoverController?*)popoverController{
? ??NSLog(@"popover dismiss");
}
@end
//
//? popController.h
//? UIActionSheetDemo
//
//? Created by Chelsea Wang 4209898762 on 11-10-4.
//? Copyright 2011年?__MyCompanyName__. All rights reserved.
//
#import?<UIKit/UIKit.h>
@interface?popController :?UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>{
? ??UIPopoverController* _popover;
? ??BOOL?isChelsea;
}
@property(nonatomic,retain)UIPopoverController* popover;
@end
//? popController.m
//? UIActionSheetDemo
//
//? Created by Chelsea Wang 4209898762 on 11-10-4.
//? Copyright 2011年?__MyCompanyName__. All rights reserved.
//
#import?"popController.h"
@implementation?popController
@synthesize?popover =?_popover;
//定義選項顯示內容
const?char* chelsea[12] = {"博阿斯","切赫","阿什利科爾","特里","路易斯","伊萬諾維奇","蘭帕德","拉米雷斯","梅來雷斯","阿內爾卡","德羅巴","托雷斯"};
- (id)initWithNibName:(NSString?*)nibNameOrNil bundle:(NSBundle?*)nibBundleOrNil
{
? ??self?= [super?initWithNibName:nibNameOrNil?bundle:nibBundleOrNil];
? ??if?(self) {
? ? ? ??// Custom initialization
?? ? ? ?
?? ? ? ?
? ? }
? ??return?self;
}
- (void)didReceiveMemoryWarning
{
? ??// Releases the view if it doesn't have a superview.
? ? [super?didReceiveMemoryWarning];
?? ?
? ??// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
? ? [super?viewDidLoad];
? ??// Do any additional setup after loading the view from its nib.
?? ?
? ??isChelsea?=?NO;
?? ?
? ??//添加完成按鈕
? ??UISegmentedControl* doneButton = [[UISegmentedControl?alloc]?initWithItems:[NSArrayarrayWithObjects:@"Done",?nil]];
? ? [doneButton?setSegmentedControlStyle:UISegmentedControlStyleBar];
? ? [doneButton?setFrame:CGRectMake(self.view.bounds.origin.x+self.view.bounds.size.width-100,self.view.bounds.origin.y+5,?75,?50)];
? ? [doneButton?addTarget:self?action:@selector(segmentAction:)?forControlEvents:UIControlEventValueChanged];
? ? [self.view?addSubview:doneButton];
? ? [doneButton?release];
? ??//添加選擇項
? ??UISegmentedControl* clubSeg = [[UISegmentedControl?alloc]?initWithItems:[NSArrayarrayWithObjects:@"Chelsea",@"AC Milan",@"Livupoor",@"Asenal",nil]];
? ? [clubSeg?setSegmentedControlStyle:UISegmentedControlStyleBar];
? ? [clubSeg?setFrame:CGRectMake(self.view.bounds.origin.x,self.view.bounds.origin.y+75,self.view.bounds.size.width,50)];
? ? [clubSeg?addTarget:self?action:@selector(chooseClub:)?forControlEvents:UIControlEventValueChanged];
? ? [self.view?addSubview:clubSeg];
? ? [clubSeg?release];
?? ?
? ??//添加選擇列表
? ??UIPickerView* picker = [[UIPickerView?alloc]?initWithFrame:CGRectMake(self.view.bounds.origin.x,self.view.bounds.origin.y+150,?self.view.bounds.size.width,self.view.bounds.size.height)];
? ? [picker?setBounds:CGRectMake(self.view.bounds.origin.x,?self.view.bounds.origin.y+75,self.view.bounds.size.width,self.view.bounds.size.height)];
? ? picker.delegate?=?self;
? ? picker.dataSource?=?self;
? ? picker.showsSelectionIndicator?=?YES;
? ? [self.view?addSubview:picker];
? ? [picker?setTag:101];
? ? [picker?release];
?? ?
?? ?
}
- (void)viewDidUnload
{
? ? [super?viewDidUnload];
? ??// Release any retained subviews of the main view.
? ??// e.g. self.myOutlet = nil;
? ??if?(self.popover?!=?nil) {
? ? ? ? [self.popover?release];
? ? ? ??self.popover?=?nil;
? ? }
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
? ??// Return YES for supported orientations
? ??return?YES;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView?*)pickerView{
? ??return?1;
}
-(NSInteger)pickerView:(UIPickerView?*)pickerView numberOfRowsInComponent:(NSInteger)component{
? ??return?12;
}
-(NSString*)pickerView:(UIPickerView?*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
? ??if?(isChelsea) {
? ? ? ??return?[NSString?stringWithCString:chelsea[row]?encoding:NSUTF8StringEncoding];
? ? }else{
? ? ? ??return?[NSString?stringWithFormat:@"chelsea_%d",arc4random()%12];
? ? ? ? ;
? ? }
?? ?
}
-(void)segmentAction:(UISegmentedControl*)seg{
? ? [self.view?removeFromSuperview];
? ? [self.popover?dismissPopoverAnimated:YES];
}
-(void)chooseClub:(UISegmentedControl*)seg{
? ??NSInteger?index = seg.selectedSegmentIndex;
? ??UIPickerView* picker = (UIPickerView*)[self.view?viewWithTag:101];
? ??switch?(index) {
? ? ? ??case?0:{
? ? ? ? ? ??NSLog(@"you choose Chelsea");
? ? ? ? ? ??isChelsea?=?YES;
? ? ? ? ? ? [picker?reloadAllComponents];
? ? ? ? ? ??break;
? ? ? ? }
? ? ? ??case?1:
? ? ? ? ? ??NSLog(@"you choose AC Milan");
? ? ? ? ? ??isChelsea?=?NO;
? ? ? ? ? ? [picker?reloadAllComponents];
? ? ? ??case?2:
? ? ? ? ? ??NSLog(@"you choose Livopoor");
? ? ? ? ? ??isChelsea?=?NO;
? ? ? ? ? ? [picker?reloadAllComponents];
? ? ? ??case?3:
? ? ? ? ? ??NSLog(@"you choose Asenal");
? ? ? ? ? ??isChelsea?=?NO;
? ? ? ? ? ? [picker?reloadAllComponents];
? ? ? ??default:
? ? ? ? ? ??break;
? ? }
}
@end
@property(nonatomic,retain)UIPopoverController* popover;
@end
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的UIPopoverController的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 把七个苹果,五个橘子,四个桃装成一个果篮
- 下一篇: 炒虾怎么做 简单易学的炒虾技巧?