如何分配和调用一个变量 to/from 一个按钮
How to assign and call a variable to/from a button
我正在尝试将一个变量分配给一个按钮并调用该变量以将其传递给另一个 viewcontroller。
目前我正在发送这样一个按钮的标题:
((sender as! UIButton).titleLabel?.text)!
但是我有一个按钮,我想将一个字符串发送到另一个与其标题不同的 viewcontroller。我尝试在身份检查器的 "label" 位置添加一些内容,但这似乎不是正确的方法。
感谢任何建议,谢谢!
将变量存储在 class 的其他位置并像这样设置 didSet 注释
var myTitle: String{
didSet{
self.theDesiredButton.setTitle(myTitle, for: .normal)
//alternatively you can use
self.theDesiredButton.title = myTitle
}
}
这里将变量传递给另一个控制器:
override func prepareForSegue(/*dunno args I code from mobile*/){
//guess figure out segueIdentifier and desired Vc subclass
if let myCustomVC = segue.viewContoller as? CustomVCSubclass{
myCustomVC.valueToPass = self.myTitle
}
}
或者您可以使用标识符实例化 viewController 作为您的子classed VC 并以相同的方式传递值
func pushNextVC(){
if let newVC = storyboard.instantiateViewController(with: "identifierFromIB") as? CustomVCSubclass{
newVC.valueToPass = self.myTitle
self.NavigationController.push(newVC)
}
}
如有任何问题,请提问 :) 祝编码愉快
首先在下一个ViewController中为按钮创建一个出口,并添加一个字符串变量并使用viewDidLoad
中的方法setTitle(_ title: String?, for state: UIControlState)
设置标题
class SecondViewController: UIViewController {
@IBOutlet weak var button: UIButton!
var buttonText: String?
override func viewDidLoad() {
super.viewDidLoad()
if let buttonText = buttonText {
button.setTitle(buttonText, for: .normal)
}
}
}
并在 FirstViewController 中将文本分配给 SecondVC 中的字符串变量,如下所示
class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func buttonClicked(_ sender: UIButton) {
self.performSegue(withIdentifier: "CustomSegue", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "CustomSegue" {
let vc = segue.destination as? SecondViewController
vc?.buttonText = "ButtonTitle"
}
}
}
我正在尝试将一个变量分配给一个按钮并调用该变量以将其传递给另一个 viewcontroller。
目前我正在发送这样一个按钮的标题:
((sender as! UIButton).titleLabel?.text)!
但是我有一个按钮,我想将一个字符串发送到另一个与其标题不同的 viewcontroller。我尝试在身份检查器的 "label" 位置添加一些内容,但这似乎不是正确的方法。
感谢任何建议,谢谢!
将变量存储在 class 的其他位置并像这样设置 didSet 注释
var myTitle: String{
didSet{
self.theDesiredButton.setTitle(myTitle, for: .normal)
//alternatively you can use
self.theDesiredButton.title = myTitle
}
}
这里将变量传递给另一个控制器:
override func prepareForSegue(/*dunno args I code from mobile*/){
//guess figure out segueIdentifier and desired Vc subclass
if let myCustomVC = segue.viewContoller as? CustomVCSubclass{
myCustomVC.valueToPass = self.myTitle
}
}
或者您可以使用标识符实例化 viewController 作为您的子classed VC 并以相同的方式传递值
func pushNextVC(){
if let newVC = storyboard.instantiateViewController(with: "identifierFromIB") as? CustomVCSubclass{
newVC.valueToPass = self.myTitle
self.NavigationController.push(newVC)
}
}
如有任何问题,请提问 :) 祝编码愉快
首先在下一个ViewController中为按钮创建一个出口,并添加一个字符串变量并使用viewDidLoad
中的方法setTitle(_ title: String?, for state: UIControlState)
设置标题
class SecondViewController: UIViewController {
@IBOutlet weak var button: UIButton!
var buttonText: String?
override func viewDidLoad() {
super.viewDidLoad()
if let buttonText = buttonText {
button.setTitle(buttonText, for: .normal)
}
}
}
并在 FirstViewController 中将文本分配给 SecondVC 中的字符串变量,如下所示
class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func buttonClicked(_ sender: UIButton) {
self.performSegue(withIdentifier: "CustomSegue", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "CustomSegue" {
let vc = segue.destination as? SecondViewController
vc?.buttonText = "ButtonTitle"
}
}
}