美文网首页
收集的工具类-判断字符是否为空 view弹出提示框而不是vc

收集的工具类-判断字符是否为空 view弹出提示框而不是vc

作者: 哆啦_ | 来源:发表于2016-06-17 13:58 被阅读95次

1./**判断输入框输入的内容是否为空 yes 表示为空 no 表示有内容**/

+ (BOOL) isBlankString:(NSString *)string {

if (string == nil || string == NULL) {

return YES;

}

if ([string isKindOfClass:[NSNull class]]) {

return YES;

}

if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {

return YES;

}

return NO;

}

2.因为弹出系统的UIAlertController是需要一个vc来present出来的, 所以有时候为了在一个自定义view弹出一个信息框,搜集了一个小方法

/**

*  弹出信息框 适用于弹出之后停留在当前页面 Class 传入的是什么类型的对象  view 或者Vc

*

*  @param message      展示的信息

*  @param Class 传入的是什么类型  view 或者Vc

*/

+ (void)showMessage:(NSString *)message Class:(id)class

{

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];

[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];

if ([class isKindOfClass:[UIViewController class]]) {

UIViewController *vc = (UIViewController *)class;

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

}else if ([class isKindOfClass:[UIView class]]){

UIView *view = (UIView *)class;

UIViewController *vc = [self getCurrentViewControllerWithView:view];

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

}

}

/**

*  弹出信息框 适用于弹出之后回到上一个页面

*

*  @param message 信息

*/

+ (void)showMessage:(NSString *)message

{

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];

[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];

//得到当前的控制器

UIViewController *vc = [self getCurrentVC];

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

}

/**

*  得到view所在的控制器

*

*  @param currentView 当前的view

*

*  @return 返回view所在的控制器

*/

+ (UIViewController *)getCurrentViewControllerWithView:(UIView *)currentView

{

for (UIView *view = currentView; view; view = view.superview)

{

UIResponder *nextResponder = [view nextResponder];

if ([nextResponder isKindOfClass:[UIViewController class]])

{

return (UIViewController *)nextResponder;

}

}

return nil;

}

/**

*  得到当前的控制器

*/

+ (UIViewController *)getCurrentVC

{

UIViewController *result = nil;

UIWindow * window = [[UIApplication sharedApplication] keyWindow];

if (window.windowLevel != UIWindowLevelNormal)

{

NSArray *windows = [[UIApplication sharedApplication] windows];

for(UIWindow * tmpWin in windows)

{

if (tmpWin.windowLevel == UIWindowLevelNormal)

{

window = tmpWin;

break;

}

}

}

UIView *frontView = [[window subviews] objectAtIndex:0];

id nextResponder = [frontView nextResponder];

if ([nextResponder isKindOfClass:[UIViewController class]])

result = nextResponder;

else

result = window.rootViewController;

return result;

}

相关文章

网友评论

      本文标题:收集的工具类-判断字符是否为空 view弹出提示框而不是vc

      本文链接:https://www.haomeiwen.com/subject/ysdvdttx.html