天天看點

UIAlertView的使用方法

UIAlertView類似于android中的對話框 或 AlertDialog ,但是,ios中使用起來要麻煩得多。

下面這段代碼是一段典型的應用:

UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"hello"

message:@"ipad ,i come"

delegate:self

cancelButtonTitle:@"ok"

otherButtonTitles:nil];

[alert show];

[alert release];

但是,如果複雜一點,就麻煩了,如果上面加上幾個按鈕,如:

UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"hello"

message:@"ipad ,i come"

delegate:self

cancelButtonTitle:@"ok"

otherButtonTitles:@"cancel",@"Ignore",nil ];

view 中會顯示3個按鈕,那怎麼知道使用者選擇了哪個按鈕呢?

步驟如下:

1、在修改.h檔案,添加對alertview的處理,如下:

@interface pad4ViewController : UIViewController

<UIAlertViewDelegate>

{.。。

2、在.m檔案中添加對alertview事件的響應,如下:

- (void) alertView:(UIAlertView *)alertview

clickedButtonAtIndex:(NSInteger)buttonIndex{

NSLog(@"%@",alertview.title);

}

以上方法實作了目前.m中所有UIAlertView的事件響應,alertview指明是哪個view,buttonIndex指明是哪介按鈕。