是否可以在iPad中彈出UIViewController
(xib文件),如UIPopOverControl
?使用IBAction UIViewController彈出UIViewController
我有一個單獨的NIB文件,它鏈接到一個UIViewController
。我想彈出該NIB文件以及按下自定義尺寸(200,200)的按鈕。
這可能嗎?
我試圖讓在iPhone上是這樣的 - http://www.freeimagehosting.net/c219p
是否可以在iPad中彈出UIViewController
(xib文件),如UIPopOverControl
?使用IBAction UIViewController彈出UIViewController
我有一個單獨的NIB文件,它鏈接到一個UIViewController
。我想彈出該NIB文件以及按下自定義尺寸(200,200)的按鈕。
這可能嗎?
我試圖讓在iPhone上是這樣的 - http://www.freeimagehosting.net/c219p
是的。在需要的時候懶惰地加載你的pOpOver控制器。將其視圖添加爲子視圖(可以爲添加添加動畫)。使其幀大小您所需要的並添加圖像您已顯示爲pOpOver控制器的背景子視圖以及您想要彈出的其他控件。
好運
UPDATE:
好吧,二會告訴你如何II在我的應用程序Lucid Reality Check(部署目標的iOS4.3)做到這一點。
可以使用UIPopoverController來顯示另一個控制器視圖。 ii首先要確保ii總是知道設備的當前方向,因此ii可以重新定位旋轉中的彈出窗口(也許這在iOS6上可以自行工作?)。所以在我的基本控制器(從那裏II想顯示彈出式)II有一個這樣的實例變量:
UIInterfaceOrientation toOrientation;
也:
UIPopoverController *popover;
UIButton *popover_from_button;
BOOL representPopover;
酥料餅將爲所有彈出窗口被重用,並且popover_from_button將保持從其發起彈出的按鈕。
然後下一個代碼進入基本控制器:
- (void)popoverWillRotate {
if ([popover isPopoverVisible]) {
[self dismissPopover];
representPopover = YES;
}
}
- (void)popoverDidRotate {
if (popover && representPopover) {
representPopover = NO;
[self representPopover];
}
}
這兩種方法都被稱爲每次設備被旋轉時,這樣的:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
//DLOG(@"willRotateTo %i", toInterfaceOrientation);
toOrientation = toInterfaceOrientation;
if ([Kriya isPad ]) {
[self popoverWillRotate];
}
}
如人們可以看到,首先定位被捕獲,然後popoverWillRotate被調用。這會在定向動畫期間隱藏彈出窗口。和旋轉後,酥料餅必須重新顯示是這樣的:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
//DLOG(@"didRotateFrom %i", fromInterfaceOrientation);
//[self layout:toOrientation]; //do some layout if You need
if ([Kriya isPad]) {
[self popoverDidRotate];
}
}
- (void)layout:(UIInterfaceOrientation)toInterfaceOrientation {
//one can do view layout here, and call other controllers to do their layout too
}
現在的方向變化制定出來,用於呈現酥料餅的代碼來到這裏:
#pragma mark Popovers
- (void)presentPopoverWith:(id)controller fromButton:(UIButton*)button {
if (popover)
[popover release];
if (popover_from_button)
[popover_from_button release];
popover_from_button = [button retain];
popover = [[UIPopoverController alloc] initWithContentViewController:controller];
[popover setDelegate:self];
[self representPopover];
}
- (void)representPopover{
if (popover) {
UIPopoverArrowDirection arrowDirection = UIPopoverArrowDirectionAny;
UIViewController *vc = (UIViewController*)[popover contentViewController];
CGSize contentSize = [vc contentSizeForViewInPopover];
if (contentSize.width > 0 && contentSize.height > 0) {
[popover setPopoverContentSize:contentSize animated:NO];
}
//DLOG(@"representPopover rect:%@", [Kriya printRect:popover_from_button.frame]);
[popover presentPopoverFromRect:CGRectOffset(popover_from_button.frame, 0, popover_from_button.frame.size.height + 7.0) inView:self.view permittedArrowDirections:arrowDirection animated:YES];
}
}
- (void)dismissPopover {
if (popover) {
[popover dismissPopoverAnimated:YES];
}
}
最後,如果一個人想通知當酥料餅被駁回,基本控制器必須實現委託方法:
#pragma mark UIPopoverControllerDelegate
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
//do something important here like drink some water
}
,不要忘記製造基地控制器UIPopoverControll erDelegate在其標題中。
的使用情況做彈出窗口,然後看起來像這樣的是這樣的:
- (void)takeImage {
UIImagePickerController *picker = [[[UIImagePickerController alloc] init] autorelease];
[picker setDelegate:self];
[picker setAllowsEditing:NO];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[picker setSourceType:UIImagePickerControllerSourceTypeCamera];
if ([Kriya isPad]) {
[self presentPopoverWith:picker fromButton:backgroundImageButton];
} else {
//modals on iPhone/iPod
//DLOG(@"takeImage addSubview picker");
[self presentModalViewController:picker animated:YES];
}
} else {
//DLOG(@"no camera");
}
}
這將使用圖像選擇器作爲彈出窗口中的內容,但可以使用任何控制器與一個有效的視圖。所以只是這樣做:
[self presentPopoverWith:popupsContentController fromButton:tappedButton];
一個不應該有任何缺失的信息,:),方法[Kriya isPad]
僅僅是這樣的:
+ (BOOL)isPad {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200
// iPad capable OS
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
//this is an iPad
return YES;
}else {
//this is an iPod/iPhone
return NO;
}
#else
//can not pissible be iPad
return NO;
#endif
}
享受!
您也可以使用這些定製clases的一個顯示彈出式:
https://github.com/sonsongithub/PopupView
https://github.com/werner77/WEPopover
https://github.com/50pixels/FPPopover
例與FPPopover:
//the view controller you want to present as popover
YourViewController *controller = [[YourViewController alloc] init];
//our popover
FPPopoverController *popover = [[FPPopoverController alloc] initWithViewController:controller];
//the popover will be presented from the okButton view
[popover presentPopoverFromView:okButton];
//release if you arent using ARC
[controller release];
你能幫我編碼?謝謝 – BigAppleBump
如果你還沒有自己做過,ii用代碼更新了我的答案;) – kitschmaster
爲什麼你在我添加了很棒的示例代碼後花了一段時間寫下我的答案? – kitschmaster