收集的工具類-判斷字符是否為空 view彈出提示框而不是vc

1./**判斷輸入框輸入的內(nèi)容是否為空 yes 表示為空 no 表示有內(nèi)容**/

+ (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.因?yàn)閺棾鱿到y(tǒng)的UIAlertController是需要一個(gè)vc來(lái)present出來(lái)的, 所以有時(shí)候?yàn)榱嗽谝粋€(gè)自定義view彈出一個(gè)信息框,搜集了一個(gè)小方法

/**

*? 彈出信息框 適用于彈出之后停留在當(dāng)前頁(yè)面 Class 傳入的是什么類型的對(duì)象? 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];

}

}

/**

*? 彈出信息框 適用于彈出之后回到上一個(gè)頁(yè)面

*

*? @param message 信息

*/

+ (void)showMessage:(NSString *)message

{

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

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

//得到當(dāng)前的控制器

UIViewController *vc = [self getCurrentVC];

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

}

/**

*? 得到view所在的控制器

*

*? @param currentView 當(dāng)前的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;

}

/**

*? 得到當(dāng)前的控制器

*/

+ (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;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、禁止手機(jī)睡眠[UIApplication sharedApplication].idleTimerDisabl...
    DingGa閱讀 1,211評(píng)論 1 6
  • *7月8日上午 N:Block :跟一個(gè)函數(shù)塊差不多,會(huì)對(duì)里面所有的內(nèi)容的引用計(jì)數(shù)+1,想要解決就用__block...
    炙冰閱讀 2,734評(píng)論 1 14
  • 自從iOS9出來(lái)之后,需要使用UIAlertController來(lái)彈出彈框,不在提倡使用UIAlertView了,...
    南京楊小兵閱讀 634評(píng)論 1 0
  • 詩(shī)人以為 青春是一首美麗的詩(shī) 屠夫以為 青春就是一把殺豬刀 上班族以為 青春就是工資 農(nóng)民工以為 青春就是鋼筋水泥...
    我愛領(lǐng)導(dǎo)的小生閱讀 290評(píng)論 3 3
  • 我想我是快樂的,因?yàn)槲翌^腦簡(jiǎn)單,四肢發(fā)達(dá),很少想事情,簡(jiǎn)單執(zhí)行的工作自己應(yīng)該還是相當(dāng)不錯(cuò)的呢。一直以來(lái),我都希望自...
    今天安好閱讀 371評(píng)論 0 0

友情鏈接更多精彩內(nèi)容