IOS以编程方式创建UIAlertViewController [英] IOS create UIAlertViewController programmatically

查看:188
本文介绍了IOS以编程方式创建UIAlertViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带代码的ViewController(没有故事板)。我正在尝试添加和AlertController

I'm working on a ViewController with code (no storyboard). I'm trying to add and AlertController

我已在.m中声明属性

@property (nonatomic, strong) UIAlertController *alertController;

中的init> loadview 方法

//alertviewController
    _alertController = [[UIAlertController alloc]initWithNibName:nil bundle:nil];

并在 viewDidLoad 中调用alertview:

And call the alertview in 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];

我不知道为什么警报没有出现。我的代码出了点问题。如何以编程方式设置和调用 alertViewController

I dont' know why the alert is not showing up. Some thing wrong with my code. How to set up and call alertViewController programmatically?

推荐答案

- (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];
}

这篇关于IOS以编程方式创建UIAlertViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆