Can you please tell me how can I transfer the value of a variable from one ViewController to another? Now implement as follows: In the cell click handler

tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath): let appDelegate = UIApplication.sharedApplication().delegate! as! AppDelegate let TaskData: TaskListTableViewController = TaskListTableViewController() TaskData.catID = currentCell.categoryID!.text // В этом месте пытаюсь передать данные let initialViewController = self.storyboard!.instantiateViewControllerWithIdentifier("taskListTable")// as! UIViewController appDelegate.window?.rootViewController = initialViewController appDelegate.window?.makeKeyAndVisible() В контроллере TaskListTableViewController переменная объявлена: class TaskListTableViewController: UITableViewController, UIPageViewControllerDelegate { var catID: String? //------------------------------ ... ... 

But when executing catID returns nil

Please tell me what could be the problem?

    1 answer 1

    It is better to use segue :

    1 RMB from the controller from which we go to the controller to which we will move: enter image description here

    2 segue :

    enter image description here

    3 Override prepareForSegue as you need.

     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { let controller = segue.destinationViewController as! SecondVC //приводим `destinationViewController` к нужному нам `SecondVC` controller.s = "Woohoo" //s - строковая переменная, которой присваивем "Woohoo" } 

    4 on the desired event perform performSegueWithIdentifier("SHOWVC", sender: self)

    • one
      segue will not work, as in the first controller (TableViewController), when you first click on a cell, the updated data of the same table is displayed, and the next click should send to the second TableViewController, passing the data of the clicked cell. Using segue transfer to the second controller was carried out at the first click - M.Sol
    • @ M.Sol You can add a flag and, on the first click, update, and on the second, send to the second VC . - VAndrJ
    • Can an example implementation? - M.Sol
    • @ M.Sol if isCellFirstClick { //update data } else { //perform segue } , where isCellFirstClick boolean flag for the TableView cell. - VAndrJ