how to call the function prepare

Discussion in 'iOS Programming' started by theprizerevealed, Feb 3, 2019.

  1. theprizerevealed macrumors member

    Joined:
    Feb 26, 2016
    #1
    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?
     
  2. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    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
        }
    }
     
  3. deadworlds, Feb 4, 2019
    Last edited: Feb 4, 2019

    deadworlds macrumors 65816

    deadworlds

    Joined:
    Jun 15, 2007
    Location:
    Citrus Heights,CA
    #3
    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.
     
  4. cmaier macrumors G4

    Joined:
    Jul 25, 2007
    Location:
    California
    #4
    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.
     
  5. MisterSavage, Feb 5, 2019
    Last edited: Feb 5, 2019

    MisterSavage macrumors 6502

    MisterSavage

    Joined:
    Nov 10, 2018
    #5
    Thank you for this tip. I'm going to do just that.
     
  6. AnonMac50 macrumors 65816

    Joined:
    Mar 24, 2010
    #6
    It’s control drag. Or right click drag.
     
  7. cmaier macrumors G4

    Joined:
    Jul 25, 2007
    Location:
    California
    #7
    Thanks. I do it without thinking, so I forgot :)
     
  8. AnonMac50 macrumors 65816

    Joined:
    Mar 24, 2010
    #8
    No worries! I forgot about the control drag way too until you mentioned it!
     

Share This Page