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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:28:24  来源:igfitidea点击:

How to create Navigation bar with back button on it?

iosxcode4uinavigationbarxcode4.3uinavigationitem

提问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

我现在得到了什么

While Drag and Drop the Navigation Bar

拖放导航栏时

Looking for

寻找

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 UIButtonor 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];