iOS how to call the function prepare

theprizerevealed

macrumors regular
Original poster
Feb 26, 2016
164
6
This function

Code:
func prepare(for segue: UIStoryboardSegue,
     sender: Any?)
is something I'm trying to use in app. But I don't know what to use for UIStoryboardSegue. Something is required but I don't know what. I have only one storyboard Main.storyboard.

I tried using that but it didn't work and I tried using the name of the segue that I want it to follow but that doesn't work either. How do I call this function?
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
prepare(for segue is called by UIKit when a segue is executed. Usually you compare segue.identifier to the identifiers your code knows about. The identifiers are specified in IB for each segue when its configured. Your code might also get the destination view controller and set a property on it.

Code:
if segue.identifier == "MyIdentifier" {
    if let controller = segue.destination as? MyViewController {
        controller.detailItem = someObject
    }
}
 
  • Like
Reactions: AnonMac50

deadworlds

macrumors 65816
Jun 15, 2007
1,026
752
Citrus Heights,CA
Check out lesson 3.6 “Segues and navigation controllers” page 333, from the Apple book “ App Development with swift”. That chapter has everything you need to know about switching views using prepare for segue.

I have told you before and will continue to say it. Work through the book, every single chapter, section and project. You will learn so much about how to put together a proper iOS app.
 
Last edited:

cmaier

macrumors P6
Jul 25, 2007
18,190
16,394
California
This function

Code:
func prepare(for segue: UIStoryboardSegue,
     sender: Any?)
is something I'm trying to use in app. But I don't know what to use for UIStoryboardSegue. Something is required but I don't know what. I have only one storyboard Main.storyboard.

I tried using that but it didn't work and I tried using the name of the segue that I want it to follow but that doesn't work either. How do I call this function?
You can create a segue by clicking and dragging in interface builder between an element (e.g. a button) and another view controller. (I forget the modifier key - option? I do it by muscle memory and my computer isn’t in front of me).

Once you do that, if you run the app, and tap the button, prepareForSegue will be called by the system prior to the segue being performed. It usually helps to give the segue an identifier in interface builder so that if you have different segues you can choose the appropriate behavior in prepareForSegue. Typically the function is used to send information to the destination viewcontroller that the destination needs.
 
  • Like
Reactions: AnonMac50

AnonMac50

macrumors 68000
Mar 24, 2010
1,553
308
You can create a segue by clicking and dragging in interface builder between an element (e.g. a button) and another view controller. (I forget the modifier key - option? I do it by muscle memory and my computer isn’t in front of me).

Once you do that, if you run the app, and tap the button, prepareForSegue will be called by the system prior to the segue being performed. It usually helps to give the segue an identifier in interface builder so that if you have different segues you can choose the appropriate behavior in prepareForSegue. Typically the function is used to send information to the destination viewcontroller that the destination needs.
It’s control drag. Or right click drag.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.