可以在Swift中实现不使用导航控制器的退出过渡吗?

huangapple go评论75阅读模式
英文:

Is it possible to implement an exit segue without a Navigation Controller in Swift?

问题

I'm new to Swift and working on a portfolio app. I've read that it's possible to add a button with an exit segue to go back a screen using a Navigation Controller. However, I'd like to implement this feature without using a Navigation Controller.

我是新手使用Swift,正在开发一个作品集应用。我了解到可以使用导航控制器添加一个带有退出转场的按钮来返回上一屏幕。但是,我想在不使用导航控制器的情况下实现这个功能。

I've tried creating a button with an exit segue and connecting it to an IBAction, but the button remains inactive when clicked. All the tutorials I've found use a Navigation Controller to implement this feature, so I'm not sure if it's possible to do it without one.

我尝试创建一个带有退出转场的按钮,并将其连接到一个IBAction,但是当单击按钮时,按钮仍然无法点击。我找到的所有教程都使用导航控制器来实现此功能,所以我不确定是否可以在没有导航控制器的情况下实现它。

My question is: is it possible to add a button with an exit segue and have it work as expected without using a Navigation Controller? If so, what steps do I need to follow to make it work?

我的问题是:是否可以添加一个带有退出转场的按钮,并使其在不使用导航控制器的情况下按预期工作?如果可以,我需要按照哪些步骤来实现它?

Thanks for any help you can provide!

感谢您提供的任何帮助!

Here is the code I have in the SecondScreen Swift file:

以下是我在SecondScreen Swift文件中的代码:

import UIKit

class SecondScreen: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
    @IBAction func goBack(segue: UIStoryboardSegue) {
        // This method is called when the exit segue is performed.
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "goBack" {
            // This is where you would prepare any data to be passed back to the previous view controller.
        }
    }

}

The first screen has the following code:

第一个屏幕的代码如下:

import UIKit

class FirstScreen: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

}

When I have all this setup, I drag and drop the button to the exit segue like I am told in all tutorials, but in the end it's still inactive.

当我完成所有这些设置后,我按照所有教程中所述将按钮拖放到退出转场,但最终它仍然无法点击。

英文:

I'm new to Swift and working on a portfolio app. I've read that it's possible to add a button with an exit segue to go back a screen using a Navigation Controller.
However, I'd like to implement this feature without using a Navigation Controller.

I've tried creating a button with an exit segue and connecting it to an IBAction, but the button remains inactive when clicked. All the tutorials I've found use a Navigation Controller to implement this feature, so I'm not sure if it's possible to do it without one.

My question is: is it possible to add a button with an exit segue and have it work as expected without using a Navigation Controller? If so, what steps do I need to follow to make it work?

Thanks for any help you can provide!

Here is the code I have in the SecondScreen Swift file:

import UIKit

class SecondScreen: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
    @IBAction func goBack(segue: UIStoryboardSegue) {
        // This method is called when the exit segue is performed.
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "goBack" {
            // This is where you would prepare any data to be passed back to the previous view controller.
        }
    }

}

The first screen has the follwoing code:

import UIKit

class FirstScreen: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }


}

When I have the all this setup, I drag and drop the button to the exit segue like I am told in all tutorials, but in the end it's still inactive

可以在Swift中实现不使用导航控制器的退出过渡吗?

答案1

得分: 1

你没有正确按照教程的指示操作。将 goBack 移动到 FirstScreen 中,一切都会正常。这个方法是一种目标,告诉反向转场要返回到哪里。它的实现不必包含任何内容;它只需存在于所需的目标中即可。

英文:

You didn't follow the tutorial directions correctly. Move goBack into FirstScreen and all will be well. This method is a kind of target telling the unwind segue where to go "back" to. (It doesn't have to have any content in its implementation; it just needs to be present in the desired destination.)

huangapple
  • 本文由 发表于 2023年4月13日 20:59:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76005738.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定