@implementation AlertViewTestViewController /* Tasks Creating Alert Views – initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles: Setting Properties delegate property title property message property visible property Configuring Buttons – addButtonWithTitle: numberOfButtons property – buttonTitleAtIndex: cancelButtonIndex property firstOtherButtonIndex property Displaying – show Dismissing – dismissWithClickedButtonIndex:animated: 無例 */ // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. - (void)viewDidLoad { //初始化AlertView UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"AlertViewTest" message:@"message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OtherBtn",nil]; //設定標題與資訊,通常在使用frame初始化AlertView時使用 alert.title = @"AlertViewTitle"; alert.message = @"AlertViewMessage"; //這個屬性繼承自UIView,當一個視圖中有多個AlertView時,可以用這個屬性來區分 alert.tag = 0; //唯讀屬性,看AlertView是否可見 NSLog(@"%d",alert.visible); //通過給定標題添加按鈕 [alert addButtonWithTitle:@"addButton"]; //按鈕總數 NSLog(@"numberOfButtons:%d",alert.numberOfButtons); //擷取指定索引的按鈕的標題 NSLog(@"buttonTitleAtIndex:%@",[alert buttonTitleAtIndex:2]); //獲得取消按鈕的索引 NSLog(@"cancelButtonIndex:%d",alert.cancelButtonIndex); //獲得第一個其他按鈕的索引 NSLog(@"firstOtherButtonIndex:%d",alert.firstOtherButtonIndex); //顯示AlertView [alert show]; [alert release]; [super viewDidLoad]; } /* // Override to allow orientations other than the default portrait orientation. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait); } */ - (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 { // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (void)dealloc { [super dealloc]; } #pragma mark -- UIAlertViewDelegate -- //根據被點擊按鈕的索引處理點擊事件 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { NSLog(@"clickedButtonAtIndex:%d",buttonIndex); } //AlertView已經消失時 - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { NSLog(@"didDismissWithButtonIndex"); } //AlertView即將消失時 - (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex { NSLog(@"willDismissWithButtonIndex"); } - (void)alertViewCancel:(UIAlertView *)alertView { NSLog(@"alertViewCancel"); } //AlertView已經顯示時 - (void)didPresentAlertView:(UIAlertView *)alertView { NSLog(@"didPresentAlertView"); } //AlertView即將顯示時 - (void)willPresentAlertView:(UIAlertView *)alertView { NSLog(@"willPresentAlertView"); } @end