iOS 基础小结 常用控件之二

本文总结了iOS开发中常用的两种控件:UIAlertView和UIActionSheet。详细介绍了UIAlertView的使用,包括初始化、回调方法以及设置样式,特别展示了如何创建带输入框的警告框。UIActionSheet的基本用法也进行了讲解,帮助开发者更好地理解和应用这些控件。
摘要由CSDN通过智能技术生成

一,UIAlertView警告框

(1)   UIAlertView * alert = [[UIAlertViewalloc]

                           initWithTitle:@"Notice"message:@"the alert"delegate:selfcancelButtonTitle:@"OK"otherButtonTitles:@"Options1",@"Options2",nil];

    [alert show];

很简单,上述方法可以产生并显示一个警告框。警告框上的button的内容就是设置ButtonTitle。关键是警告框给提供的协议方法。

(2)

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

这个方法是当用户点击警告框中的某个按键时会触发该方法,buttonIndex代表按钮的索引,从0开始

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex

这个方法是完全隐藏警示框出来之后会触发

- (void)alertViewCancel:(UIAlertView *)alertView

这个是在警告框被取消时触发

- (void) alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex

这个方法是在将要显示隐藏的时候触发

- (void)willPresentAlertView:(UIAlertView *)alertView

这个是警告框将要显示出来的时候触发

-(void) didPresentAlertView:(UIAlertView *)alertView

这个是警告框完全显示出来的时候触发

(3)该控件有个属性,actionSheetStyle,用来设置风格,比如可以使其带有一个输入框,带有密码输入框等,举个栗子,输入用户名和密码

- (IBAction)clicked:(id)sender {

    UIAlertView * alert = [[UIAlertViewalloc]

                           initWithTitle:@"Log on"message:@"please input the user name and the password"delegate:selfcancelButtonTitle:@"Cancle"otherButtonTitles:@"OK",nil];

    alert.alertViewStyle =UIAlertViewStyleLoginAndPasswordInput;

    [alert textFieldAtIndex:1].keyboardType =UIKeyboardTypeNumberPad;

    [alert show];

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{    

    if (buttonIndex == 1) {

        UITextField * nameField = [alertViewtextFieldAtIndex:0];

        UITextField * passField = [alertViewtextFieldAtIndex:1];

        

        NSString * msg = [NSStringstringWithFormat:@"you have input the user name : %@ and the password : %@",nameField.text,passField.text];

        UIAlertView * alert = [[UIAlertViewalloc]initWithTitle:@"notice"message:msg delegate:nilcancelButtonTitle:@"OK"otherButtonTitles: nil];

        [alert show];

    }

}

- (void) willPresentAlertView:(UIAlertView *)alertView{

    for (UIView * viewin alertView.subviews) {

        if ([view isKindOfClass:[UILabelclass]]) {

            UILabel * lable = (UILabel *)view;

            lable.textAlignment =UITextAlignmentLeft;

        }

    }

}

二,UIActionSheet

这个控件跟Alert非常之像,写一点简单的使用就可以知道了

- (IBAction)clicked:(id)sender {  

    UIActionSheet * sheet = [[UIActionSheet alloc]initWithTitle:@"make sure to delete" delegate:self cancelButtonTitle:@"Cancle" destructiveButtonTitle:@"OK" otherButtonTitles:@"Options1", @"Options2",nil];

    sheet.actionSheetStyle = UIActionSheetStyleAutomatic;

    [sheet showInView:self.view];

}

- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{

    NSString * msg = [NSString stringWithFormat:@"you have clicked the %ld button",(long)buttonIndex];

    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"notice" message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];

    [alert show];

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值