ios 弹窗测试提示的小技巧

有的时候我们只是想弹出个提示框来提示,测试之类的东西,一直要去 写 一大段的代码是挺烦的,copy 感觉也不太好 ,那么宏定义就是个不错的选择了.

// 抽成宏之后,只需要一行代码就能完成弹窗提醒
#define alert(string, detail,VC,okTitle) UIAlertController *alertController = [UIAlertController alertControllerWithTitle:string message:detail preferredStyle:UIAlertControllerStyleAlert]; \
UIAlertAction *okAction = [UIAlertAction actionWithTitle:okTitle style:UIAlertActionStyleDefault handler:nil]; \
[alertController addAction:okAction]; \
[VC presentViewController:alertController animated:YES completion:nil];

顺便附上 获取当前视图所在的 ViewController 的代码

//OC
- (UIViewController *)viewController
{
    UIViewController *viewController = nil;
    UIResponder *next = self.nextResponder;
    while (next)
    {
        
        if ([next isKindOfClass:[UIViewController class]])
        {
            viewController = (UIViewController *)next;
            break;
        }
        next = next.nextResponder;
    } 
    return viewController;
}
//swift
 func getViewController() -> UIViewController{
        
        var viewController :UIViewController! ;
        var next = self.next;
        while ((next) != nil)
        {
            
            if (next?.isKind(of: UIViewController.classForCoder()))!
            {
                viewController = next as! UIViewController;
                break;
            }
            next = next!.next;
        } 
        return viewController;
        
    }

你可能感兴趣的:(ios 弹窗测试提示的小技巧)