如何从其他类访问ViewController属性?
(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 *'
类型的对象上找不到属性'web'答案 0 :(得分:2)
我认为这篇文章可能会回答你的问题
How can I access variables from another class?
您需要从外部类(使用@property和@synthesize)访问您的变量,然后您的外国类需要知道您的ViewController实例
<强> [编辑] 强>
在你的AppDelegate.m中,必须有这样的一行:
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
这是ViewController实例化的地方。从那里,您可以访问ViewController的每个属性。
<强> [编辑] 强>
您需要在实施开始时添加以下内容
@implementation ViewController
@synthesize web;