IOS 以编程方式创建 UIAlertViewController
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38702269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IOS create UIAlertViewController programmatically
提问by Lê Khánh Vinh
I'm working on a ViewController with code (no storyboard). I'm trying to add and AlertController
我正在使用代码(没有故事板)处理 ViewController。我正在尝试添加和 AlertController
I have declare propery in .m
我在 .m 中声明了财产
@property (nonatomic, strong) UIAlertController *alertController;
And init in loadview
method
并在loadview
方法中初始化
//alertviewController
_alertController = [[UIAlertController alloc]initWithNibName:nil bundle:nil];
And call the alertview in viewDidLoad
:
并调用警报视图viewDidLoad
:
_alertController = [UIAlertController alertControllerWithTitle:@"Error display content" message:@"Error connecting to server, no local database" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
LandingPageViewController *viewController = [[LandingPageViewController alloc] initWithNibName:nil bundle:nil];
// viewController.showNavBarBackButton = YES;
[[AppDelegate sharedAppDelegate].rootViewController cPushViewController:viewController];
}];
[_alertController addAction:ok];
[self presentViewController:_alertController animated:YES completion:nil];
I dont' know why the alert is not showing up. Some thing wrong with my code. How to set up and call alertViewController
programmatically?
我不知道为什么警报没有出现。我的代码有问题。如何以alertViewController
编程方式设置和调用?
回答by ChenSmile
- (void)logoutButtonPressed
{
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:@"Logout"
message:@"Are You Sure Want to Logout!"
preferredStyle:UIAlertControllerStyleAlert];
//Add Buttons
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:@"Yes"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//Handle your yes please button action here
[self clearAllData];
}];
UIAlertAction* noButton = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//Handle no, thanks button
}];
//Add your buttons to alert controller
[alert addAction:yesButton];
[alert addAction:noButton];
[self presentViewController:alert animated:YES completion:nil];
}
回答by iOS
In Xcode 9.4.1
在 Xcode 9.4.1 中
Create alert function globally and use every where.
全局创建警报功能并在任何地方使用。
//Alert function
- (void) showAlertMsg:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message {
UIAlertController * alert = [UIAlertController alertControllerWithTitle : title
message : message
preferredStyle : UIAlertControllerStyleAlert];
UIAlertAction * ok = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{ }];
[alert addAction:ok];
dispatch_async(dispatch_get_main_queue(), ^{
[viewController presentViewController:alert animated:YES completion:nil];
});
}
Call like this in your required place.
在您需要的地方这样呼叫。
Import that class and create instance
导入该类并创建实例
//Ex:
GlobalClassViewController *gcvc = [[GlobalClassViewController alloc]init];
//Call alert function with instance
[gcvc showAlertMsg:self title:@"" message:placeholderText];
回答by Ethan Parker
And in Swift >=3:
在 Swift >=3 中:
let alertController = UIAlertController(title: "Some Error",
message: "Pleas confirm?",
preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(defaultAction)
self?.present(alertController, animated: true, completion: nil)
回答by Krunal
Create global Alert controller that is accessible in all view controllers using extension.
Create an extension of UIViewController
with your function to display alert with required parameter arguments.
使用扩展创建可在所有视图控制器中访问的全局警报控制器。
使用您的函数
创建扩展UIViewController
以显示带有必需参数参数的警报。
extension UIViewController {
func displayalert(title:String, message:String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction((UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
alert.dismiss(animated: true, completion: nil)
})))
self.present(alert, animated: true, completion: nil)
}
}
Now call this function from your view controller:
现在从你的视图控制器调用这个函数:
class TestViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.displayalert(title: <String>, message: <String>)
}
}