
Alerts 警告訊息的使用方法可以參考以下程式碼,首先是 Alerts 警告訊息的介面先告方式。(View-based Template)
//產生一個Alerts對話框並設定顯示內容與按鈕 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Furncae Digital" message:@"請選擇聯絡方式" delegate:self cancelButtonTitle:@"還是算了" otherButtonTitles:@"電話", @"E-Mail", nil];
//也可以使用這種方式增加事件按鈕 [alert addButtonWithTitle:@"MSN"];
//將Alerts顯示於畫面上 [alert show];
//好孩子都會做的記憶體釋放 [alert release];
如果只想要使用單一一個按鈕來確認並隱藏 Alerts 警告訊息,在宣告時只要將按鈕名稱寫在cancelButtonTitle: 上,而 otherButtonTitle: 則填入 nil,同樣在宣告時不想出現的欄位時也可以填入 nil。
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Alert Message" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 下面是按鈕事件的寫法,其程式碼如下。
//判斷Alerts按鈕事件 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { //將按鈕的Title當作判斷的依據 NSString *title = [alertView buttonTitleAtIndex:buttonIndex]; if([title isEqualToString:@"電話"]) { contentLabel.text = @"可惜我們目前還沒有電話"; } else if([title isEqualToString:@"E-Mail"]) { contentLabel.text = @"furnacedigital@gmail.com"; } else if([title isEqualToString:@"MSN"]) { contentLabel.text = @"可惜我們目前還沒有MSN"; } else if([title isEqualToString:@"還是算了"]) { contentLabel.text = @""; } }
沒有留言:
張貼留言