xcode 在 Swift 中将变量传递回父级

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

Passing variable back to parent in Swift

iosxcodeswift

提问by LittleJohnD

I am re-writing a tutorial converting the code from Objective-C to swift. The app moves from VC one where there is 3 sliders (Red, Green and Blue) that set the background colour, a label of the colour name and a button that links to the second VC. In the second VC the colour from the first VC is used as the background and the user has a chance to name the colour.

我正在重新编写一个将代码从 Objective-C 转换为 swift 的教程。该应用程序从 VC 移动,其中有 3 个滑块(红色、绿色和蓝色)用于设置背景颜色、颜色名称标签和链接到第二个 VC 的按钮。在第二个 VC 中,第一个 VC 的颜色用作背景,用户有机会命名颜色。

When the user enters the colour name it should return the new colour name to the orginal VC and the label that shows the colour name should show the text entered.

当用户输入颜色名称时,它应该将新颜色名称返回给原始 VC,显示颜色名称的标签应显示输入的文本。

The following is the code that is causing issue:

以下是导致问题的代码:

func textFieldShouldReturn(nameEntry: UITextField) -> Bool
{
    ViewController().colourLabel.text = nameEntry.text
    nameEntry.resignFirstResponder()
    dismissViewControllerAnimated(true, completion: nil)
    return true
}

The error "fatal error: unexpectedly found nil while unwrapping an Optional value" is generated. However debugging nameEntry.text has a string in it.

生成错误“致命错误:在展开可选值时意外发现 nil”。但是调试 nameEntry.text 中有一个字符串。

I'm a little stumped. I could try and do a prepare for unwind segue but it is meant to be a tutorial app.

我有点难住了。我可以尝试为 unwind segue 做准备,但它是一个教程应用程序。

Cheers

干杯

回答by rakeshbs

ViewController()actually creates a new instance of your ViewController. This is not a reference to the already existing ViewController. What you can do is create a weak variable pointing to first ViewController inside the second ViewController and set it at prepareForSegue or when the second View controller is shown.

ViewController()实际上创建了一个新的ViewController. 这不是对已经存在的ViewController. 您可以做的是在第二个 ViewController 中创建一个指向第一个 ViewController 的弱变量,并将其设置为 prepareForSegue 或显示第二个 View Controller 时。

class SecondViewController : UIViewController {
    weak var firstViewController : ViewController?

    // Other code

    func textFieldShouldReturn(nameEntry: UITextField) -> Bool
    {
        firstViewController?.colourLabel.text = nameEntry.text
        nameEntry.resignFirstResponder()
        dismissViewControllerAnimated(true, completion: nil)
        return true
    }
}

Inside First View Controller prepareForSegue

在第一个视图控制器中 prepareForSegue

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "SecondViewController" {

        let secondViewController = segue.destinationViewController as SecondViewController
        secondViewController.firstViewController = self
    }
}

回答by Casey Fleser

It's possible that the view controller returned by ViewController() has not yet loaded its views. You could try checking this in a setter function and storing it for later use once the views have been loaded.

ViewController() 返回的视图控制器可能尚未加载其视图。您可以尝试在 setter 函数中检查它并在加载视图后将其存储以备后用。

class VC : UIViewController {
    @IBOutlet weak var colourLabel: UILabel!
    var savedLabelText: String?

    override func viewDidLoad() {
        super.viewDidLoad()

        self.colourLabel.text = self.savedLabelText
    }

    func setColorLabelText(label: String) {
        if self.isViewLoaded() {
            self.colourLabel.text = label
        }
        else {
            self.savedLabelText = label
        }
    }
}