gpt4 book ai didi

ios - 委托(delegate)仍然为零

转载 作者:行者123 更新时间:2023-11-28 14:17:11 26 4
gpt4 key购买 nike

我将用户数据从名为 CreateNewAccount 的 View Controller 传递到另一个名为 ThanksForJoining 的 View Controller 。出于某种原因,我的委托(delegate)是 nil。我正在使用 segue 以将我的 vc.delegate 设置为 self (= self) 并且 segue 标识符“thanksForJoining”指的是将 CreateNewAccount 连接到 Storyboard 上的 ThanksForJoining 的 segue。但不知何故,委托(delegate)仍然是 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!)
}
}
}
}

感谢加入:

import UIKit

class ThanksForJoining: UIViewController, UserInfoDelegate {
@IBOutlet weak var fName: UILabel!

func sendUserInfo(firstName: String, lastName: String, username: String, password: String) {
print(firstName)
fName.text = firstName
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "thanksForJoining") {
let createNewAccount: CreateNewAccount = segue.destination as! CreateNewAccount
createNewAccount.infoDelegate = self
}
}
}

最佳答案

首先,您需要确认:

  • 您通过 segue 将 CreateNewAccount 连接到 ThanksForJoining

ViewControllersConnected

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

ConnectingSegue

如果两者中有任何一个不成立,你就浪费了一点时间,我就浪费了准备大台风的时间。更新您的问题以澄清正在发生的事情并等待有人帮助您...


假设以上两件事,prepare(for:sender:) 在源 View Controller 上被调用。您需要在 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
}
}

似乎委托(delegate)模式对您的目的来说有点太多了,您只需要在目标 View Controller ThanksForJoining 中定义一个数据传递方法。

我假设您已经通过 CreateNewAccount 的某个按钮连接了您的 segue。如果 segue 是从 View Controller (而不是按钮)连接的,则上面的代码需要稍作修改。

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

关于ios - 委托(delegate)仍然为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52132668/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com