0

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)
    }
  . . .
}

Screenshot 1

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?

Community
  • 1
  • 1
Jayprakash Dubey
  • 35,723
  • 18
  • 170
  • 177
  • `objForgotPassword` is completely new instance, it is not previous view controller, and it will never be presented on screen – Yury Aug 25 '16 at 13:05
  • @ShadowOf is right. Plus, you can take a look at this post http://stackoverflow.com/questions/6203799/dismissmodalviewcontroller-and-pass-data-back as suggested by Max Pevsner. – Jerome Aug 25 '16 at 13:08
  • You can access instance of ForgotPasswordViewController by checking self.presentingViewController in SecurityQuestionViewController – Mohammed Shakeer Aug 25 '16 at 13:08
  • @JJBoursier : Pls check question. It is further elaborated. – Jayprakash Dubey Aug 26 '16 at 05:58

2 Answers2

5

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.

Max Pevsner
  • 4,098
  • 2
  • 18
  • 32
0

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

Mubin Mall
  • 546
  • 10
  • 25