我有一个针对此函数的提交UIButton,我有一个textField,我想传递给下一个视图控制器。因此,用户将键入textField以及键入的内容,一旦用户点击提交按钮,文本字段内的字符串将被传递到下一个视图控制器的UILabel。
func submitTapped() {
let secondViewController = SecondViewController(nibName: nil, bundle: nil)
navigationController?.pushViewController(secondViewController, animated: true)
print("Button tapped")
}
}
如何通过第二个控制器推送该字符串?另外,我尝试过使用prepareForSegue,但我认为这与segue无关,因为我们没有通过segue过渡屏幕,我们正在通过导航控制器进行转换,我是对的吗?
谢谢!
答案 0 :(得分:1)
在SecondViewController中,创建变量并在创建对象时指定值。检查以下示例:
class SecondViewController {
var dataString: String?
}
传递如下数据:
func submitTapped() {
let secondViewController = SecondViewController(nibName: nil, bundle: nil)
secondViewController.dataString = textField.text!
navigationController?.pushViewController(secondViewController, animated: true)
print("Button tapped")
}
}
答案 1 :(得分:1)
在YourSecondViewController中,声明一个变量。
class YourSecondViewController {
var strData: String?
}
使用以下代码传递数据:
func submitTapped() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let objSecond = storyboard.instantiateViewController(withIdentifier: "YourSecondViewControllerID")
objSecond.strData = yourTextField.text!
self.navigationController?.pushViewController(objSecond, animated: true)
}