Search code examples
objective-cios6propertiesuiviewcontroller

How to access properties from other view controllers


I am using Xcode 4.6.1 to develop for iOS 6.

I have two view controllers: viewController1 and viewController2. I am trying to access a property defined in viewController1 in viewController2 as follow (but it isn't working):

This is viewController1.m:

#import "viewController1.h"

@interface viewController1 ()

@property (nonatomic) MPMoviePlayerController *videoPlayer;

@end

@implementation viewController1

...

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    ...

    self.videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];

    ...

    [self.videoPlayer play];

    ...
}

This is viewController2.m:

#import "viewController2.h"
#import "viewController1.h"

@interface viewController2 ()

@end

@implementation viewController2

...

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    viewController1 *thePlayer;
    [thePlayer.videoPlayer pause];

    //This is where I get an error:
    //Property 'videoPlayer' not found on object of type 'viewController1 *'
}

I have tried a lot of searching but I wasn't able to figure out what am I doing wrong. Any help would be highly appreciated.

Thanks! :)


Solution

  • You've got your videoPlayer property defined in your viewController1.m, but you import viewController1.h.

    Move videoPlayer to viewController1.h. DO NOT import viewController1.m.