2010-08-25 35 views
4

我不明白爲什麼它不能正常工作,因爲它應該當我嘗試通過單擊一個UIButton來解除彈出窗口,而這個UIButton本身就是一個彈出窗口,我的項目會崩潰。 。使用UIbutton關閉popover

- (IBAction) cancelButton: (id) sender{ 
//[self dismissPopoverAnimated:YES]; 
} 

以上就是我爲我的UIButton

+0

什麼是崩潰消息(查看控制檯)? – 2010-08-25 13:08:34

+0

在拋出'NSException'實例後終止調用 程序接收信號:「SIGABRT」。 (gdb) – awlcs 2010-08-25 15:54:44

+0

您的回調是否指定爲@selector(cancelButton)且冒號爲空? – ZaBlanc 2010-08-26 09:44:17

回答

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 
+0

hai ur ans是正確的它工作非常好謝謝@ Renegade428 – Rocky 2012-02-02 06:47:42

+0

你的委託方法不工作..任何建議我正在使用xcode 4.3.3 – 2012-07-28 05:57:47

0

dismissPopoverAnimated是在PopoverController本身,而不是您的自定義VC的屬性。你正在展示一個關於'自我'的IBAction,這表明你在這裏屬於你自己的班級。 (如果不正確,請張貼更多周邊代碼。)

VC無法直接到達其封閉的彈出窗口;你只需要在創建它的VC中保留一個引用,這樣你就可以從中刪除它。

您可以從內部解散Popover,但不會崩潰。

3

您可以使用KVC訪問「popoverController」像

[self valueForKey:@"popoverController"] 

不過,它可能通過AppStore的拒絕,如果他們發現你的代碼是使用他們的「私有API」。