UIAlertView怎么bindviewtap 触发事件件

96847人阅读
Object-C编程语言(40)
IOS开发(71)
首先,视图控制器必须得实现协议UIAlertViewDelegate中的方法,并指定delegate为self,才能使弹出的Alert窗口响应点击事件。
具体代码如下:
ViewController.h中的代码如下:
#import &UIKit/UIKit.h&
@interface ViewController : UIViewController&UIAlertViewDelegate&
ViewController.m中的详细代码:
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view from its nib
//初始化AlertView
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@&AlertViewTest&
message:@&message&
delegate:self
cancelButtonTitle:@&Cancel&
otherButtonTitles:@&OtherBtn&,nil];
//设置标题与信息,通常在使用frame初始化AlertView时使用
alert.title = @&AlertViewTitle&;
alert.message = @&AlertViewMessage&;
//这个属性继承自UIView,当一个视图中有多个AlertView时,可以用这个属性来区分
alert.tag = 0;
//只读属性,看AlertView是否可见
NSLog(@&%d&,alert.visible);
//通过给定标题添加按钮
[alert addButtonWithTitle:@&addButton&];
//按钮总数
NSLog(@&number Of Buttons :%d&,alert.numberOfButtons);
//获取指定索引的按钮标题
NSLog(@&buttonTitleAtIndex1:%@&,[alert buttonTitleAtIndex:1]);
NSLog(@&buttonTitleAtIndex2:%@&,[alert buttonTitleAtIndex:2]);
//获取取消按钮的索引
NSLog(@&cancelButtonIndex:%d&,alert.cancelButtonIndex);
//获取第一个其他按钮的索引
NSLog(@&firstOtherButtonIndex:%d&,alert.firstOtherButtonIndex);
//显示AlertView
[alert show];
[alert release];
#pragma marks -- UIAlertViewDelegate --
//根据被点击按钮的索引处理点击事件
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
NSLog(@&clickButtonAtIndex:%d&,buttonIndex);
//AlertView已经消失时执行的事件
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
NSLog(@&didDismissWithButtonIndex&);
//ALertView即将消失时的事件
-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
NSLog(@&willDismissWithButtonIndex&);
//AlertView的取消按钮的事件
-(void)alertViewCancel:(UIAlertView *)alertView
NSLog(@&alertViewCancel&);
//AlertView已经显示时的事件
-(void)didPresentAlertView:(UIAlertView *)alertView
NSLog(@&didPresentAlertView&);
//AlertView即将显示时
-(void)willPresentAlertView:(UIAlertView *)alertView
NSLog(@&willPresentAlertView&);
- (void)viewDidUnload
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet =
&&相关文章推荐
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1607900次
积分:7748
积分:7748
排名:第2381名
原创:59篇
转载:22篇
评论:250条
(1)(2)(1)(1)(2)(4)(2)(4)(2)(4)(2)(1)(3)(13)(5)(2)(17)(15)UIAlertView,UIActionSheet如何响应选项事件 - 简书
UIAlertView,UIActionSheet如何响应选项事件
一、UIAlertView:警告弹出窗口。主要用于提示用户一些操作后果,像退出程序等。一般如下代码创建一个警告窗口。
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil
message:@"是否退出当前账号?" delegate:self
cancelButtonTitle:@"否" otherButtonTitles:@"是", nil];
[alert show];//调用show才会显示警告。
如何实现响应“否”、“是”按钮呢?这需要它的代理对象实现UIAlertViewDelegate协议中的方法:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1) {
[UserData clearUser];
LoginViewController *next = [[LoginViewController alloc] init];
[self.navigationController pushViewController:next animated:YES];
警告窗口上的按钮从左向右编号从0开始一次增加。所以可以通过判断buttonindex来知道用户选择了哪个按钮。
二、UIActionSheet:动作表单。也是弹出一个菜单供用户选择相应的操作。
UIActionSheet *sheetView = [[UIActionSheet alloc] initWithTitle:@"修改头像"
delegate:self cancelButtonTitle:@"取消"
destructiveButtonTitle:nil otherButtonTitles:@"从相册选择",@"拍照", nil];
sheetView.tag = 1;
[sheetView showInView:self.view];
要响应你的选择也需要当前代理self实现UIActionSheetDelegate协议。注意其button编号是从other button开始不编号 cancel button.
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (actionSheet.tag == 1) {//头像
if (buttonIndex == 0) {
}else if (buttonIndex == 1){
}else if(actionSheet.tag == 2){//性别
if (buttonIndex == 0) {
sexlab.text = @"男";
}else if (buttonIndex == 1){
sexlab.text = @"女";温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
五、UIAlertController参考网址:
//初始化提示框控制器对象& & UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"内容" preferredStyle:UIAlertControllerStyleActionSheet];& & //弹出提示框& & [VC presentViewController:alert animated:YES completion:nil];
//初始化提示框按钮对象
UIAlertAction *action6 = [UIAlertAction actionWithTitle:@"人身攻击" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {& & & & //触发事件代码区域& & }];& & UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
//需要按一定顺序加入& & [alert addAction:action6];& & [alert addAction:action];
//设置文本框对象
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {& & & & // 可以在这里对textfield进行定制,例如改变背景色& & & & textField.backgroundColor = [UIColor orangeColor];& & & && & }];
//获取文本框对象
UITextField *txtF = alert.textFields.firstObject;
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
在LOFTER的更多文章
loftPermalink:'',
id:'fks_',
blogTitle:'UIAlertView(消息提示框)和UIAlertController',
blogAbstract:'一、消息提示框的介绍& UIAlertView是消息提示框UI控件,对于消息提示框的中的按钮事件采用的是事件委托机制。要实现事件响应,需要实现对应协议、重写函数达到目的。二、消息提示框的创建
//创建UIAlertView的对象
UIAlertView *aler=[[UIAlertView alloc]initWithTitle:@\"警告\" message:@\"密码错误\" delegate:self',
blogTag:'ios',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:8,
publishTime:9,
permalink:'blog/static/',
commentCount:0,
mainCommentCount:0,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}

我要回帖

更多关于 uialertview点击事件 的文章

 

随机推荐