標籤:
給單獨的viewcontroller或者在Appdelegate的首頁面添加導航條,只要在viewcontroller上添加navigationcontroller,在添加此navigationcontroller即可
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
ViewController *mainView = [[ViewController alloc]init];
UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:mainView];
navi.navigationBar.backgroundColor = [UIColor blueColor];
[self.window setRootViewController:navi];
[self.window makeKeyAndVisible];
return YES;
}
導航條的字型和顏色的設定
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor]; // --- 字型顏色
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"BJ.png"] forBarMetrics:UIBarMetricsDefault]; // — 背景色
導航條跳轉頁面的考慮對於用navigationcontroller來跳轉頁面的時候,其實是執行堆棧的進棧和出棧的操作,要想釋放記憶體,那麼在來回跳轉的時候,就要考慮幾個問題了
1 A =>B=>C=>D,
D=>A 有根視圖的話 (HOME)
[self.navigationController popToRootViewControllerAnimated:YES];
D=>C (每一個介面返回上一層)
[self.navigationController popViewControllerAnimated:YES];
返回到上一層,並且傳遞參數
CViewController *cvc = [CViewController alloc]init];
cvc.str = self.str;
[self.navigationController popToViewController:cvc animated:true];
返回到上一層後,上一頁面顯示後要接收參數,並重新整理。注意此時應該在viewDidAppear中進行判斷並接收傳遞的值
-(void)viewDidAppear:(BOOL)animated
{
//判斷並接收返回的參數
}
2 A =>B=>C=>D=>E,E=>B=>C=>E因為B在之前已經出現過,不能在E中直接PUSH到B,因為那樣已經是兩個B了,增加記憶體,所以在跳轉的時候,就要進行判斷是否之前已經出現過B了,出現過,則直接push。這樣push到的是原有的B,不會在記憶體中重建一個B了。
NSArray *array = self.navigationController.viewControllers;
for (UIViewController *vc in array) {
if ([vc isKindOfClass:[BXXXViewController class]]) {
push VC;
}
或者知道每個介面的指標
[self.navigationController
popToViewController: [self.navigationController.viewControllers
objectAtIndex: ([self.navigationController.viewControllers count] -4)]
animated:YES];
在使用時,根據自己返回層的需要,只要改變一下“-4”這個數字就可以達到目的了
iOS viewController添加導航條以及返回跳轉選擇