ios 如何创建带有后退按钮的导航栏?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11688411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create Navigation bar with back button on it?
提问by vinothp
I am very new to iOS App Development.
我对iOS 应用程序开发很陌生。
In my app I am trying to navigate from second view to first view using navigation bar back button, but while drag and drop the navigation bar in the view I am just able to get the bar with title.
在我的应用程序中,我尝试使用导航栏后退按钮从第二个视图导航到第一个视图,但是在视图中拖放导航栏时,我只能获得带有标题的栏。
I am really worried and I am not sure what I am doing wrong? I am using Xcode 4.3.and storyboard to design the view.
我真的很担心,我不确定我做错了什么?我正在使用Xcode 4.3。和故事板来设计视图。
What i am getting now
我现在得到了什么
Looking for
寻找
Thanks for your help guys
谢谢你们的帮助
回答by Ryan
Xcode handles this for you.
Xcode 会为您处理这个问题。
Highlight your first controller. Go to the menu bar and click Embed In > Navigation Controller. Now using UIButton
or something, control-click and drag from your first view controller's button to your second view controller and make it a Push segue. The back button won't appear, but if you run it, it will be there.
突出显示您的第一个控制器。转到菜单栏,然后单击嵌入 > 导航控制器。现在使用UIButton
或其他东西,按住 Control 单击并从第一个视图控制器的按钮拖动到第二个视图控制器并使其成为Push segue。后退按钮不会出现,但如果你运行它,它就会出现。
So you should have 3 views in storyboard. The Navigation Controller, your first view controller, and your second view controller.
所以你应该在故事板中有 3 个视图。导航控制器、第一个视图控制器和第二个视图控制器。
Hereis one way to do it using code.
回答by cloosen
if you want to get a plainButton in the navigation like the iphone`s backButton (only one view), you must add image, i think there is no other way.Of course , if you have 2 or more viewcontrollers , you will use navigationItem.backBarButtonItem without add image .But the first viewcontroller do not have the backButton.
如果你想在导航中得到一个像 iphone 的 backButton(只有一个视图)一样的普通按钮,你必须添加图像,我认为没有其他方法。当然,如果你有 2 个或更多的视图控制器,你将使用 navigationItem .backBarButtonItem 没有添加图像。但是第一个viewcontroller 没有backButton。
the code like this:
像这样的代码:
navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UINavigationItem *navigationItem = [[[UINavigationItem alloc] initWithTitle:@"Detail"] autorelease];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 30)];
[button setImage:[UIImage imageNamed:@"plain.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc]
initWithCustomView:button];
navigationItem.leftBarButtonItem = buttonItem;
[buttonItem release];
[button release];
[navigationBar pushNavigationItem:navigationItem animated:NO];
[self.view addSubview:navigationBar];