IOS create UIAlertViewController programmatically

前端 未结 4 715
时光取名叫无心
时光取名叫无心 2020-12-29 04:54

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

I have declare propery in .m

@property (nonatomic,         


        
4条回答
  •  天命终不由人
    2020-12-29 05:15

    In 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];
    

    How would I create a UIAlertView in Swift?

提交回复
热议问题