http://www.cnblogs.com/dotey/archive/2011/06/09/2075954.html 第二个iPhone应用程序:“Say Hello”
http://edenhe.me/2011/04/28/uiwindow-uiview%E5%92%8Cuiviewcontroller/ UIWindow, UIView和UIViewController
http://www.cnblogs.com/ternastone/archive/2011/11/10/2244460.html IOS开发笔记(七)---对iOS多视图开发的补充(针对XCode4.2开发环境,
http://www.cocoachina.com/downloads/video/2010/1101/2272.html TabBarController与NavigationController嵌套的例子
google tabbarcontroller navigationcontroller
NSMutableArray *controllers = [NSMutableArray array];
for (int i = 0; i < count; i++)
{
TableViewController * tabViewController = [[TableViewController alloc]initController:[item objectAtIndex:i]];//1
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:tabViewController];//2
[controllers addObject:nav];//3
}
UITabBarController *bar = [[UITabBarController alloc] init];
bar.viewControllers = controllers;//4
bar.customizableViewControllers = controllers;
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];//1
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];//2
self.window.rootViewController = self.tabBarController;//3
1.通过navigationController来控制viewController
navigationController就像一个栈,可以放进很多viewController(pushViewController),显示的是栈顶上的viewController。
在appdelegate.h中添加:
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
UINavigationController * navigationcontroller;
}
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UINavigationController *navigationcontroller;
注意:UINavigationController一定要定义,如果直接在方法中这么写:
UINavigationController *navigationcontroller=[[UINavigationController alloc] initWithRootViewController:firstviewcontroller];//不要这样写
程序会崩溃。
在appdelegate.m中添加
firstViewController * firstviewcontroller=[[firstViewController alloc] initWithNibName:@"firstViewController" bundle:nil];
navigationcontroller=[[UINavigationController alloc] initWithRootViewController:firstviewcontroller];
[self.window addSubview:navigationcontroller.view];
修改viewController.h
- (void)viewDidLoad
{
[super viewDidLoad];
self.title=@"firstViewController";
UIBarButtonItem * weiboItem=[[UIBarButtonItem alloc] initWithTitle:@"newWeibo" style:UIBarButtonItemStyleDone target:self action:@selector(toNewWeibo)];
self.navigationItem.leftBarButtonItem=weiboItem;
button=[UIButton buttonWithType:1];
button.frame=CGRectMake(100, 100, 100, 100);
[button setTitle:@"toSecond" forState:UIControlStateNormal];
[button addTarget:self action:@selector(tosecond) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
切换到下一个viewController
-(void)tosecond
{
UIBarButtonItem * backItem=[[UIBarButtonItem alloc] initWithTitle:@"first" style:UIBarButtonItemStyleDone target:nil action:nil];//backButton不写也可以,默认就有
self.navigationItem.backBarButtonItem=backItem;
secondViewController *secondnavigationcontroller=[[secondViewController alloc] initWithNibName:@"secondViewController" bundle:nil];
AppDelegate * appdelegate=[UIApplication sharedApplication].delegate;
[appdelegate.navigationcontroller pushViewController:secondnavigationcontroller animated:YES];//push新的ViewController进栈并显示出来
}
转帖:http://www.cnblogs.com/phoenix13suns/archive/2012/02/06/2340420.html