向基于视图的应用程序添加导航效果?
我创建了基于视图的应用程序,在这里我需要在按下按钮时在视图之间导航。 因此,在第一个视图控制器中,我为按下的按钮创建了操作。
-(IBAction)loadSecondView:(id)sender
{
SecondView *sView = [[SecondView alloc]initWithNibName:@"SecondView" bundle:nil];
[self.navigationController pushViewController:sView animated:YES];
[sView release];
}
这段代码不起作用,我缺少任何东西,
我可以通过 [self.view addSubview:sView]; 来做到这一点,但我需要导航效果。 提前致谢。
I have created View based application, here i need to navigate between views when button pressed.
so in first view controller i have created action for button pressed.
-(IBAction)loadSecondView:(id)sender
{
SecondView *sView = [[SecondView alloc]initWithNibName:@"SecondView" bundle:nil];
[self.navigationController pushViewController:sView animated:YES];
[sView release];
}
this code is not working, anything i am missing,
i can do this by [self.view addSubview:sView];
but i need navigation effect.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能只是将
UIView
实例挂接到导航控制器,这不是它们的工作方式。查看 Xcode 中的“基于导航的应用程序”模板,了解导航控制器的工作原理。
您可以在隐藏导航栏时使用视图控制器:
然后您可以将 UIButton 实例映射到推送或弹出视图控制器的选择器,同时保持导航栏隐藏。
这些按钮实例是视图控制器的
view
属性的子视图。隐藏导航栏可以帮助您产生一种您没有使用导航控制器的错觉,同时为您提供导航控制器的所有功能。
You can't just hook a
UIView
instance to a navigation controller, that's not how they work.Take a look at the "Navigation-based Application" template in Xcode, to learn how navigation controllers work.
You can use view controllers while hiding the navigation bar:
You can then map
UIButton
instances to selectors that push or pop view controllers, while keeping the navigation bar hidden.These button instances are subviews of the view controller's
view
property.Hiding the navigation bar can help provide the illusion that you are not using a navigation controller, while giving you all the functionality of the navigation controller.
Alex 是对的,如果您只创建一个“基于视图的应用程序”项目,则不会创建 UINavigationController,因此当您向其推送某些内容时不会发生任何情况,这是正常的。
您必须创建一个 UINavigationController 并使您主视图其 rootViewController,然后您可以在其上推送一个新的 viewController。
Alex is right, if you create just a "View Based Application" project, no UINavigationController was created so when you push something on it nothing happen, that's normal.
You have to create a UINavigationController and make you main view its rootViewController, then you can push on it a new viewController.
我对此有一个解决方案,
在基于视图的应用程序中,appdelegate 文件为视图控制器创建对象并将该视图添加到主窗口,为了完成我们的任务,删除 mainwindow.xib 中的控制器并添加一个 UINavigation 控制器,然后创建一个对象到它,并将插座连接到它,然后将此导航控制器视图添加为子视图,
其工作正常。
I have got a solution for this,
In View based application the appdelegate file creates object for, view controller and added that view to main window, to do our task, delete the controller in mainwindow.xib and add a UINavigation controller,and create a object to it, and connect outlet to it,and then add this navigation controller view as a sub view,
its work fine.