如果我們有一個介面有很多請求,但這時候所有的請求都無效,這是我們可能會提示使用者 “請求失敗”,“請求逾時“等等,如果提示一次那當然很好,但是每一個失敗的請求都觸發一次提示框來提示使用者,這樣會很糟糕。可能一次彈出很多提示框,我們點擊一次然後又彈出另一個,並且這些提示資訊還是一樣的,這樣會讓使用者很惱火的。 假設某家公司有兩個不同的資料庫A 和B,但是A是內網的,B是現網的,我們在A中註冊下,然後把資訊儲存,這時候我們修改地址指向B,可能會出現問題。如果我們訪問了需要提供access token 的資源,我們需要提供access token ,但是這個access token 在A中是有效,在B中是無效的。所以服務端可能會返回 “無效的access token”等。可是我們已經登陸了啊,咋辦呢?我們可以根據具體的情況,重新彈出登陸介面,當然這隻是一個例子,我們可以用這樣的方法處理其他類似的情況。 1 用dispatch once這個不說了2 用工廠類方法#import <UIKit/UIKit.h>@interface PresentLogInVC : UIViewController+(void)appearLogInVC;@end #import "PresentLogInVC.h" static UINavigationController *_navController;static PresentLogInVC *_viewController;@interface PresentLogInVC (){ }@end @implementation PresentLogInVC+(void)appearLogInVC{ if (!_navController) { _viewController=[[self alloc]init]; _navController=[[UINavigationController alloc]initWithRootViewController:_viewController]; [_viewController show]; } else { NSLog(@"have appeared !!!"); }} -(void)show{ UIWindow *window=[[[UIApplication sharedApplication] windows] objectAtIndex:0]; [window addSubview: _navController.view]; __block CGRect r=[[UIScreen mainScreen]bounds]; r.origin.y=r.size.height; [_navController.view setFrame:r]; [UIView animateWithDuration:0.3 animations:^{ r.origin.y=0; www.2cto.com [_navController.view setFrame:r]; }];}-(void)dismiss{ [UIView animateWithDuration:0.3 animations:^{ CGRect r=[[UIScreen mainScreen]bounds]; r.origin.y=r.size.height; [_navController.view setFrame:r]; } completion:^(BOOL b){ [_navController.view removeFromSuperview]; }];} -(void)viewDidLoad{ [super viewDidLoad]; self.title=@"登陸"; UIBarButtonItem * leftItem = [[UIBarButtonItem alloc] initWithTitle:@"dismiss" style:UIBarButtonItemStyleDone target:self action:@selector(dismiss)]; [self.navigationItem setLeftBarButtonItem:leftItem]; self.view.backgroundColor=[UIColor grayColor];}-(void)viewDidDisappear:(BOOL)animated{ [super viewDidDisappear:animated]; [_navController release]; _navController=nil; [_viewController release]; _viewController =nil;}@end