Reputation: 36447
I'm trying to pass value between two view controller but it failed. Second view controller (which is table view controller) is presented modally when button present in first view controller is pressed.
When user taps on any cell present in second view controller it is dismissed and value of cell is set to button present in first view controller.
Below is code that I've used :
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)
}
. . .
}
Where I'm going wrong? Any other solution?
This link helped in creating a delegate using Swift language.
However, the app crashes when second view controller is presented using modal segue.
It crashes when delegate is declared weak and set to nil in second view controller. It works fine for show segue.
How can this be done using modal segue?
Upvotes: 0
Views: 233
Reputation: 546
Okay Bro I Can give you clue... No need of notification no need of protocol...
Do one thing Just grab the parent view controller's instance from navigation hierarchy and access its property variable in which you want to set the data
If you are presenting View controller then access parent by this
ParentVC *objParentVC = self.presentingViewController
If you are pushing view controller then access parent by
ParentVC *objParentVC = [[self.navigationController childViewController] objectAtIndex:self.navigationController.childViewController.count - 2];
Just for clarification. Here if you -2 will be your parent. -1 will be your current view controller. as its count
Upvotes: 0
Reputation: 4104
Add a protocol to your SecurityQuestionViewController
with a function that accepts the data you want to pass to the previous viewController as a parameter. In your ForgotPasswordViewController
implement that function. Before dismissing the SecurityQuestionViewController
call that function on delegate.
Upvotes: 5