你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iPhone開發[四]常用控件之ActionSheet與AlertView

iPhone開發[四]常用控件之ActionSheet與AlertView

編輯:IOS開發綜合

關鍵詞:ActionSheet AlertView
1、創建一個Single View Application工程,命名為:ActionSheetDemo,如下圖

2、在ViewController.xib上放置一個Button,Title為“刪除”
      修改ViewController.h,添加操作:
[cpp] 
<span style="font-family:Microsoft YaHei;font-size:18px;">-(IBAction)delete:(id)sender;</span> 
     將Button“刪除”的Touch Up Inside與操作delete關聯起來(操作方法上一篇已講過)
3、修改Controller ViewController,讓其實現協議UIActionSheetDelegate:
[cpp] view plaincopy
<span style="font-family:Microsoft YaHei;font-size:18px;">//修改,實現UIActionSheetDelegate協議 
@interface ViewController : UIViewController<UIActionSheetDelegate></span> 

4、修改ViewController.m
     實現操作delete,如下
[cpp] 
<span style="font-family:Microsoft YaHei;font-size:18px;">//執行刪除操作 
-(IBAction)delete:(id)sender{ 
    UIActionSheet *actionSheet = [[UIActionSheet alloc] 
                                  initWithTitle:@"確定要刪除該服務器?"  
                                  delegate:self //actionSheet的代理,按鈕被按下時收到通知,然後回調協議中的相關方法 
                                  cancelButtonTitle:@"取消" 
                                  destructiveButtonTitle:@"確定" 
                                  otherButtonTitles:nil]; 
    //展示actionSheet 
    [actionSheet showInView:self.view]; 
}</span> 
ViewController作為ActionSheet的代理,需要實現協議中定義的方法,有2中實現方法
方法一:實現didDismissWithButtonIndex,在ActionSheet消失後做提示處理
[cpp] 
<span style="font-family:Microsoft YaHei;font-size:18px;">//方法1 
-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{ 
    NSLog(@"didDismissWithButtonIndex"); 
    UIAlertView *alert = nil; 
    if(buttonIndex == [actionSheet destructiveButtonIndex]){//確定 
        //NSLog(@"確定"); 
        alert = [[UIAlertView alloc] 
                 initWithTitle:@"結果"  
                 message:@"刪除完畢"  
                 delegate:self  
                 cancelButtonTitle:@"確定"  
                 otherButtonTitles:nil]; 
        [alert show]; 
    }else if(buttonIndex == [actionSheet cancelButtonIndex]){//取消 
        NSLog(@"取消"); 
    } 
}</span> 

方法而,實現clickedButtonAtIndex,在ActionSheet上的按鈕被點擊時做處理
[cpp] 
y:Microsoft YaHei;font-size:18px;">//方法2 
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ 
    NSLog(@"clickedButtonAtIndex"); 
    UIAlertView *alert = nil; 
    if(buttonIndex == [actionSheet destructiveButtonIndex]){//確定 
        //NSLog(@"確定"); 
        //執行刪除操作 
        [self doDelete]; 
        alert = [[UIAlertView alloc] 
                 initWithTitle:@"結果"  
                 message:@"刪除完畢"  
                 delegate:self  
                 cancelButtonTitle:@"確定"  
                 otherButtonTitles:nil]; 
        [alert show]; 
    }else if(buttonIndex == [actionSheet cancelButtonIndex]){//取消 
        NSLog(@"取消"); 
    } 
}</span> 

兩種方法都是通過buttonIndex判斷當前點擊的按鈕,做不同處理
演示方法,doDelete沒有實現實際操作,如下
[cpp] 
<span style="font-family:Microsoft YaHei;font-size:18px;">-(void)doDelete{ 
    NSLog(@"執行刪除操作"); 
}</span> 

5、編譯、運行,效果如下:


  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved