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)
4

1 回答 1

1

创建协议/委托。关于如何实现这一点有很多资源。总之,您使用希望在源上运行的方法在目标 viewController 上创建协议,然后从源 viewController 订阅该委托。

协议代表

不要忘记从您的 prepareForSegue 方法设置委托

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

}

于 2013-05-03T13:43:57.357 回答