Skip to content

Instantly share code, notes, and snippets.

Created October 23, 2015 23:56
Show Gist options
  • Save anonymous/12436da81ff0be6085de to your computer and use it in GitHub Desktop.
Save anonymous/12436da81ff0be6085de to your computer and use it in GitHub Desktop.

Objective-C category

開一組新的檔案 UIViewController+Alert.hUIViewController+Alert.m 分別放以下內容:

UIViewController+Alert.h

#import <UIKit/UIKit.h>

@interface UIViewController (Alert)

- (void)presentAlertWithTitle:(NSString *)title 
                      message:(NSString *)message;

@end

UIViewController+Alert.m

#import "UIViewController+Alert.h"

@implementation UIViewController (Alert)

- (void)presentAlertWithTitle:(NSString *)title
                      message:(NSString *)message
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title
                                                                             message:message
                                                                      preferredStyle:UIAlertControllerStyleAlert];
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"OK"
                                                        style:UIAlertActionStyleCancel
                                                      handler:nil]];
    
    [self presentViewController:alertController
                       animated:YES
                     completion:nil];
}

@end

在別的 ViewController 就可以用以下方法呼叫:

#import "UIViewController+Alert.h"

@implementation MyViewController

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self presentAlertWithTitle:@"Oops!"
                        message:@"This app is about to crash!"];
}

@end

如果很常用的話可以考慮在 precompiled prefix header 裡面 import UIViewController+Alert.h

Swift extension

開一個新檔案:UIViewController+Alert.swift 放以下內容:

extension UIViewController {
    func presentAlertWith(title title: String!, message: String!) {
        let alertController = UIAlertController(
            title: title,
            message: message,
            preferredStyle: .Alert
        )
        
        alertController.addAction(
            UIAlertAction(
                title: "OK",
                style: .Cancel,
                handler: nil
            )
        )
        
        self.presentViewController(
        	alertController, 
        	animated: true, 
        	completion: nil
        )
    }
}

在別的 ViewController 就可用以下方法呼叫:

class MyViewController: UIViewController {
	...
	override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        self.presentAlertWith(
        	title: "Oops!", 
        	message: "This app is about to crash!"
        )
    }
	...
}
@cyhsutw
Copy link

cyhsutw commented Oct 30, 2015

可以吃 callback 的宣告範例

Objective-C

- (void)presentAlertWithTitle:(NSString *)title
                      message:(NSString *)message
                      handler:(void(^)(UIAlertAction *action))actionHandler
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title
                                                                             message:message
                                                                      preferredStyle:UIAlertControllerStyleAlert];

    [alertController addAction:[UIAlertAction actionWithTitle:@"OK"
                                                        style:UIAlertActionStyleCancel
                                                      handler:actionHandler]];

    [self presentViewController:alertController
                       animated:YES
                     completion:nil];
}

Swift

func presentAlertWith(title title: String!, message: String!, actionHandler: ((UIAlertAction) -> Void)?) {
        let alertController = UIAlertController(
            title: title,
            message: message,
            preferredStyle: .Alert
        )

        alertController.addAction(
            UIAlertAction(
                title: "OK",
                style: .Cancel,
                handler: actionHandler
            )
        )

        self.presentViewController(
            alertController,
            animated: true,
            completion: nil
        )
}

使用方法

Objective-C

使用方法:

[self presentAlertWithTitle:@"Oops!"
                        message:@"Something went wrong!"
                        handler:^(UIAlertAction *action) {
                            // do something
                        }];

Swift

self.presentAlertWith(title: "Oops!", message: "Something went wrong!") {
            (action) -> Void in
            // do something
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment