2012-05-17 87 views
0

如何从另一个类访问ViewController属性?
(在视图控制器是由的XCode创建)
访问另一个类的属性

这是ViewController.h代码:

#import <UIKit/UIKit.h> 
#import "AppDelegate.h" 

@interface ViewController : UIViewController{ 
    AppDelegate *appDelegate; //il delegate disponibile per tutta la classe 
    int numPag; //indice array (sulle pagine html da visualizzare) 

    NSString *path; 
    NSURL *baseURL; 

    NSString *file; 
    NSString *contentFile; 

} 


- (IBAction)sottrai:(id)sender; 

- (IBAction)aggiungi:(id)sender; 



@property (strong, nonatomic) IBOutlet UILabel *valore; 
@property (strong, nonatomic) IBOutlet UIWebView *web; 

@end 

的感谢!

[编辑]

我使用的Xcode 4.3.2和AppDelegate.m的代码没有找到这样的:

self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 

现在,我把方法didFinishLaunchingWithOptions:(进入AppDelegate.m )以下代码:

viewController = [[UIViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 

[viewController.web loadHTMLString:contentFile baseURL:baseURL]; 

在文件中添加AppDelegate.h可变的viewController:

#import <UIKit/UIKit.h> 

@interface AppDelegate : UIResponder <UIApplicationDelegate>{ 
    NSArray *pageInItalian; 
    NSArray *pageInEnglish; 
    UIViewController *viewController; 

} 

@property (strong, nonatomic) NSArray *pageInLanguage; 
@property (strong, nonatomic) UIWindow *window; 



@end 

我做得对吗?错误是:住宅“网络”的类型的对象未找到“的UIViewController *”

回答

2

我认为这个帖子可能会回答你的问题

How can I access variables from another class?

你需要让你的外国类变量访问(与@property和@synthesize),然后你的外国类需要知道你的ViewController的实例

[编辑]

在你APPD elegate.m,必须有这样一条线:

self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 

这是您的ViewController实例化的地方。从那里,你可以访问你的ViewController的每个属性。

[编辑]

您需要添加在你开始执行以下

@implementation ViewController 

@synthesize web; 
+0

我怎么走的ViewController的实例?视图控制器是由代码中的XCode创建的。我可以实例化它吗? – Antilope

+0

看看我的编辑。 –

+0

我还添加了编辑 – Antilope