access to property of another class

落花浮王杯 提交于 2019-12-14 03:11:56

问题


How can I access to ViewController property from another class ?
(the ViewController was created by XCode)

this is code of 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

thanks!

[EDIT]

I use Xcode 4.3.2 and the code of AppDelegate.m not find something like:

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

Now, I put in the method didFinishLaunchingWithOptions:(into AppDelegate.m) this code:

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

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

adding in the file AppDelegate.h the variable 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

I did it correctly? the error is: Property 'web' not found on object of type 'UIViewController *'


回答1:


I think this post might answer your question

How can I access variables from another class?

You need to make your variable accessible from foreign classes (with @property and @synthesize) and then your foreign class need to know your instance of ViewController

[EDIT]

In your AppDelegate.m, there must be a line like this one :

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

This is where your ViewController is instantiate. From there, you can access every property of your ViewController.

[EDIT]

You need to add the following at the beginning of your implementation

@implementation ViewController

@synthesize web;


来源:https://stackoverflow.com/questions/10632764/access-to-property-of-another-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!