uialertcontroller 怎么ios 设置不支持横屏屏

1977人阅读
针对错误:
Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and [UIAlertController shouldAutorotate] is returning YES
提示错误信息:
10:41:55.442 MobileLesson[] *** Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and [UIAlertController shouldAutorotate] is returning YES'
*** First throw call stack:
(0x185a5cf48 0x19a60ff80 0x185a5ce90 0x18b0b071b84 0x18b071afc 0x18bx18afeb020 0x18afea350 0xx18afea1d4 0x18aff7b0c 0x18b43ec7c 0x18bx18bx18b2a056c 0x18b2ad4bc 0x18afea13bd0 0x185aa11da4 0x 0x190b7c088 0x18b058ffc 0x1001dda98 0x19ae5e8b8)
libc++abi.dylib: terminating with uncaught exception of type NSException
解决办法,重写UIAlertContrller 的shouldAutorotate 方法,
#import &UIKit/UIKit.h&
@interface UIAlertController (Rotation)
- (BOOL)shouldA
#import &UIAlertController+Rotation.h&
@implementation UIAlertController (Rotation)
- (BOOL)shouldAutorotate
return NO;
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:37239次
排名:千里之外
原创:49篇
转载:15篇
(1)(1)(1)(1)(2)(4)(7)(4)(1)(2)(3)(2)(1)(3)(6)(15)(7)(2)(1)当前位置:
& Swift - 告警提示框(UIAlertController)的用法
Swift - 告警提示框(UIAlertController)的用法
发布:hangge
阅读:16754
自iOS8起,苹果就建议告警框使用UIAlertController来代替UIAlertView和UIActionSheel。下面总结了一些常见的用法(本文代码都已更新至Swift3)
1,简单的应用(同时按钮响应Handler使用闭包函数)
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
override func viewDidAppear(_ animated: Bool){
super.viewDidAppear(animated)
let alertController = UIAlertController(title: "系统提示",
message: "您确定要离开吗?", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
let okAction = UIAlertAction(title: "好的", style: .default, handler: {
print("点击了确定")
alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
2,除了弹出,还可以使用从底部向上滑出的样式
(注意:如果上拉菜单中有“取消”按钮的话,那么它永远都会出现在菜单的底部,不管添加的次序是如何)
let alertController = UIAlertController(title: "保存或删除数据", message: "删除数据将不可恢复",
preferredStyle: .actionSheet)
let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
let deleteAction = UIAlertAction(title: "删除", style: .destructive, handler: nil)
let archiveAction = UIAlertAction(title: "保存", style: .default, handler: nil)
alertController.addAction(cancelAction)
alertController.addAction(deleteAction)
alertController.addAction(archiveAction)
self.present(alertController, animated: true, completion: nil)
3,按钮使用“告警”样式(文字颜色变红,用来来警示用户)
let okAction = UIAlertAction(title: "好的", style: .destructive, handler: nil)
4,添加任意数量文本输入框(比如可以用来实现个登陆框)
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
override func viewDidAppear(_ animated: Bool){
super.viewDidAppear(animated)
let alertController = UIAlertController(title: "系统登录",
message: "请输入用户名和密码", preferredStyle: .alert)
alertController.addTextField {
(textField: UITextField!) -& Void in
textField.placeholder = "用户名"
alertController.addTextField {
(textField: UITextField!) -& Void in
textField.placeholder = "密码"
textField.isSecureTextEntry = true
let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
let okAction = UIAlertAction(title: "好的", style: .default, handler: {
//也可以用下标的形式获取textField let login = alertController.textFields![0]
let login = alertController.textFields!.first!
let password = alertController.textFields!.last!
print("用户名:\(login.text) 密码:\(password.text)")
alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
5,使用代码移除提示框
self.presentedViewController?.dismiss(animated: false, completion: nil)
6,提示框弹出后,过段时间自动移除
下面样例弹出一个不带按钮的消息提示框,过个两秒钟提示框自动消失。
let alertController = UIAlertController(title: "保存成功!",
message: nil, preferredStyle: .alert)
//显示提示框
self.present(alertController, animated: true, completion: nil)
//两秒钟后自动消失
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
self.presentedViewController?.dismiss(animated: false, completion: nil)Objective-C(43)
//UIActionSheet is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleActionSheet instead
UIActionSheet弃用。使用UIAlertController preferredStyle UIAlertControllerStyleActionSheet替换。
//UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead
UIAlertView弃用。使用UIAlertController preferredStyle UIAlertControllerStyleAlert替换
UIAlertController同时替代了
UIAlertView 和 UIActionSheet
UIAlertController对象向用户显示一个警告消息。这个类替换UIActionSheet和UIAlertView类显示警报。
使用警报控制器后必须设置行为和风格,目前使用presentViewController:animated:completion:方法。
除了向用户提示一个消息,你还可以做其他事情。为每个控件添加addAction:方法,监听动作细节。
当用户操作时,UIAlertController创建动作对象时,执行block中内容。
官方使用样例:
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@&My Alert&
message:@&This is an alert.&
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@&OK& style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
UIActionSheet使用
&UIAlertControllerStyleActionSheet的使用注意
1.不能有文本框
2.在iPad中,必须使用popover的形式展示
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@&警告& message:@&你有严重的精神病,赶紧去治疗& preferredStyle:UIAlertControllerStyleActionSheet];
// 设置popover指向的item
alert.popoverPresentationController.barButtonItem = self.navigationItem.leftBarButtonI
// 添加按钮
[alert addAction:[UIAlertAction actionWithTitle:@&确定& style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
NSLog(@&点击了确定按钮&);
[alert addAction:[UIAlertAction actionWithTitle:@&取消& style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@&点击了取消按钮&);
[self presentViewController:alert animated:YES completion:nil];
&Text fields can only be added to an alert controller of style UIAlertControllerStyleAlert
&只能在UIAlertControllerStyleAlert样式的view上添加文本框
UIAlertView使用
// 危险操作:弹框提醒
// 1.UIAlertView
// 2.UIActionSheet
// iOS8开始:UIAlertController == UIAlertView + UIActionSheet
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@&警告& message:@&你有严重的精神病,赶紧去治疗& preferredStyle:UIAlertControllerStyleAlert];
// 添加按钮
__weak typeof(alert) weakAlert =
[alert addAction:[UIAlertAction actionWithTitle:@&确定& style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
NSLog(@&点击了确定按钮--%@-%@&, [weakAlert.textFields.firstObject text], [weakAlert.textFields.lastObject text]);
[alert addAction:[UIAlertAction actionWithTitle:@&取消& style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@&点击了取消按钮&);
// 添加文本框
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.textColor = [UIColor redColor];
textField.text = @&123&;
[textField addTarget:self action:@selector(usernameDidChange:) forControlEvents:UIControlEventEditingChanged];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(usernameDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.secureTextEntry = YES;
textField.text = @&123&;
[self presentViewController:alert animated:YES completion:nil];
- (void)usernameDidChange:(UITextField *)username
NSLog(@&%@&, username.text);
ios8之前用法
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@&警告:你有严重的精神病,赶紧去治疗& delegate:nil cancelButtonTitle:@&取消& destructiveButtonTitle:@&确定&
otherButtonTitles:@&关闭&, nil];
[sheet showInView:self.view];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@&警告& message:@&你有严重的精神病,赶紧去治疗& delegate:nil cancelButtonTitle:@&取消& otherButtonTitles:@&确定&, nil];
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordI
[alert show];
感谢李明杰老师。
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:29252次
排名:千里之外
原创:55篇
转载:13篇iOS开发之UIAlertView与UIAlertController的详尽用法说明
来源:博客园
本文将从四个方面对IOS开发中UIAlertView与UIAlertController的用法进行讲解:

一、UIAlertView与UIAlertController是什么东东?
二、我们为什么要用UIAlertView或UIAlertController?
三、如何使用UIAlertView和UIAlertController?
四、阅读提醒。

一、UIAlertView与UIAlertController是什么东东?
 一句话,在所有移动应用中,出现的提示窗一般都由UIAlertView或UIAlertController来编写的,两者功能相同。
 
二、我们为什么要用UIAlertView或UIAlertController? 
  好的人机交互,可以提高用户体验,操作系统的人机交互功能是决定计算机系统“友善性”的一个重要因素。人机交互功能主要靠可输入输出的外部设备和相应的软件来完成。在传统时期,可供人机交互使用的设备主要有键盘显示、鼠标、各种模式识别设备等。与这些设备相应的软件就是操作系统提供人机交互功能的部分。早期的人机交互设施主要是键盘和显示器。操作员通过键盘打入命令,操作系统接到命令后立即执行并将结果通过显示器显示。打入的命令可以有不同方式,但每一条命令的解释是清楚的,唯一的。如今,在移动端我们所进行的所有操作均是在可触摸屏幕上进行的,为了更好的进行人机交互,工程师便把操作信息或提示信息显示到屏幕上,用户根据自身判断来决定所需点击的按钮。一句话,就是利用人机交互更好的提升用户的使用体验和产品设计的质量。
 
三、如何使用UIAlertView和UIAlertController?
 1、UIAlertView的使用方法:

// 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.
// 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 = UIAlertViewStylePlainTextI
// Returns the text field at the given index
UITextField *textField = [alertView textFieldAtIndex:0];
textField.text = model.
// 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)buttonI

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

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

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

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

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

 
 2、UIAlertController的使用方法:
  *
使用UIAlertController共需要三步
1.实例化alert:alertControllerWithTitle
2.实例化按钮:actionWithTitle
3.显示alertController:presentViewController

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

// 2.实例化按钮:actionWithTitle
// 为防止block与控制器间循环引用,我们这里需用__weak来预防
__weak typeof(alert) wAlert =
[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.
//监听文字改变的方法
[textField addTarget:self action:@selector(textFieldsValueDidChange:) forControlEvents:UIControlEventEditingChanged];
}];

[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.secureTextEntry = YES;
// 密文形式显示
textField.text = model.
}];

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

UIAlertController的基本使用
 
 
四、阅读提醒
  在Xcode的iOS8 SDK中,UIAlertView和UIAlertController都被UIAlertController取代。官方库解释:"UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead."、"UIActionSheet is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleActionSheet instead."。说明了在iOS8+开发,UIALertView和UIActionSheet已经过时了,UIAlertController以一种模块化替换的方式来代替这两这两个控件的功能和作用。
免责声明:本站部分内容、图片、文字、视频等来自于互联网,仅供大家学习与交流。相关内容如涉嫌侵犯您的知识产权或其他合法权益,请向本站发送有效通知,我们会及时处理。反馈邮箱&&&&。
学生服务号
在线咨询,奖学金返现,名师点评,等你来互动

我要回帖

更多关于 ios 只支持横屏 的文章

 

随机推荐