新视图 pushViewController presentModalViewController 用法

这两个方法都是必须要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就可以了


 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值