Passing Values Using PrepareForSegue In iOS App

Why Use Prepareforsegue Method

In this article, we will learn how to create and implement prepareforsegue in iPhone applications. Basically, this is a method used  when you use the table view and you need it when the user clicks on each cell  -- it moves on the next View Controller. For this purpose we use this method.

Creating a Simple Project

Open the Xcode editor and choose the Single View Application and create the project;  after that you will see in the Main.Storyboard file there is one ViewController, delete this ViewController and in the object library search Navigation Controller and  drag and drop it into Main.Storyboard file and now set the Navigation Controller to “Is Initial View Controller”.

Project

Set Up Table View Cell

Now click on table view cell and you see that in your table view cell the identifier is blank, now write “cell” in an identifier textbox. Then select the style “Basic”.

Cell

After that create a New File and select the Cocoa Touch Class and then click next and then give a class name and extend from UITableViewController and then press next and click on create -- now your Controller file is ready,

Controller

Assign Class to FirstViewController

Now go the Main.Storyboard section and click on Root View Controller and assign the class which is now called “FirstTableViewController”.

FirstViewController

Now drag and drop the new ViewController in Main.Storyboard file and connect the Root View Controller to Next ViewController -- click on the yellow button, press Ctrl button and click on the Next View Controller and choose the ManualSegue “Show”.

Show

Set Up Segue

Now click on the circle which appears between the two View Controllers and set the name of the segue identifier, I set the name of the identifier “SendDataSegue”.

SendDataSegue

Create A second ViewController

Now create a second ViewController with the same procedure of FirstViewController -- now I give the name of the class “SecondViewController” and extend with the subclass “UIViewController” and then create a class.
SecondViewController

Now go the Second ViewController and bind the class “SecondViewController” as in the previous procedure for FirstViewController, and then drag a label and drop it into the second ViewController and choose the Assistant Mode in the left side. Open the the Second ViewController and in the left side open the SecondViewController class, then click on it  while pressing the Ctrl button and drag it into the right class and choose the label name. I chose the label name “SegueLabel”.

SegueLabel

Set Up FirstTableViewController

Now go to the the FirstTableViewController file and set the following code in it.

  1. override func numberOfSectionsInTableView(tableView: UITableView) -> Int {  
  2.         return 1  
  3.     }  
  4.   
  5. override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {  
  6.           
  7.         return 10  
  8.     }  
  9.   
  10. override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell   
  11. {   
  12. let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)   
  13. cell.textLabel?.text = "Hello from cell #\(indexPath.row))" return cell   
  14. }  
  15.   
  16. override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {  
  17.         if segue.identifier == "SendDataSegue" {  
  18.             if let destination = segue.destinationViewController as? SecondViewController {  
  19.                   
  20.                 let path = tableView.indexPathForSelectedRow  
  21.                 let cell = tableView.cellForRowAtIndexPath(path!)  
  22.          
  23.                 destination.viaSegue=(cell?.textLabel?.text!)!  
  24.             }  
  25.         }  
  26.     }  
  27.       
  28. override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {  
  29.         _ = tableView.indexPathForSelectedRow!  
  30.         if let _ = tableView.cellForRowAtIndexPath(indexPath) {  
  31.             self.performSegueWithIdentifier("SendDataSegue", sender: self)  
  32.         }  
  33.           
  34.     }  
Now when you run the program in the application when you click on any table view cell it moves on the second ViewContoller.

 


Similar Articles