一次定义UIAlertViewController并在Objective-C的不同类中使用它

时间:2018-12-21 11:04:53

标签: ios objective-c uialertcontroller

我想知道解决这个问题的方法。

//Modal.h

-(void)errorAlert : (NSString *)title : (NSString *)desc : (NSString *)buttonTitle;


//Modal.m

-(void)errorAlert: (NSString *)title : (NSString *)desc : (NSString *)buttonTitle{

    alert = [UIAlertController alertControllerWithTitle:title message:desc preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *ok = [UIAlertAction actionWithTitle:buttonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

    }];

    [alert addAction:ok];

    [self presentViewController:alert animated:YES completion:nil];

}

现在,我想在其他课程中使用此errorAlert,这样就不必再次为警报编写验证了

//Login.m


#import "Modal.m"

@property (strong, nonatomic) Modal *modal;


-(void)viewDidLoad{

  _modal = [[Modal alloc] init];

}



//MARK: Submit Clicked

- (IBAction)submitClicked:(UIButton *)sender {


    // I want to use that method here.

}

请给我建议一条出路,以便我可以根据格式优化代码。

2 个答案:

答案 0 :(得分:0)

正如评论中提到的那样,这实际上并不是最好的实现。无论如何,要回答您的问题:首先,应该以这种方式固定方法的签名

-(void)errorAlertWithTitle:(NSString *)title description:(NSString *)desc buttonTitle:(NSString *)buttonTitle fromViewController:(UIViewController *)viewController;

及其实现

-(void)errorAlertWithTitle:(NSString *)title description:(NSString *)desc buttonTitle:(NSString *)buttonTitle fromViewController:(UIViewController *)viewController {
    alert = [UIAlertController alertControllerWithTitle:title message:desc preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *ok = [UIAlertAction actionWithTitle:buttonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){}];
    [alert addAction:ok];
    [viewController presentViewController:alert animated:YES completion:nil];
}

然后,您只需要在SubmitClicked中调用它

- (IBAction)submitClicked:(UIButton *)sender {
    [self.modal errorAlertWithTitle:@"The title" description:@"The description" buttonTitle:@"ButtonTitle" fromViewController:self];
}

答案 1 :(得分:0)

目标C:

  
      
  1. 在Alert.h
  2.   
(void)showAlertWithTitle:(NSString *)title 
        message:(NSString* _Nullable)message 
        defaultPrompt:(NSString *)defaultPrompt 
        optionalPrompt:(NSString* _Nullable)optionalPrompt 
        defaultHandler:(nullable void(^)(UIAlertAction *))defaultHandler 
        optionalHandler:(nullable void(^)(UIAlertAction *))optionalHandler;
  
      
  1. 在Alert.m
  2.   
    (void)showAlertWithTitle:(NSString *)title message:(NSString* _Nullable)message defaultPrompt:(NSString *)defaultPrompt optionalPrompt:(NSString* _Nullable)optionalPrompt defaultHandler:(nullable void(^)(UIAlertAction *))defaultHandler optionalHandler:(nullable void(^)(UIAlertAction *))optionalHandler {

    // Executing the method in the main thread, since its involving the UI updates.
    dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];

    [alert addAction:[UIAlertAction actionWithTitle:defaultPrompt style:UIAlertActionStyleDefault handler:defaultHandler]];
    if (optionalPrompt) {
        [alert addAction:[UIAlertAction actionWithTitle:optionalPrompt style:UIAlertActionStyleCancel handler:optionalHandler]];
    }
    [[[UIApplication sharedApplication] keyWindow].rootViewController presentViewController:alert animated:true completion:nil];
}); }

SWIFT 4.2:

只需创建一个名为 Alert.swift 的新swift文件,然后在其中定义AlertController的通用版本。这样,您只需调用Alert.showErrorAlert("", title: "")

就可以在项目中的任何地方使用它
import UIKit
import os.log

class Alert {

    /// Display the given error message as an alert pop-up
    /// - parameter errorMessage: The error description
    /// - parameter title: The title of the error alert
    class func showErrorAlert(_ errorMessage: String, title: String?) {
        let alert = UIAlertController(title: title ?? "Error", message: errorMessage, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        guard let rootView = UIApplication.shared.keyWindow?.rootViewController else {
            os_log("Cannot show alert in the root view", log: .default, type: .error)
            return
        }
        rootView.present(alert, animated: true, completion: nil)
    }


}