代表仍为零

时间:2018-09-01 22:35:48

标签: ios swift delegates segue

我正在将用户数据从名为CreateNewAccount的视图控制器传递到另一个名为ThanksForJoining的视图控制器。由于某种原因,我的代表是nil。我正在使用segue来将我的vc.delegate设置为self= self),并且segue标识符“ thanksForJoining”是指将CreateNewAccount与{{1 }}在情节提要上。但是不知何故,委托人仍然是ThanksForJoining

CreateNewAccount:

nil

感谢加入:

import UIKit

protocol UserInfoDelegate {
   func sendUserInfo(firstName: String, lastName: String, username: String, password: String)
}

class CreateNewAccount: UIViewController{
    @IBOutlet weak var FNInput: UITextField!
    @IBOutlet weak var LNInput: UITextField!
    @IBOutlet weak var usernameInput: UITextField!
    @IBOutlet weak var passwordInput: UITextField!

    var infoDelegate: UserInfoDelegate?

    @IBAction func sendInfo(_ sender: Any) {
        if(infoDelegate != nil){
            if(FNInput.text != nil && LNInput.text != nil && usernameInput.text != nil && passwordInput.text != nil){
                let firstName = FNInput.text
                let lastName = LNInput.text
                let username = usernameInput.text
                let password = passwordInput.text

                infoDelegate?.sendUserInfo(firstName: firstName!, lastName: lastName!, username: username!, password: password!)
            }
        }
    }
}

3 个答案:

答案 0 :(得分:1)

首先,您需要确认:

  • 您已通过Segue将CreateNewAccount连接到ThanksForJoining

ViewControllersConnected

  • 正确地将segue的标识符设置为thanksForJoining (请注意字母的大小写。)

ConnectingSegue

如果这两个都不正确,则您浪费了一些时间,而我却浪费了时间为台风做准备。更新您的问题以澄清情况,然后等待某人帮助您...


假设以上两件事,在源视图控制器上调用prepare(for:sender:)。您需要在CreateNewAccount类中实现它。

CreateNewAccount:

import UIKit

class CreateNewAccount: UIViewController {
    @IBOutlet weak var firstNameInput: UITextField!
    @IBOutlet weak var lastNameInput: UITextField!
    @IBOutlet weak var usernameInput: UITextField!
    @IBOutlet weak var passwordInput: UITextField!

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "thanksForJoining" {
            let destinationVC = segue.destination as! ThanksForJoining
            if
                let firstName = firstNameInput.text, !firstName.isEmpty,
                let lastName = lastNameInput.text, !lastName.isEmpty,
                let username = usernameInput.text, !username.isEmpty,
                let password = passwordInput.text, !password.isEmpty
            {
                destinationVC.receiveUserInfo(firstName: firstName, lastName: lastName, username: username, password: password)
            }
        }
    }
}

感谢加入:

import UIKit

class ThanksForJoining: UIViewController {
    var firstName: String?

    @IBOutlet weak var firstNameLabel: UILabel!

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        firstNameLabel.text = firstName
    }

    func receiveUserInfo(firstName: String, lastName: String, username: String, password: String) {
        print(firstName)
        self.firstName = firstName
    }
}

似乎委派模式对于您的目的来说实在太多了,您只需要在目标视图控制器ThanksForJoining中定义一个数据传递方法即可。

我假设您已经通过CreateNewAccount的某个按钮连接了segue。如果segue是通过视图控制器(而不是按钮)连接的,则上面的代码需要进行一些小的修改。

但是无论如何,在您的原始代码中,将永远不会调用prepare(for:sender:)中的方法ThanksForJoining,因此就不会设置委托。因此,委托仍为nil

答案 1 :(得分:0)

首先,您需要对ThankForJoining类中的CreateNewAccount类进行引用。然后,需要通过在viewDidLoad中的ThanksForJoing类中将其设置为self来激活该委托。

class ThanksForJoining: UIViewController, UserInfoDelegate {
    var createNewAccount: CreateNewAccount?

    override func viewDidLoad() {
        super.viewDidLoad()
        createNewAccount?.infoDelegate = self
    }

{

然后您的委托方法将起作用。

答案 2 :(得分:0)

唯一可能的问题是代码和情节提要中的segue标识符不匹配

在故事板上,选择两个VC之间的segue

enter image description here

然后进入属性检查器,并在“标识符”字段中将ID设置为“ thanksForJoining”

enter image description here

一些建议

如果您打算在发送数据之前检查用户是否已输入所有字段,那么此代码将更好地达到目的

if (infoDelegate != nil) {
    if let firstName = FNInput.text, firstName.count>0,
        let lastName = LNInput.text, lastName.count>0,
        let username = usernameInput.text, username.count>0,
        let password = passwordInput.text, password.count>0 {

        infoDelegate?.sendUserInfo(firstName: firstName, lastName: lastName, username: username, password: password)
    }
}