这几天在做推送消息的处理,要有个提示框.花了几个小时,我整理了下UIAlertView与UIAlertController,闲话不多说,上代码.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self alertStyleWithTwoTextField];
[self alertStyleWithTextField];
[self alertStyle];
[self actionSheetStyle];
[self loginAndPassword];
[self secureText];
[self plainText];
[self defaultAlert];
[self actionSheet];
}
下面是几种状态
//原来的最
- (void)defaultAlert
{
// iOS8被废弃
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alertView" message:@"默认样式" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"查看",@"评论", nil];
alert.alertViewStyle = UIAlertViewStyleDefault;
[alert show];
}
当你要使用提示框的按钮,比如说跳转的时候这时候你就要用到代理了
#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"点击的%ld",buttonIndex);
if (buttonIndex == 1) {
NSLog(@"点击了这个是有效果的");
secondViewController *secondVC = [[secondViewController alloc] init];
[self presentViewController:secondVC animated:YES completion:nil];
}
}
要说的一点是,这个提示框下方的按钮你可以给他设置tag值,如果不设置的话那么从下往下数buttonIndex的值从1开始.最后的取消的buttonIndex值是0.
然后说说其他的
// 带有明文输入框
- (void)plainText
{
// iOS8被废弃
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"明文" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
}
// 带有密文输入框
- (void)secureText
{
// iOS8被废弃
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"密文" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
alert.alertViewStyle = UIAlertViewStyleSecureTextInput;
[alert show];
}
// 带有登录和密码输入框
- (void)loginAndPassword
{
// iOS8被废弃
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"登录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"登录",@"注册", nil];
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[alert show];
}
- (void)actionSheet
{
// iOS8被废弃
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"查看",@"评论", nil];
[sheet showInView:self.view];
}
#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"%ld",buttonIndex);
}
可以复制过去模拟机看看.具体的也没有什么好详细说的.这个比较基础了.以上都是比较老的了 ,下面说说iOS9之后的提示框
- (void)actionSheetStyle {
UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:nil message:@"actionSheetStyle" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *showAllInfoAction = [UIAlertAction actionWithTitle:@"查看" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *pickAction = [UIAlertAction actionWithTitle:@"评论" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[actionSheetController addAction:pickAction];
[actionSheetController addAction:cancelAction];
[actionSheetController addAction:showAllInfoAction];
[self presentViewController:actionSheetController animated:YES completion:nil];
}
- (void)alertStyle
{
UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:@"新版的alertView" message:@"这个在哪里的" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *showAllInfoAction = [UIAlertAction actionWithTitle:@"查看" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *pickAction = [UIAlertAction actionWithTitle:@"评论" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
secondViewController *secondVC = [[secondViewController alloc] init];
[self presentViewController:secondVC animated:YES completion:nil];
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[actionSheetController addAction:pickAction];
[actionSheetController addAction:cancelAction];
[actionSheetController addAction:showAllInfoAction];
[self presentViewController:actionSheetController animated:YES completion:nil];
}
- (void)alertStyleWithTextField
{
UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:nil message:@"输入姓名" preferredStyle:UIAlertControllerStyleAlert];
[actionSheetController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"请输入姓名";
}];
UIAlertAction *determineAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[actionSheetController addAction:determineAction];
[actionSheetController addAction:cancelAction];
[self presentViewController:actionSheetController animated:YES completion:nil];
}
- (void)alertStyleWithTwoTextField
{
UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:nil message:@"登录" preferredStyle:UIAlertControllerStyleAlert];
[actionSheetController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"账号";
}];
[actionSheetController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"密码";
textField.secureTextEntry = YES;
}];
UIAlertAction *determineAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[actionSheetController addAction:determineAction];
[actionSheetController addAction:cancelAction];
[self presentViewController:actionSheetController animated:YES completion:nil];
}
根据实际的使用对我来说是个更加的方便了,因为毕竟少了很多的代理,你想要的按钮动作可以直接添加到block块里面,一个对一个不容易混乱.
另外感谢@VV木公子(作者)的文章.