正确的使用方法是什么getters/setters

What is the correct way to use getters/setters

致意,

这是我之前 post () 的跟进。它得到了回答,但更新的编辑当然发生了。

我的查询如下,目前我有两个 IBActions 函数,它们是空的,没有任何代码,它们存在的唯一目的是连接到我的另一个视图控制器,因此如果我删除它们我无法区分应该使用哪个 segue(我的理解是,虽然我们可以在两个视图控制器之间创建 segue,但我认为只有一个是有意义的(例如,如何决定使用哪个一起去)。我离题了。

我之前尝试在我的代码中使用以下内容:

self.performSegue(withIdentifier: "slideTwo", sender: self);

这很好用,但是,它确实会导致双重转场。因此,我解决了以下问题:

override func prepare(for segue: UIStoryboardSegue, sender: Any?)
    {
        let passedPic = segue.destination as! ViewControllerTwo;

        if(segue.identifier == "slideOne") {
            passedPic.picsChosen = sLS1;
        } else {
            passedPic.picsChosen = sLS2;
        }  
    }

这很好用,满足了我的需要,但我确实遇到了两个空 IBAction 的问题:

@IBAction func slideShowOne() {

    }

    @IBAction func slideShowTwo() {

        //self.performSegue(withIdentifier: "slideTwo", sender: self);

    }

这是我认为也许我可以尝试创建一个 getter/setter 的地方(swift 使它比实际应该的复杂得多)。这就是我迷路的地方。

代码如下所示:

var picsChosen : [String] {
    set(newData) {
        for index in 0..<newData.count {
            print("Index is: ", newData[index]);
        }
        self._picsChosen = newData;
    } get {
        return self._picsChosen;
    }
}

现在,我的问题有两个。首先,实际访问值并将值传递到我的变量中的正确方法是什么。我尝试这样做:myClass: ViewControllerTwo! = ViewControllerTwo();

然后使用 myClass.picsChosen = myArray; 但是,尽管看起来数据确实已成功传递到我的 VC2 中,因为当我循环(在 setter 内)时,我确实看到值成功显示,当我尝试访问它之外的 picsChosen 时,我得到一个索引越界错误,换句话说,该值从未与 picsChosen 相关联。我目前使用的方法运行良好,但我想知道 getters/setters 的正确使用方法是什么,以及为什么我使用的方法不起作用。

干杯!

This works well, does what I need, but I do then have the problem of two empty IBActions:

您不需要这些空的 @IBAction。他们没有为你做任何事。 segues 是从情节提要中的按钮连线的,因此您不需要 @IBActions。如果删除代码,您还需要从故事板中删除连接,否则您的应用程序在尝试调用已删除的 @IBAction 时会崩溃。要删除,control-单击 Storyboard 中的按钮,然后单击 Touch Up Inside 旁边的小 x 以删除连接。然后你可以在你的代码中删除@IBAction

if I remove them I have no way to differentiate between which segue should be used

不正确。您有两个不同的 segue 连接到两个不同的按钮。 segue 的标识符是您区分这两个 segue 的方式。

I tried doing: myClass: ViewControllerTwo! = ViewControllerTwo();

使用 segue 时,segue 会为您创建目标 viewController。 这不起作用,因为您正在此处创建一个全新的 ViewControllerTwo。您正在将值传递给这个新实例,但是这不是 Storyboard segue 为您创建的 ViewControllerTwo

使用 segues 时,您应该在 prepare(for:sender:) 中传递数据,就像上面显示的那样。获取目的地ViewController,填写你要传递的数据

现在回答你没有问的问题:

How could I have done this using the @IBActions to trigger the segue?

  1. 移除按钮上的连线。

  2. 将 segue 从 viewController1 连接到 viewController2。单击 Storyboard 中的 segue 箭头并分配标识符 slideShow.

  3. 为您的两个按钮分配独特的标签。您可以在 Interface Builder 中进行设置。给第一个按钮标签1,第二个按钮标签2.

  4. 您的按钮可以共享相同的 @IBAction:

    @IBAction func goToSlideShow(button: UIButton) {
        self.performSegue(withIdentifier: "slideShow", sender: button)
    }
    
  5. prepare(for:sender:):

    override func prepare(for segue: UIStoryboardSegue, sender: Any?)
    {
        let passedPic = segue.destination as! ViewControllerTwo
    
        if let button = sender as? UIButton {
            if button.tag == 1 {
                passedPic.picsChosen = sLS1
            } else if button.tag == 2 {
                passedPic.picsChosen = sLS2
            }
        }
    }