这两个方法都是必须要ViewController, 是建立在UINavigationController之上的。
SQLiteViewController *baSQLiteViewController = [[SQLiteViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:baSQLiteViewController];
[self.window addSubview:nav.view];
推出视图push,下一页面会自动添加返回按钮
InforViewController *baInforViewController = [[InforViewController alloc] init];
[self.navigationController pushViewController:baInforViewController animated:YES];
以Modal方式推出一个视图,弹出的视图没有返回按钮,需要自己添加
NewDataViewController *baNewViewController = [[NewDataViewController alloc] init];
UINavigationController *baNavigation = [[UINavigationController alloc] initWithRootViewController:baNewViewController];
[self.navigationController presentModalViewController:baNavigation animated:YES];
[baNewViewController release];
[baNavigation release];
为弹出视图添加返回按钮
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(returnView)];
返回方法
- (void)returnView
{
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
总结:
1 将需要present(需要弹出来的)的ViewController先添加加到一个新的UINavigationController的RootView中,例如
NewDataViewController *baNewViewController = [[NewDataViewController alloc] init]; //需要弹出的ViewController
UINavigationController *baNavigation = [[UINavigationController alloc] initWithRootViewController:baNewViewController];//实例化一个NavigationController对象,这个对象使用将要弹出ViewController作为RootView Controlller初始化。
2. 然后present这个UINavigationController,接着就可以继续push了,当返回到present还可以缩回去
右侧进入
BaseInfoEdit *View = [[BaseInfoEdit alloc] initWithNibName:@"BaseInfo" bundle:nil];
[self.navigationController pushViewController:View animated:YES];
返回方法
[self.navigationController popViewControllerAnimated:YES];
下方进入
OilRecordAdd *View = [[OilRecordAdd alloc] initWithNibName:@"OilRecordDetail" bundle:nil];
[self.navigationController presentModalViewController:Nav animated:YES];
返回方法
[self.navigationController dismissModalViewControllerAnimated:YES];
leftBarButtonItem和rightBarButtonItem设置的是本级页面上的BarButtonItem,
backBarButtonItem设置的是下一级页面上的BarButtonItem
比如:两个ViewController,主A和子B,我们想在A上显示“刷新”的右BarButton,B上的BackButton显示为“撤退”
就应该在A的viewDidLoad类似方法中写:
UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithTitle:@"刷新" style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationItem.rightBarButtonItem = refreshButton;
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"撤退" style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationItem.backBarButtonItem = cancelButton;
B不需要做任何处理
然后A push B就可以了