访问属性形成目标c中的uiviewcontroler实例。

时间:2011-10-31 21:56:43

标签: objective-c properties ios5 xcode4.2

我是编程新手,盯着客观c。我想在一个文件中声明一个属性并从另一个文件中访问它,但由于某种原因它不起作用。我可能正在做一些非常愚蠢的事情,不要怪我。

我有第一个标题:

#import <Foundation/Foundation.h>
#import "second.h"

@interface ViewController : UIViewController{
    NSString* theText;
}
@property (nonatomic, assign) IBOutlet UITextField *textField;
@property (nonatomic, retain) NSString *theText;



@end

这里是.m文件

#import "ViewController.h"

@implementation ViewController
@synthesize theText,textField;

- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
    [self setTheText:textField.text];

}

@end

现在我想在其他文件中使用theText属性来使用它并更改它。所以我认为这会有效,但事实并非如此:

第二个.m文件:

#import "second.h"
#import "ViewController.h"

@implementation second
@synthesize secLabel;

-(void)nameLabel{

    secLabel.text = [ViewController theText];
}

@end

编译器说没有已知的选择器类方法。我尝试了很多,但没有任何效果,有人知道如何使这项工作?

TNX

2 个答案:

答案 0 :(得分:0)

theText是一个实例级属性,但您尝试在ViewController类上访问它。相反,您需要在ViewController的某个实例上访问它。换句话说,你需要:

ViewController *viewController = ... some code to get a ViewController pointer ...
secLabel.text = [viewController theText];

您的“第二个”视图控制器需要以某种方式获取ViewController的实例。你如何实现这一点取决于你的应用程序,如果不了解你的代码,我就不能说。

答案 1 :(得分:0)

http://www.galloway.me.uk/tutorials/singleton-classes/

这将向您展示如何正确创建单例类。

这可能不是最干净的方式,但它在正确的存储数据路径上。