Search code examples
iosobjective-cxcodeuipopover

How to send string value from secondView to mainView with use popover segue?


i want to send string value from secondView to mainView but I click OK button use this code [self dismissViewControllerAnimated:YES completion:nil]; and then prepare segue not working. When I want to click OK button, I send in textfield value to mainView controller.

Thanks in replies.

enter image description here enter image description here


Solution

  • In your Main View Controller (That contains the label) header file declare the outlet for the label:

    @property (nonatomic, weak) IBOutlet UILabel *nameLabel;
    

    In Your SettingsViewController.m import your ViewController.h. And change the OK button l.ke below>

    - (IBAction)goBack:(id)sender
    {
        ViewController *vCtrl = (ViewController *)self.presentingViewController;
        vCtrl.nameLabel.text  = yourTextField.text;
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    

    EDIT:

    As OP is using UINavigationController and push segues for navigating, the method should be changed to:

    - (IBAction)goBack:(id)sender
    {
        ViewController *vCtrl = (ViewController *)[[(UINavigationController *)self.presentingViewController viewControllers] lastObject];
        vCtrl.nameLabel.text  = yourTextField.text;
        [self dismissViewControllerAnimated:YES completion:nil];
    }