Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

objective c - access to property of another class

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 *'

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...