Access Outlets from a different class
So heres my issue. I assume its a simple one but i just cannot figure out why this is not working.
I need to access Outlets (UITextField, UITableView, UIActivityIndicatorView, etc.) in one class (RootViewController) from a different class (ServiceController).
I have this currently (only showing whats need) :
RootViewController.h
@interface RootViewController : UIViewController{
UITextField *textField;
}
@property (nonatomic, retain) IBOutlet UITextField *textField;
- (void)startService开发者_运维问答;
@end
RootViewController.m
@implementation RootViewController
@synthesize textField;
- (void)startService{
ServiceController *service = [[ServiceController alloc] init];
[service start];
[service release];
}
@end
ServiceController.h
@class RootViewController;
@interface ServiceController : NSObject{
}
@property (nonatomic, retain) IBOutlet RootViewController *rootViewController;
- (void)start;
@end
ServiceController.m
#import "RootViewController.h"
@implementation ServiceController
@synthesize rootViewController;
- (void)start{
[((RootViewController*)rootViewController).textField setText:@"HELLO !"];
NSLog(@"CALLED !");
}
@end
I do not see why this is not working, i use the same method to do the same thing from the RootViewController to the DetailViewController and it works fine. I checked to see if there were any other declaration in the detail/root controllers to allow that to work but i did not notice any.
PS: everything is being called successfully and "CALLED !" is being logged, just whatever i do to the outlet(s) has no effect.
Your ServiceController
needs to have its rootViewController
property set. You can either do this in an XIB file or explicitly in code:
myServiceController.rootViewController = myRootViewController;
It looks like you didn't set the rootViewController
.
Try adding :
ServiceController *service = [[ServiceController alloc] init];
// set the property here
[service setRootViewController:self];
[service start];
[service release];
精彩评论