协议/代表中的IBOutlets难度

时间:2018-02-05 22:05:50

标签: ios swift uitableview viewcontroller iboutlet

我对IBOutlets有困难。我尝试允许用户在表视图控制器(LoLAddGoalsTableViewController)中输入目标(名为nameOfRewardText),然后当他们点击" Done"时,将该目标显示在名为&的标签中#34; currentGoalTextField"在不同的视图控制器(LoLGoalViewController)中。我一直试图使用Save segue来实现它,但建议使用带代理的协议(Updating text in ViewController using Save function)。现在我已经用协议和委托替换了保存segue,输入的" nameOfRewardText"文本没有显示在" currentGoalTextField"标签,我怀疑是因为IBOutlets不再正确捆绑在一起。我已经附上了下面奥特莱斯的代码和截图,试图澄清我在哪里。有谁知道我如何修复IBOutlets,或者是否还需要添加其他内容以使其正常工作?我删除了将nameOfRewardText.text指定为goal.goalText的行,所以我认为nameOfRewardText没有被分配到var目标?也许我为这个文本使用了太多的名字(nameOfRewardText,goalText和currentGoalTextField),这会让事情变得复杂吗?任何帮助都会非常感激,因为我对此非常陌生!谢谢大家!

这是结构目标:

import UIKit

struct Goal {
    var goalText: String
    var pointsToCompleteGoal: Int
    var pointsEarnedTowardsGoal: Int
    var repeatGoal: Bool

    init(goalText: String, pointsToCompleteGoal: Int, pointsEarnedTowardsGoal: Int, repeatGoal: Bool = false) { //Made String non-optional. If issue later, can revert.
        self.goalText = goalText
        self.pointsToCompleteGoal = pointsToCompleteGoal
        self.pointsEarnedTowardsGoal = pointsEarnedTowardsGoal
        self.repeatGoal = repeatGoal
    }
}

以下是公共协议:

import Foundation
import UIKit

protocol GoalDelegate: class {
    func passGoal(_ goal: Goal?)
}

这是创建委托的地方,正如您所看到的,我将nameOfRewardText.text指定为goal.goalText的语句现已消失:

import UIKit

class AddGoalsTableViewController: UITableViewController {

    var goal:Goal?
    var delegate: GoalDelegate?

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// HASHED OUT THE BELOW BECAUSE REPLACING WITH DELEGATE:
//        if segue.identifier == "SaveGoal" {
//            let pointsNeededInt = Int(pointsNeededText.text!)
//            let pointsEarnedInt = Int(goalProgressText.text!)
//            goal = Goal(goalText: nameOfRewardText.text!, pointsToCompleteGoal: pointsNeededInt!, pointsEarnedTowardsGoal: pointsEarnedInt!)
//        }

        if let secondViewController = segue.destination as? LoLGoalViewController{
            delegate = secondViewController
            delegate?.passGoal(goal)
        }
    }

    @IBOutlet var goalTableTitleText : UILabel!
    @IBOutlet weak var goalProgressText: UILabel!
    @IBOutlet weak var nameOfRewardText: UITextField!
    @IBOutlet weak var pointsNeededText: UITextField!
    @IBOutlet weak var repeatSwitch: UISwitch!


override func viewDidLoad() {
    super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

带有插座的AddGoalsTableViewController的屏幕截图: LoLGoalViewController

这里我遵守协议并调用函数passGoal:

import UIKit

class LoLGoalViewController: UIViewController, GoalDelegate {

    @IBOutlet weak var currentGoalTextField: UILabel!

    func passGoal(_ goal: Goal?) {
        currentGoalTextField.text = goal?.goalText
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

extension LoLGoalViewController {

    @IBAction func cancelToLoLGoalViewController(_ segue: UIStoryboardSegue) {
    }
}

带有插座的LoLGoalViewController的屏幕截图: LoLGoalViewController

1 个答案:

答案 0 :(得分:0)

您的LoLGoalViewController视图控制器可能没有完全加载其所有插座。添加到我之前问题的答案,您可以在LolGoalViewController中声明另一个变量:

@IBOutlet weak var currentGoalTextField: UILabel!

var goalText: String = ""

passGoal方法中,将字符串设置为goalText变量而不是标签的文字:

func passGoal(_ goal: Goal?) {
    goalText = goal?.goalText
}

最后,在viewDidLoad的{​​{1}}中,将标签文字设为LolGoalViewController

goalText