xcode 导航控制器中的取消按钮而不是后退按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28749848/
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-09-15 06:43:55  来源:igfitidea点击:

Cancel button instead of a back button in Navigation Controller

iosxcodeswiftstoryboarduibarbuttonitem

提问by jilu

I have a general question concerning the navigation between views.

我有一个关于视图之间导航的一般问题。

I have a view controller 1 embeded in a navigation controller. In the nav bar, I have a button to do add data, when pushed, it goes to view controller 2 through segue Show (in Storyboard). In view controller 2, I return to view controller 1 after collecting data when I click the button save in nav bar on the right. I collect data with:

我有一个嵌入在导航控制器中的视图控制器 1。在导航栏中,我有一个按钮来添加数据,按下时,它会通过 segue Show(在 Storyboard 中)转到视图控制器 2。在视图控制器 2 中,当我单击右侧导航栏中的保存按钮时,我在收集数据后返回到视图控制器 1。我收集数据:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {...}

I would prefer instead of a back button to have a cancel button in the view controller 2 to return to view controller 1.

我更喜欢在视图控制器 2 中使用取消按钮而不是后退按钮返回到视图控制器 1。

I feel something is wrong in my design but I dont know what.

我觉得我的设计有问题,但我不知道是什么。

In fact I am looking for something very similar to app clock.

事实上,我正在寻找与应用时钟非常相似的东西。

Do you have any idea?

你有什么主意吗?

Edit: Here is the story board. you will notice the loop beween the two controller. how to do when I click save to not have a back button automatically on the first controller?

编辑:这是故事板。您会注意到两个控制器之间的循环。当我点击保存在第一个控制器上没有自动返回按钮时怎么办?

Storyboard

故事板

回答by Leo Dabus

You just have to select your UIBarButtonItem, click at show Attributes Inspector, click at Identifier and select "Cancel" from the drop-down menu.

您只需要选择您的 UIBarButtonItem,单击显示属性检查器,单击标识符并从下拉菜单中选择“取消”。

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

@IBAction func cancelDownload(sender: AnyObject) {
    dismissViewControllerAnimated(true, completion: nil)

}

回答by Shaan Singh

You can customize the text of the back button by adding this code to the view controller that contains it (in viewDidLoad):

您可以通过将此代码添加到包含它的视图控制器(在 中viewDidLoad)来自定义后退按钮的文本:

let backItem = UIBarButtonItem(title: "Cancel", style: .Bordered, target: nil, action: nil)
navigationItem.backBarButtonItem = backItem