2

I have two view controllers. MainViewController pushes SignUp ViewController. Once SignUp View Controller dismisses, I need access to the same instance of MainViewController to update a UIButton.title.

.h MainView

//MainViewController.h
#import <UIKit/UIKit.h>


@interface MainViewController : UITableViewController 
-(void)loggedIn;

@end

.m

@interface MainViewController ()

@end    

-(void)loggedIn
{
    NSLog (@"This is Logged in inside MainView.m");
    self.logInOutButton.title = @"Logout";
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    UIViewController *destinationViewController = segue.destinationViewController;
    Signup *signUp = [destinationViewController isKindOfClass:[SignUp class]] ? (Signup*)destinationViewController : nil;
    signUp.mainViewController = self;
    NSLog(@"Preapre For Segue %@", self);
}

.h SignUp

#import <UIKit/UIKit.h>
#import "MainViewController.h"
@interface SignUp : UIViewController 
@property (strong, nonatomic) MainViewController *mainViewController;
@end

.m

@synthesize mainViewController;

- (void) loggedIn
{
    NSLog(@" MainViewController %@", mainViewController);
    [mainViewController loggedIn];
    self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}

When I do NSLog I'm getting different values:

Preapre For Seg <STMainViewController: 0x9f7d600>
MainViewController (null)
user1107173
  • 10,334
  • 16
  • 72
  • 117
  • 1
    Have you tried logging what the signUp View Controller is after you assign the mainViewController? Looks like there might be an intermediary controller between the two, most likely a navigation controller... – David Doyle May 03 '13 at 13:25
  • 1
    Check to see if `signUp` is `nil` before you set the `mainViewController` property. – Marcus Adams May 03 '13 at 13:42
  • 1
    See the discussion of delegate protocols in the "passing data back" section of http://stackoverflow.com/a/9736559/1271826 – Rob May 03 '13 at 13:49
  • 1
    You use SignUp with a capital U in the declaration of the class. Then you use Signup with a lowercase U in the MainViewController. This would cause signUp to be nil. This may be because you re-typed it here instead of copying and pasting. If that is the case, then copy and paste directly, as it may show some errors you didn't anticipate. – Justin Paulson May 03 '13 at 13:56

1 Answers1

1

Create a protocol / delegate. There are numerous resources on how to achieve this. In summary, you create the protocol on the destination viewController with the method you wish to run on the source and then you subscribe to that delegate from the source viewController.

Protocol Delegate

Dont forget to set the delegate from your prepareForSegue method

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    Signup *signUp = segue.destinationViewController;
    signup.delegate = self;
    NSLog(@"Preapre For Segue %@", self);

}

Community
  • 1
  • 1
Mark McCorkle
  • 9,349
  • 2
  • 32
  • 42