UIActionSheet,UIAlertView技术分享

UIActionSheet

[objc] view plaincopy
 
  1. #import "FirstViewController.h"  
  2.   
  3. @interface FirstViewController ()<UIActionSheetDelegate,UIAlertViewDelegate>  
  4. @property (retain, nonatomic) IBOutlet UILabel *aLabel;  
  5. @property (retain, nonatomic) IBOutlet UITextField *textField;  
  6. @end  
  7. @implementation FirstViewController  
  8. - (void)viewDidLoad {  
  9.     [super viewDidLoad];  
  10.     // Do any additional setup after loading the view from its nib.  
  11. }  
  12. - (IBAction)upButton:(UIButton *)sender {  
  13.       
  14.       
  15.     //UIActionSheet  
  16.       
  17.     UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:@"选择" delegate:self cancelButtonTitle:@"取消按钮" destructiveButtonTitle:@"确认按钮" otherButtonTitles:nil, nil nil];  
  18.     sheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;  
  19. //      更改标题  
  20.     sheet.title = @"请点击您的选择";  
  21. //    添加按钮  
  22.     //添加的按钮位置从第三个算起  
  23.     [sheet addButtonWithTitle:@"添加的按钮"];  
  24.     //红色按钮位置  
  25.     sheet.destructiveButtonIndex = 0;  
  26.     [sheet showInView:self.view];  
  27.     [sheet release];  
  28.       
  29.     
  30.     //UIAlertView  
  31.       
  32.     //一般来说,根据苹果官方制定的《iOS 用户界面指南》,在拥有两个按钮的对话框中,您应当将取消按钮放在左边  
  33.     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"是否已满十八岁?"  delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil nil];  
  34.         alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;  
  35.     //UIAlertViewStyleDefault 默认风格,无输入框  
  36.     //UIAlertViewStyleSecureTextInput 带一个密码输入框  
  37.     //UIAlertViewStylePlainTextInput 带一个文本输入框  
  38.     //UIAlertViewStyleLoginAndPasswordInput 带一个文本输入框,一个密码输入框  
  39.     [alert show];  
  40.     [alert release];  
  41. }  

第一种 ActionSheet单独使用

[objc] view plaincopy
 
  1. //第一种 ActionSheet单独使用  
  2. //接收ActionSheet点击事件  
  3. //该方式由UIActionSheetDelegate协议定义,点击ActionSheet的按钮后自动执行  
  4. - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{  
  5.   
  6.     switch (buttonIndex) {  
  7.         case 0:  
  8.             self.aLabel.text = self.textField.text;  
  9.             break;  
  10.         case 1:  
  11.             break;  
  12.         case 2:  
  13.             NSLog(@"让你点你还真点");  
  14.             break;  
  15.         default:  
  16.             break;  
  17.     }  
  18.   
  19. }  

第二种,单独使用UIAlertView

[objc] view plaincopy
 
  1. //接收UIAlertView点击事件  
  2. -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex  
  3. {  
  4.    
  5.       
  6.     switch (buttonIndex) {  
  7.         case 0:  
  8.             break;  
  9.         case 1:  
  10.             self.aLabel.text = self.textField.text;  
  11.             break;  
  12.         default:  
  13.             break;  
  14.     }  
  15.   
  16. }  

第三种混合使用

[objc] view plaincopy
 
  1. - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{  
  2.   
  3.      NSString *string=[NSString stringWithFormat:@"你选择了 %@",[actionSheet buttonTitleAtIndex:buttonIndex]];  
  4.       
  5.     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"输入账户和密码" message:string  delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil nil];  
  6.     alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;  
  7.     //UIAlertViewStyleDefault 默认风格,无输入框  
  8.     //UIAlertViewStyleSecureTextInput 带一个密码输入框  
  9.     //UIAlertViewStylePlainTextInput 带一个文本输入框  
  10.     //UIAlertViewStyleLoginAndPasswordInput 带一个文本输入框,一个密码输入框  
  11.   
  12.     switch (buttonIndex) {  
  13.         case 0:  
  14.             [alert show];  
  15.             break;  
  16.         case 1:  
  17.             break;  
  18.         default:  
  19.             break;  
  20.     }  
  21.      [alert release];  
  22.    
  23. }  
[objc] view plaincopy
 
    1. - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex  
    2. {  
    3.     //该方法由UIAlertViewDelegate协议定义,在点击AlertView按钮时自动执行,所以如果这里再用alertView来弹出提示,就会死循环,不停的弹AlertView  
    4.     //    NSString * string=[NSString stringWithFormat:@"你点击了 %@",[alertView buttonTitleAtIndex:buttonIndex]];  
    5.     NSString * string=[NSString stringWithFormat:@"你点击了 %@",[alertView buttonTitleAtIndex:buttonIndex]];  
    6.       
    7.     switch (buttonIndex) {  
    8.         case 0:  
    9.             break;  
    10.         case 1:  
    11.             self.aLabel.text = self.textField.text;  
    12.             break;  
    13.         default:  
    14.             break;  
    15.     }  
    16.       
    17. //    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:string delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];  
    18. //        [alert show];  
    19.     NSLog(@"%@",string);  
    20. //    NSLog(@"输入 %@",[[alertView textFieldAtIndex:0] text]);  
    21.     //获取第一个文本框输入的文本,如果没有文件框,会异常,索引从0开始  
    22.       
    23. }  
    24. [objc] view plaincopy
       
      1. <span style="background-color: rgb(255, 0, 0);"><span style="color:#99ff99;">最终效果:</span></span>  
原文地址:https://www.cnblogs.com/Yishu/p/4867331.html