How to pass prepareForSegue: an object

后端 未结 10 2067
北荒
北荒 2020-11-21 11:18

I have many annotations in a mapview (with rightCalloutAccessory buttons). The button will perform a segue from this mapview to a tableview

相关标签:
10条回答
  • 2020-11-21 11:43

    For Swift use this,

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        var segueID = segue.identifier
    
        if(segueID! == "yourSegueName"){
    
            var yourVC:YourViewController = segue.destinationViewController as YourViewController
    
            yourVC.objectOnYourVC = setObjectValueHere!
    
        }
    }
    
    0 讨论(0)
  • 2020-11-21 11:45

    Simply grab a reference to the target view controller in prepareForSegue: method and pass any objects you need to there. Here's an example...

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        // Make sure your segue name in storyboard is the same as this line
        if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
        {
            // Get reference to the destination view controller
            YourViewController *vc = [segue destinationViewController];
    
            // Pass any objects to the view controller here, like...
            [vc setMyObjectHere:object];
        }
    }
    

    REVISION: You can also use performSegueWithIdentifier:sender: method to activate the transition to a new view based on a selection or button press.

    For instance, consider I had two view controllers. The first contains three buttons and the second needs to know which of those buttons has been pressed before the transition. You could wire the buttons up to an IBAction in your code which uses performSegueWithIdentifier: method, like this...

    // When any of my buttons are pressed, push the next view
    - (IBAction)buttonPressed:(id)sender
    {
        [self performSegueWithIdentifier:@"MySegue" sender:sender];
    }
    
    // This will get called too before the view appears
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([[segue identifier] isEqualToString:@"MySegue"]) {
    
            // Get destination view
            SecondView *vc = [segue destinationViewController];
    
            // Get button tag number (or do whatever you need to do here, based on your object
            NSInteger tagIndex = [(UIButton *)sender tag];
    
            // Pass the information to your destination view
            [vc setSelectedButton:tagIndex];
        }
    }
    

    EDIT: The demo application I originally attached is now six years old, so I've removed it to avoid any confusion.

    0 讨论(0)
  • 2020-11-21 11:47

    Sometimes it is helpful to avoid creating a compile-time dependency between two view controllers. Here's how you can do it without caring about the type of the destination view controller:

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([segue.destinationViewController respondsToSelector:@selector(setMyData:)]) {
            [segue.destinationViewController performSelector:@selector(setMyData:) 
                                                  withObject:myData];
        } 
    }
    

    So as long as your destination view controller declares a public property, e.g.:

    @property (nonatomic, strong) MyData *myData;
    

    you can set this property in the previous view controller as I described above.

    0 讨论(0)
  • 2020-11-21 11:51

    Just use this function.

     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let index = CategorytableView.indexPathForSelectedRow
        let indexNumber = index?.row
        let VC = segue.destination as! DestinationViewController
       VC.value = self.data
    
    }
    
    0 讨论(0)
提交回复
热议问题