IOS以编程方式创建UIAlertViewController

时间:2016-08-01 15:07:55

标签: ios objective-c uialertview

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

我已经在.m

宣布了
@property (nonatomic, strong) UIAlertController *alertController;

并在loadview方法

中初始化
//alertviewController
    _alertController = [[UIAlertController alloc]initWithNibName:nil bundle:nil];

并在viewDidLoad中调用alertview:

_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

4 个答案:

答案 0 :(得分:49)

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

答案 1 :(得分:2)

并且在 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)

答案 2 :(得分:1)

在Xcode 9.4.1中

全局创建警报功能并使用每种商品。

//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];
    });

}

在您需要的位置拨打此电话。

导入该类并创建实例

//Ex:
GlobalClassViewController *gcvc = [[GlobalClassViewController alloc]init];

//Call alert function with instance
[gcvc showAlertMsg:self title:@"" message:placeholderText];

How would I create a UIAlertView in Swift?

答案 3 :(得分:0)

使用扩展名创建可在所有视图控制器中访问的全局警报控制器。
使用您的函数创建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)


      }
}


现在从视图控制器调用此函数:

class TestViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.displayalert(title: <String>, message: <String>)
    }
}