ios学习笔记之UI篇(三):操作表单(action sheet)和警告(alert)

来源:互联网 发布:数据有效性做二级菜单 编辑:程序博客网 时间:2024/05/19 14:17

在UI篇二的时候我们创建了一个按钮,现在,我们为这个按钮添加action方法,用户点击按钮触发事件产生操作表单:action方法代码如下:

- (IBAction)buttonPressed:(id)sender {    UIActionSheet *actionSheet=[[UIActionSheet alloc]initWithTitle:@"Are you sure?" delegate:self cancelButtonTitle:@"No way!" destructiveButtonTitle:@"Yes,I'm sure" otherButtonTitles:nil];    [actionSheet showInView:self.view];}

实现actionsheet的按钮的动态交互就要采用代理协议,实现的协议为

<UIActionSheetDelegate>即在.h文件中添加协议,情况如下:

@interface ViewController :UIViewController<UIActionSheetDelegate>

然后在.m文件中实现协议中的

-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex

方法,具体实现代码为:

-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{    //if (buttonIndex!=[actionSheet cancelButtonIndex]) {    if (buttonIndex==[actionSheet destructiveButtonIndex]) {        NSString *msg=nil;        if (self.nameField.text.length>0) {            msg=[NSString stringWithFormat:@"You can breathe easy,%@,everything went ok",self.nameField.text];        } else {            msg=@"You can breathe easy,everything went ok.";        }        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"something was done" message:msg delegate:self cancelButtonTitle:@"Phew!" otherButtonTitles:nil];        [alert show];    }}
当点击destructivebutton时,弹出警告(alert)