我无法弄清楚为什么它不能正常工作当我尝试通过点击一个UIButton来解雇一个popover时,它本身就是一个被解雇的popover,我的项目崩溃......
- (IBAction) cancelButton: (id) sender{
//[self dismissPopoverAnimated:YES];
}
以上是我的UIButton的代码
答案 0 :(得分:19)
不要忽视自己内部的弹出窗口。制定一个协议,向其委托发送消息然后解除它。例如,您的弹出视图控制器可能看起来像这样..
// MyPopoverViewController.h
@protocol MyPopoverDelegate <NSObject>
-(void)didClickCancelButton;
@end
@interface MyPopoverViewController : UIViewController {
}
@property (nonatomic, assign) id<MyPopoverDelegate> delegate;
-(IBAction)cancelButton;
@end
// MyPopoverViewController.m
#import "MyPopoverViewController.h"
@implementation MyPopoverViewController
@synthesize delegate;
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
-(IBAction)cancelButton {
[self.delegate didClickCancelButton];
}
#pragma mark -
#pragma mark Rotation support
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}
#pragma mark -
#pragma mark Memory Management
- (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.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
然后你可以使用..
// ClassImplementingPopoverController.h
#import <UIKit/UIKit.h>
#import "MyPopoverViewController.h"
@interface ClassImplementingPopoverController : UIViewController <UIPopoverControllerDelegate, MyPopoverDelegate> {
UIPopoverController *myPopoverController;
}
@property (nonatomic, retain) UIPopoverController *myPopoverController;
@end
// ClassImplementingPopoverController.m
#import "ClassImplementingPopoverController.h"
#import "MyPopoverViewController.h"
@implementation ClassImplementingPopoverController
@synthesize myPopoverController;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return YES;
}
#pragma mark -
#pragma mark MyPopover delegate
-(void)didClickCancelButton {
if ([myPopoverController isPopoverVisible]) {
[myPopoverController dismissPopoverAnimated:YES];
[myPopoverController release];
}
}
#pragma mark -
#pragma mark UIPopoverController delegate
-(void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
if (popoverController == myPopoverController) {
[myPopoverController release];
}
}
/* Use something like this to create your popover, just make sure you set the delegate to self so you can receive the messages
NSLog(@"Displaying Popover!");
MyPopoverViewController *detailViewController = [[MyPopoverViewController alloc] initWithNibName:@"MyPopoverViewController" bundle:nil];
[detailViewController setDelegate:self];
// Pass the selected object to the new view controller.
myPopoverController = [[UIPopoverController alloc] initWithContentViewController:detailViewController];
[detailViewController release];
myPopoverController.popoverContentSize = CGSizeMake(500.0, 150.0);
[myPopoverController setDelegate:self];
*/
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
self.myPopoverController = nil;
}
- (void)dealloc {
[myPopoverController release];
[super dealloc];
}
@end
答案 1 :(得分:3)
您可以使用KVC访问“popoverController”,如
[self valueForKey:@"popoverController"]
但如果AppStore检测到您的代码使用的是“私有API”,则可能会被AppStore拒绝。
答案 2 :(得分:0)
dismissPopoverAnimated是PopoverController本身的属性,而不是您的自定义VC。你正在展示一个关于'self'的IBAction,这表明你在这里属于自己的班级。 (如果不正确,请发布更多周边代码。)
VC无法直接进入其封闭的popover;你只需要在创建它的VC中保留对它的引用,这样你就可以从那里解雇它。
你可以从内部解除一个不会崩溃的Popover。