2012-11-26 41 views
1

我創造與UIPopover爲iPad應用程序。我有一個主要觀點,我正在調用兩個顯示不同信息的popovers。我是按照How to Dismiss a Storyboard Popover線程和線程的另一個準則,一切,除了一件事工作正常。在我的popover中,我有一個觸發父視圖上的操作的按鈕。即使按鈕被點擊一次,popover也會不止一次觸發,並且popover只打開一次。我的第一個假設是,popover緩存了幾次調用的一些數據,但問題似乎只是隨機出現。UIPopover按鈕重複動作

我的配置是:MAC OSX雪豹和Xcode 4.2的iOS 5.0。 經測試,在模擬器,iPad的5.1和iPad 6.0的所有相同的結果。

我有主視圖視圖1和酥料餅視圖視圖2

我認爲2我有一種方法ProceedButtonClicked它發送通知,以查看1

- (IBAction) ProceedButtonClicked{ 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"proceedButtonClicked" object:self]; 
} 

的方法綁定到彈出視圖中的按鈕。 在廠景(父視圖):

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ButtonClicked:) name:@"proceedButtonClicked" object:nil]; 
} 

- (void) ButtonClicked:(NSNotification *) notification { 
NSLog(@"I'm here ..."); 
//dismiss popover 
if (paramPopover) 
    [paramPopover dismissPopoverAnimated:YES]; 
} 

我非常新的iPad的發展,所以也許我缺少在我的代碼的東西很明顯,但搜索直到現在導致不了了之。 任何幫助,將不勝感激。 謝謝。

回答

0

使用的通知,你的風險一類回答同樣通知的多個實例,所以如果你有2個控制器活着出於某種原因(壞的內存管理?),按下按鈕,然後在2個控制器會接聽電話,你將會有重複的操作。

按鈕可分配有一個特定的回調,這是非常簡單的代碼來設置它:

如果您的按鈕就是一個UIButton,你可以這樣設置你的目標行動:

[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; 

如果你的按鈕是一個的UIBarButtonItem,你設定的目標,當您創建

[[UIBarButtonItem alloc] initWithTitle:@"Title" style:UIBarButtonItemStyleBordered target:self action:@selector(buttonAction:)]; 

編輯:

NSLog(@"I'm here ..."); 這是令人毛骨悚然的...

+0

謝謝。我也假設內存管理不當,但我正在使用ARC,而且我不知道如何恰當地保持活動只有一個控制器。調用'popoverControllerDidDismissPopover'並設置'popOverController'到'nil'沒有幫助。我也嘗試將UIButton操作更改爲上述代碼,結果仍然相同。 –