它显示源视图控制器没有字符串变量,所以该怎么办?
这是我的源视图控制器中的代码
@IBOutlet var textfield: UITextField!
@IBOutlet var tofirstbutton: UIButton!
@IBOutlet var tosecondbutton: UIButton!
var s:String!
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "unwindtosecond"{
self.s=self.textfield.text
}
}
这是我的目标视图控制器中的代码
@IBOutlet var textfield: UITextField!
@IBOutlet var tofirstbutton: UIButton!
@IBOutlet var tothirdbutton: UIButton!
@IBOutlet var label: UILabel!
@IBAction func unwindtosecond(Segue:UIStoryboardSegue){
if Segue.identifier == "unwindtosecond" {
var source:ViewController = Segue.sourceViewController as ViewController
var s:String = source.s!
}
我收到一条错误消息,说viewcontroller没有名为s的成员。
答案 0 :(得分:0)
尝试以下解决方案:
答案 1 :(得分:0)
以下是一些示例代码,用于实现主控制器与第二个VC的连接,以及第二个向主控制器展开的代码。请注意,在第二个cVC展开的函数中,您可以参考第二个控制器并访问第二个控制器中的变量。如果要在主VC中初始化第二个VC中的变量,可以在主VC中使用prepareForSegue。
import UIKit
class ViewController: UIViewController {
var varInMainVC = ""
@IBAction func segueToSecondVCAction(sender: UIButton) {
performSegueWithIdentifier("segueToSecondVC", sender: self)
}
@IBAction func unwindFromSecondVC(segue: UIStoryboardSegue) {
// return here from second VC
let vc = segue.sourceViewController as! SecondViewController
varInMainVC = vc.varInSecondVC
println("varInMainVC = \(varInMainVC)")
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
import UIKit
class SecondViewController: UIViewController {
var varInSecondVC = ""
var varLoadedFromMainVC = 0
override func viewDidLoad() {
super.viewDidLoad()
varInSecondVC = "Test String to be returned"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}