獨立開發項目中,由于開發經驗有限,項目的結構有些混亂,遇到很多坑,自己一個個的去解決,但是我感覺解決的方式方法肯定不是很好,主要身邊沒有iOS的前輩能夠探討,導致自己進步感覺有點慢。。。網絡上雖然也能交流,不過我好像工作之余,看帖和文檔是我主要學習方式,所以,提出的觀點可能存在一些問題,或者并不是最簡單或者最合理的方式,在此誠懇的希望同學們,前輩們能夠多多指點。
一、此文章的產生原因
1、項目登陸注冊界面與內容界面的UINavigationController最開始差別很大(顏色,樣式)。
2、首次登陸需要登陸或者注冊才能進入內容界面,一周內打開app則不需要登陸直接進入內容界面。
3、在內容界面里要可以退出到登陸注冊界面
so ----登陸注冊部分是一個單獨的UINavigationController
----內容界面是一個獨立的UINavigationController管理內容的所有界面
二、看圖說話
在didFinishLaunching中寫如下偽代碼,是否第一次登陸根據具體情況看官自行添加,ViewController是登陸注冊界面,TwoViewController是內容界面
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
if (第一次登陸) {
ViewController *vc=[[ViewController alloc]init];
self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
_nv=[[UINavigationController alloc]initWithRootViewController:vc];
self.window.backgroundColor=[UIColor whiteColor];
self.window.rootViewController=_nv;
[self.window makeKeyAndVisible];
return YES;
}
else if (6天內登陸) {
TwoViewController *vc=[[TwoViewController alloc]init];
self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
_nv=[[UINavigationController alloc]initWithRootViewController:vc];
self.window.backgroundColor=[UIColor whiteColor];
self.window.rootViewController=_nv;
[self.window makeKeyAndVisible];
return YES;
}
這兩種情況分別代表著self.window.rootViewController為上面提到的兩個獨立的UINavigationController
從登陸注冊界面,跳轉到內容界面,此處只能用presentViewController 來推出一個UINavigationController ,用push來推出一個UINavigationController會爆炸,下面兩個二選一,貌似都可以,目前還不明白有什么區別
[self presentViewController:newVc animated:YES completion:nil];
[self.navigationController presentViewController:newVc animated:YES completion:nil];
現在,以及通過登陸注冊界面進入了內容界面,接下來就是在合適的地方要能退回到登陸注冊界面進行換賬號等操作
if (self.navigationController.presentingViewController) {
NSLog(@"上一級存在");
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
else{
NSLog(@"上一級不存在");
UIWindow *window = [[[UIApplication sharedApplication] delegate] window]; // 獲得根窗口
ViewController *vc=[[ViewController alloc]init];
UINavigationController *nv=[[UINavigationController alloc]initWithRootViewController:vc];
window.rootViewController=nv;
CATransition *myTransition=[CATransition animation];//創建CATransition
myTransition.duration=0.35;//持續時長0.35秒
myTransition.timingFunction=UIViewAnimationCurveEaseInOut;//計時函數,從頭到尾的流暢度
myTransition.type = kCATransitionReveal;//子類型
[window.layer addAnimation:myTransition forKey:nil];
}
判斷內容界面的navigationController管理器是被上一級present出來的。
- 如果是,那就反過來dismiss掉就退回到登陸注冊界面,內容界面也得到釋放
- 如果不是,那dismiss就沒有用了,這時則直接更改window的根視圖為需要跳轉的navigationController,為了看上去更平滑,加上動畫效果CATransition,動畫效果選擇不同的myTransition.type
到此告一段落,僅當拋磚引玉