【发布时间】:2017-01-01 22:25:13
【问题描述】:
我正在尝试在两个视图控制器之间传递值,但失败了。 当按下第一个视图控制器中的按钮时,第二个视图控制器(即表格视图控制器)会以模态的方式呈现。
当用户点击第二个视图控制器中存在的任何单元格时,它将被关闭,并且单元格的值设置为第一个视图控制器中存在的按钮。
下面是我用过的代码:
ForgotPasswordViewController
class ForgotPasswordViewController: UIViewController {
var securityQuestion = ""
@IBOutlet weak var btnSecurityQuestion: UIButton!
//MARK: - viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
}
var secQuestionAccessor: (String) {
get{
return securityQuestion
}set(param) { print("param : \(param)") //Value is correctly printed in console
securityQuestion = param
print("securityQuestion : \(securityQuestion)") //Value is correctly printed in console
}
}
//MARK: - viewWillAppear
override func viewWillAppear(animated: Bool) {
. . .
print("secQuestionAccessor : \(secQuestionAccessor)") //Value is empty here
if secQuestionAccessor == "" {
btnSecurityQuestion.setTitle("Select", forState: UIControlState.Normal)
}
else {
btnSecurityQuestion.setTitle(secQuestionAccessor, forState: UIControlState.Normal)
}
. . .
}
. . .
}
SecurityQuestionViewController:
class SecurityQuestionViewController: UITableViewController {
. . .
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let objForgotPassword = ForgotPasswordViewController()
print("\(responseQuestion.objectAtIndex(indexPath.row) as! String)") //Value printed correctly here
objForgotPassword.secQuestionAccessor = responseQuestion.objectAtIndex(indexPath.row) as! String
self.dismissViewControllerAnimated(true, completion: nil)
}
. . .
}
我哪里出错了?还有其他解决方案吗?
这个link 帮助使用 Swift 语言创建了一个委托。
但是,app crashes when second view controller is presented using modal segue.
当委托被声明为弱并在第二个视图控制器中设置为 nil 时,它崩溃。它适用于 show segue。
如何使用模态转场来做到这一点?
【问题讨论】:
-
objForgotPassword是全新的实例,它不是以前的视图控制器,永远不会出现在屏幕上 -
@ShadowOf 是对的。另外,您可以按照 Max Pevsner 的建议查看此帖子 stackoverflow.com/questions/6203799/…。
-
您可以通过检查 SecurityQuestionViewController 中的 self.presentingViewController 来访问 ForgotPasswordViewController 的实例
-
@JJBoursier:请检查问题。进一步阐述。
标签: ios swift xcode storyboard segue