如何从其他视图控制器访问属性

时间:2013-06-28 15:34:49

标签: objective-c ios6 properties uiviewcontroller

我正在使用Xcode 4.6.1为iOS 6开发。

我有两个视图控制器:viewController1viewController2。我正在尝试访问viewController1viewController2中定义的属性,如下所示(但它不起作用):

这是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];

    ...
}

这是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 *'
}

我尝试了很多搜索,但我无法弄清楚我做错了什么。任何帮助都将受到高度赞赏。

谢谢! :)

1 个答案:

答案 0 :(得分:3)

您已在videoPlayer中定义了viewController1.m属性,但导入了viewController1.h

videoPlayer移至viewController1.h。请勿导入viewController1.m