2012年6月25日 星期一

TabbarController為每個UIView 加上各自的UINavigationController

在TabbarController為每個獨立的 UIView加上各自的UINavigatonController有兩個方法:

一、自已以語法的方式建立。


1、在 ExampleAppDelegate.h 檔加上下面的語法
  1. /*
  2. * ExampleAppDelegate.h
  3. */
  4. @interface ExampleAppDelegate : NSObject
  5. {
  6. UIWindow *window;
  7. UITabBarController *mainBar;
  8. UINavigationController *firstNav;
  9. UINavigationController *secondNav;
  10. }
  11. @property (nonatomic, retain) IBOutlet UIWindow *window;
  12. @property (nonatomic, retain) UITabBarController *mainBar;
  13. @property (nonatomic, retain) UINavigationController *firstNav;
  14. @property (nonatomic, retain) UINavigationController *secondNav;
  15. @end

2、在 ExampleAppDelegate.m 寫上下面的語法
  1. /*
  2. * ExampleAppDelegate.m
  3. */
  4. #import "ExampleAppDelegate.h"
  5. #import "FirstView.h"
  6. #import "SecondView.h"
  7.  
  8. @implementation ExampleAppDelegate
  9.  
  10. @synthesize window;
  11. @synthesize mainBar;
  12. @synthesize firstNav;
  13. @synthesize secondNav;
  14.  
  15. - (BOOL)application:(UIApplication *)application
  16. didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  17. {
  18. firstNav = [[UINavigationController alloc] init];
  19. FirstView *firstView = [[FirstView alloc] init];
  20. [firstNav pushViewController:firstView animated:YES];
  21. firstNav.tabBarItem.title = @"First Tab";
  22. [firstView release];
  23.  
  24. secondNav = [[UINavigationController alloc] init];
  25. SecondView *secondView = [[SecondView alloc] init];
  26. [secondNav pushViewController:secondView animated:YES];
  27. secondNav.tabBarItem.title = @"Second Tab";
  28. [secondView release];
  29.  
  30. mainBar = [[UITabBarController alloc] init];
  31. mainBar.viewControllers = [NSArray arrayWithObjects:
  32. firstNav, secondNav, nil];
  33.  
  34. [self.window addSubview:mainBar.view];
  35. [self.window makeKeyAndVisible];
  36. return YES;
  37. }
  1.  
  1. - (void)dealloc
  2. {
  3.    [firstView release];
  4.    [secondView release];
  5.    [mainBar release];
  6.    [window release];
  7.    [super dealloc];
  8. }
  9. 二、以 xib 的方法建立
  10. 可以參考 http://blog.willwinder.com/2011/05/xcode4-uitabbarcontroller-with.html
  11. 特要注意的是要在 view 也拉一個 UINavigationControll,然後把UIView  UINavigationControll全建立個link  ViewController.h檔裡面,然後在 VieDidLoad加上下列的語法:在這邊我把UIView取名為theParentViewOutletUINavigationController取名為 navigationController
  12. 這樣的用意是可以取代原本的navigationController。就不用改太多了。
  13. [self.theParentViewOutlet addSubview:self.navigationController.view];
  14.  
  15. 以上的資料參考下面兩個網址:
  16. 1http://ppkko.blogspot.tw/2011/05/tabbarcontroller-navigationcontroller.html
  17.  
  18. 2http://stackoverflow.com/questions/5163024/uinavigationcontroller-inside-uiview

沒有留言:

張貼留言