简体   繁体   English

从不同的 class 访问插座

[英]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).我需要从不同的 class(ServiceController)访问一个 class(RootViewController)中的 Outlets(UITextField、UITableView、UIActivityIndicatorView 等)。

I have this currently (only showing whats need):我目前有这个(只显示需要什么):

RootViewController.h RootViewController.h

@interface RootViewController : UIViewController{
    UITextField *textField;
}

@property (nonatomic, retain) IBOutlet UITextField *textField;

- (void)startService;

@end

RootViewController.m根视图控制器.m

@implementation RootViewController

@synthesize textField;

- (void)startService{
    ServiceController *service = [[ServiceController alloc] init];
    [service start];
    [service release];
}

@end

ServiceController.h服务控制器.h

@class RootViewController;

@interface ServiceController : NSObject{
}

@property (nonatomic, retain) IBOutlet RootViewController *rootViewController;

- (void)start;

@end

ServiceController.m服务控制器.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.我不明白为什么这不起作用,我使用相同的方法从 RootViewController 到 DetailViewController 做同样的事情,它工作正常。 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. PS:一切都被成功调用,并且正在记录“CALLED”。 just whatever i do to the outlet(s) has no effect.只是我对插座所做的任何事情都没有效果。

It looks like you didn't set the rootViewController .看起来你没有设置rootViewController Try adding:尝试添加:

ServiceController *service = [[ServiceController alloc] init];

// set the property here
[service setRootViewController:self];

[service start];
[service release];

Your ServiceController needs to have its rootViewController property set.您的ServiceController需要设置其rootViewController属性。 You can either do this in an XIB file or explicitly in code:您可以在 XIB 文件中执行此操作,也可以在代码中显式执行此操作:

myServiceController.rootViewController = myRootViewController;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM