iOS AlertController的简单封装

import <UIKit/UIKit.h>

@interface LoginViewController : UIViewController

@end

+(UIAlertController *)showWithInfo:(NSString *)title and:(NSString *)message;

import "YDAlert.h"

@implementation YDAlert

+(UIAlertController *)showWithInfo:(NSString *)title and:(NSString *)message{

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

UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];

[alert addAction:actionOk];

[[YDAlert currentViewController] presentViewController:alert animated:NO completion:^{
    
}];

return alert;

}

  • (UIViewController *)currentViewController {
    UIWindow *window = [[UIApplication sharedApplication].delegate window];
    UIViewController *presentedVC = [[window rootViewController] presentedViewController];
    if (presentedVC) {
    return presentedVC;

    } else {
    return window.rootViewController;
    }
    }

一句代码调用
[YDAlert showWithInfo:@"账号和密码不能为空" and:@"请输入账号密码"];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 2,772评论 1 14
  • 1、禁止手机睡眠[UIApplication sharedApplication].idleTimerDisabl...
    DingGa阅读 1,226评论 1 6
  • 1、禁止手机睡眠 [UIApplicationsharedApplication].idleTimerDisabl...
    小热狗阅读 1,009评论 0 2
  • 要过多少个夜晚,才会理智对待夜晚的空虚? 要跌过多少跟头,才能从容接受情感的得失? 要经历多少得失,终于不那么尖锐...
    猴有钱阅读 284评论 0 0
  • 8再过两天就是20周岁的生日了。我想,不如就写点什么作为这20周年的一个纪念吧。 如果说每个人有一百岁的寿命,那么...
    怪兽小馒头阅读 655评论 0 0

友情链接更多精彩内容