IOS Using UIAlertView to show alerts

UIAlertView in other words, it‘s a dialog box. You want to show a message or ask user to confirm an action. UIAlertView would come in handy. Here, I create a simple project and show a alert view when suer click a button named "Show A Simple Alert View".
The implementation is in class ViewController. Below is the main code.

//
//  ViewController.m
//  Chapter1UIAlertView
//
//  Created by Winter on 15/8/13.
//  Copyright (c) 2015年 YLD. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    UIButton *buttonSimple = [[UIButton alloc] initWithFrame:CGRectMake(20.0f, 30.0f, 300.0f, 30.0f)];
    [buttonSimple setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [buttonSimple setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [buttonSimple setBackgroundColor:[UIColor orangeColor]];
    [buttonSimple setTitle:@"Show A Simple Alert View" forState:UIControlStateNormal];
    [buttonSimple addTarget:self action:@selector(showAlert) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:buttonSimple];
}

- (void) showAlert {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Warning"
                                                        message:@"Hello"
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil, nil];

    [alertView show];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Run app you and click the button you would see below scene.

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" >

Now, you probably appreciate to have more button in alert view and each button would invoke different function while user clicking it. To implement this function you need to make you view controller accord with protocol UIAlertViewDelegate and implement
method  alertView:clickedButtonAtIndex:. I put a label at the top of view to see what button had clicked in alert view and its button index. The cancel button‘s index is 0.

The ViewController.h code as below.

//
//  ViewController.h
//  Chapter1UIAlertView
//
//  Created by Winter on 15/8/13.
//  Copyright (c) 2015年 YLD. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIAlertViewDelegate>{
    UILabel *label_;
}

@end

Add below codes to viewDidLoad in ViewController.m file. These codes would create a label in top of the view and create a button next to last view element. When you click this button it would invoke method showAlertWithMoreButtons.

    label_ = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 30.0f, self.view.bounds.size.width, 60.0f)];
    label_.backgroundColor = [UIColor blackColor];
    label_.textAlignment = NSTextAlignmentCenter;
    label_.textColor = [UIColor whiteColor];
    [self.view addSubview:label_];

    UIButton *buttonMore = [[UIButton alloc] initWithFrame:CGRectMake(20.0f, 140.0f, 300.0f, 30.0f)];
    [buttonMore setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [buttonMore setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [buttonMore setBackgroundColor:[UIColor brownColor]];
    [buttonMore setTitle:@"Show More Buttons Alert View" forState:UIControlStateNormal];
    [buttonMore addTarget:self action:@selector(showAlertWithMoreButtons) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonMore];

Add method alertView:clickedButtonAtIndex: and implement it.

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    label_.text =[NSString stringWithFormat:@"%@ %ld", [alertView buttonTitleAtIndex:buttonIndex], (long)buttonIndex];
}

Run the app and click button "Show More Buttons Alert View".

If you want user input some text, you can set alert view style as UIAlertViewStylePlainTextInput

- (void) showAlertTextInput {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Warning"
                                                        message:@"Do you like apple?"
                                                       delegate:self
                                              cancelButtonTitle:@"Cancel"
                                              otherButtonTitles:@"OK", nil];

    [alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];

    [alertView show];
}

You can get what user had input via [alertView textFieldAtIndex:0].text. I made it display in label after user finishing inputting and clicking OK.

<pre name="code" class="objc">- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    label_.text =[NSString stringWithFormat:@"%@ %ld TextFiled 0: %@", [alertView buttonTitleAtIndex:buttonIndex], (long)buttonIndex, [alertView textFieldAtIndex:0].text];

}

If you want user input some secure text, you can set alert view style as UIAlertViewStyleSecureTextInput

If you want user input login information user name and password, you can set alert view style as UIAlertViewStyleLoginAndPasswordInput. Use [alertView textFieldAtIndex:0].text
to get login text, [alertView textFieldAtIndex:1].text to get password text.

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" >

时间: 2024-10-11 08:28:55

IOS Using UIAlertView to show alerts的相关文章

【iOS】UIAlertView 点击跳转事件

iOS 开发中,UIAlertView 经常用到.这里记录下曾用到的点击跳转事件. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"退出" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil]; if ([@"请记住新密码" isEqualToString:resu

iOS中UIAlertView的使用方法

UIAlertView * alertView=[[UIAlertView alloc]initWithTitle:nil message:@"真的要退出?" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil]; alertView.tag=11; [alertView show]; 然后实现 UIAlertViewDelegate的代理方法 -(void)ale

IOS中UIAlertView(警告框)常用方法总结

一.初始化方法 - (instancetype)initWithTitle:(NSString *)title message:(NSString*)message delegate:(id /*<UIAlertViewDelegate>*/)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...; 这个方法通过设置一个标题,内容,

iOS中UIAlertView的点击响应事件

1:先遵守UIAlertViewDelegate 2:- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ if (buttonIndex==0) { NSLog(@"你点击了取消"); }else if (buttonIndex==1){ [self.myOverButton setTitle:@"已结束" forState:UIControl

IOS UIAlertView(警告框)方法总结

IOS中UIAlertView(警告框)常用方法总结 一.初始化方法 - (instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id /*<UIAlertViewDelegate>*/)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonT

解决js的confirm转换为iOS UIAlertView的title问题

使用cordova做跨平台开发的时候,碰到了js调用confirm转换成iOS的UIAlertView的title显示成xxxx.html的问题. 我想到的第一种办法就是监听UIApplication的windows,然后手动修改当前alert的title.以前做过类似的监听系统通知,修改样式的.不过这样跟JS交互起来效率就低了好多. 另一种思路就是如果能够找到confirm转化成alert的方法不是更好?这样就可以重载该方法替换成任意的样式了. 在stackoverflow上还真找到了,哇哈哈

iOS开发者福利之精品源码汇总!免费下载

汇总一些看着不错的源码,有需要的朋友过来下载吧!{:4_102:} 1.用swift制作的色彩炫丽的进度条-KDCircularProgressKDCircularProgress是使用swift制作的色彩炫丽的进度条,可以加入多种颜色来控制进度条的渐变效果.支持xcode6.3 ios8.3.源码下载地址:用swift制作的色彩炫丽的进度条-KDCircularProgress 2.ios精品:如何实现快速集成下拉上拉刷新效果源码 使用方法: .添加头部控件的方法 [self.tableVie

Facebook

Facebook登录为iOS Facebook的SDK为iOS提供了各种登录的经验,你的应用程序可以使用它来 ??验证一个人.这份文件包括了所有你需要知道,以落实Facebook登录在你的iOS应用程序的信息.如果您想了解更多关于Facebook登录一般而言,为什么你可能想使用它,你可以在我们这样做的登录门户. 有两种方式可以实现Facebook登录在你的iOS应用方法:使用Facebook的登录按钮,或者实现你使用API调用自定义登录界面.我们将介绍这两个步骤一步的教程,包括示例代码和示例应用

iOS 8及以后版本 如何创建UIAlertView?

1. Important: UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated.) To create and manage alerts in iOS 8 and later, instead use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert. //UIAlertView和UI