加上傑夫的答案,我認爲你必須啓用另一個按鈕,以便點擊按鈕時放置你的邏輯。
爲了你的代碼創建按鈕:
- (IBAction)button1
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello"
message:@"Do you like smoking?"
delegate:self
cancelButtonTitle:@"Yes"
otherButtonTitles:@"No", nil];
[alert show];
}
但我們知道當點擊該按鈕之前,您必須啓用第一其它按鈕,通過調用此委託方法:
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
return YES;
}
這將啓用您創建的NO按鈕。然後,你將能夠用clickedButtonAtIndex方法做一些邏輯,我想你會這樣做。
落實UIAlertView中的委託方法:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
// i love smoking!
} else if (buttonIndex == 1) {
// i hate smoking!
}
}
一定要申報UIAlertViewDelegate在你的頭類。
確保alertViewShouldEnableFirstOtherButton:方法返回YES,否則無法在按下按鈕時放入邏輯。
希望這會有所幫助! :)
委託方法。 – Larme