ios 为什么使用 uialertcontroller 抛弃uialertview

 我来答
夜独行孤单
2016-03-08 · TA获得超过3460个赞
知道大有可为答主
回答量:2803
采纳率:82%
帮助的人:631万
展开全部
// Newly initialized alert view.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Edit" message:@"Please Modify the Info" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Sure", @"Other", nil];

// 为下面修改数据用
// This property is inherited from the UIView, You can use this property to distinguish when a AlertView has multiple view
alertView.tag = indexPath.row;

// Adds a button to the receiver with the given title.
[alertView addButtonWithTitle:@"addBtn"];

/**
UIAlertViewStyle = 以下4种
UIAlertViewStyleDefault,
UIAlertViewStyleSecureTextInput, //密码输入框风格
UIAlertViewStylePlainTextInput, //普通输入框风格
UIAlertViewStyleLoginAndPasswordInput //账号密码框风格
*/
// An alert that allows the user to enter text. Available in iOS 5.0 and later.
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;

// Returns the text field at the given index
UITextField *textField = [alertView textFieldAtIndex:0];
textField.text = model.title;

// The number of buttons on the alert view. (read-only)
NSLog(@"The total number of button is : %ld", alertView.numberOfButtons);

// Returns the title of the button at the given index.
NSLog(@"The button title at the specified index : %@", [alertView buttonTitleAtIndex:1]);

// The index number of the cancel button.
NSLog(@"Index for the cancel button is : %ld",alertView.cancelButtonIndex);

// -1 if no otherButtonTitles or initWithTitle:... not used
NSLog(@"The index of the first other button is (read-only) : %ld",alertView.firstOtherButtonIndex);

// show UIAlertView
[alertView show];

UIAlertView的一些基本属性

/**
//根据被点击按钮的索引处理点击事件
Called when a button is clicked. The view will be automatically dismissed after this call returns
*/
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

//AlertView即将显示时
-(void)willPresentAlertView:(UIAlertView *)alertView;

// AlertView已经显示时的事件
-(void)didPresentAlertView:(UIAlertView *)alertView;

// ALertView即将消失时的事件
// This method is invoked before the animation begins and the view is hidden.
-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex;

// AlertView已经消失时执行的事件
// This method is invoked after the animation ends and the view is hidden.
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;

// AlertView的取消按钮的事件
// If not defined in the delegate, we simulate a click in the cancel button
-(void)alertViewCancel:(UIAlertView *)alertView;

// 1.实例化alert:alertControllerWithTitle
UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"编辑" message:@"请修改菜单名称:" preferredStyle:UIAlertControllerStyleAlert];

// 2.实例化按钮:actionWithTitle
// 为防止block与控制器间循环引用,我们这里需用__weak来预防
__weak typeof(alert) wAlert = alert;
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
// 点击确定按钮的时候, 会调用这个block
NSLog(@"%@",[wAlert.textFields.firstObject text]);

}]];

[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];

// 添加文本框(只能添加到UIAlertControllerStyleAlert的样式,如果是preferredStyle:UIAlertControllerStyleActionSheet则会崩溃)
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {

textField.text = model.title;

//监听文字改变的方法
[textField addTarget:self action:@selector(textFieldsValueDidChange:) forControlEvents:UIControlEventEditingChanged];
}];

[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {

textField.secureTextEntry = YES; // 密文形式显示
textField.text = model.price;
}];

// 3.显示alertController:presentViewController
[self presentViewController:alert animated:YES completion:nil];

UIAlertController的基本使用
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式