2014-02-12 46 views
0

我希望有人能幫我弄清楚這一點......我是一個初學Xcode/Objective-C程序員。我正在研究上個學期延續的應用。如何讓UIBarButton執行自定義函數?

1:所以我創建了一個按鈕,我需要它來執行這個自定義函數:

- (void)cancelTapped { 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notification" message:@"Do you want to delete everything and go back to product selection?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes"]; 
    [alert setTag:1]; 
    [alert show]; 
} 

如何/我在哪裏把這個功能?它在按鈕屬性中?或者我會在一個自定義類/控制器中編寫它並將其鏈接到它?

2:我如何得到它偵聽警惕回報: - alertView:didDismissWithButtonIndex:

3:從那裏,怎麼會我會寫隱藏頁面和彈出視圖的邏輯是什麼?

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { 
    if (alertView.tag == 1 && buttonIndex == 1) { 
     // Delete data and return to lobby 
     [self.navigationController popViewControllerAnimated:YES]; 
    } 
} 
+0

你如何創建欄按鈕項目?你有沒有嘗試將這段代碼添加到你的視圖控制器? – Wain

+0

這是回(leftBarButton)項目..? –

+0

@我去了故事板,並在編輯器視圖中創建了一個新的UIBarButton。 庫馬爾,是的。 – Camerz007

回答

1

您將需要一個自定義的UIViewController容納邏輯爲您的按鈕,警報視圖交互。我假設你知道如何做到這一點。

完成後,假設你有你的視圖控制器的按鈕屬性的引用,你可以將目標編程添加到您的按鈕,並通過選擇cancelTapped作爲參數:

[myButton addTarget:self action:@selector(cancelTapped) forControlEvents:UIControlEventTouchUpInside]; 

另外,您可以控制從Storyboard中的按鈕拖動到自定義UIViewController的頭文件,並定義一個IBAction。這將創建一個在您實現一個空cancelTapped方法,然後你可以在添加你的邏輯。

至於監聽UIAlertView中的郵件,您將需要通過「自我」爲使您的自定義的UIViewController的UIAlertView中的委託在下列聲明中委託:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notification" message:@"My Message" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes"]; 

您的CustomViewController也應聲明爲UIAlertViewDelegate。

CustomViewController.h

@interface CustomViewController : UIViewController<UIAlertViewDelegate> 
@end 

希望這有助於!

0

通過使用VIewWillDisappear方法NavigationItem的detect the press of The back button

-(void) viewWillDisappear:(BOOL)animated { 
    if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) { 
     // Navigation button was pressed. Do some stuff 
     [self cancelTapped]; 

    } 
    [super viewWillDisappear:animated]; 
} 
- (void)cancelTapped { 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notification" message:@"Do you want to delete everything and go back to product selection?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes"]; 
    [alert setTag:1]; 
    [alert show]; 
} 

欲瞭解更多信息& &還定製的UIBarButtonItem Check Here

相關問題