歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> UIAlertView警報-iOS開發

UIAlertView警報-iOS開發

日期:2017/3/1 10:28:35   编辑:Linux編程
  1. UIAlertView* myAlert = [[UIAlertView alloc]
  2. initWithTitle:@"sorry"
  3. message:@"1234567890"
  4. delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
  5. myAlert.delegate = self;
  6. [myAlert show];
  7. //[myAlert release];//如果未在下面的委托中release的話記得要在這裡release,否則內存洩漏了
這裡設置delegate有啥用出,加入我們僅僅是只要顯示一個彈窗警告,而不采取其他動作我們可以安全用如下代碼解決:
  1. [[[[UIAlertView alloc]
  2. initWithTitle:@"sorry"
  3. message:@"1234567890"
  4. delegate:self
  5. cancelButtonTitle:@"OK"
  6. otherButtonTitles:nil, nil]
  7. autorelease]
  8. show];

設置委托有啥作用呢?我們可以實現UIAlertView的一個委托方法,讓後可以在這個方法裡處理按下按鈕後的動作,根據用戶按下的哪個按鈕來決定進行射門樣的操作,比如按下OK按鈕與按下Cancel按鈕的後要進行的操作必然有可能不同:

  1. -(void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
  2. NSLog(@"Button %d pressed",buttonIndex);
  3. [alertView release];
  4. }
看到否?我們可以通過buttonIndex來判斷用戶按下了哪個按鈕,然後來進行相應處理,同時我們還可以在這裡面release,因為我們在上面的代碼中可能不確定需要在哪裡release,所以在按下按鈕後release是不是最安全的呢?
Copyright © Linux教程網 All Rights Reserved