你的确不是很清楚。你可能想要尝试的第一件事是描述你做了什么:
我们假设你的两个视图是从两个不同的nib文件实例化的。
让我们假设这是一个视图控制器。单个视图控制器控制来自两个不同的nib文件的两个视图有点不寻常,但毕竟,为什么不呢?
在任何情况下,你可以设置所有者类你的两个笔尖文件是类视图控制器。然后在Interface Builder中,从第一个视图中,可以将标签绑定到文件所有者的出口。而在Interface Builder中,从第二视图中,可以将UISwitch
绑定到文件所有者的第二出口,UISwitch
类型。
但是你的方法可能是你的两个视图类中的一个?同样的想法适用:您可以将第二个nib文件中的文件所有者设置为第一个视图的视图类,然后将该开关绑定到文件所有者的出口。
但它听起来像是你的设计可能是值得努力...
编辑:您的评论后,这里更是一个有点...
的问题是,你的两个视图控制器每个控制一个不同的页面,没有理由相互了解。所以你需要一个中间人物。这可能是另一个控制器。让我们使用应用程序委托。然后,在你SwitchViewController的IBAction
方法,你可以这样做:
- (IBAction) switchChangedValue:(UISwitch *) sender {
NSString *newLabelText = sender.isOn ? @"On" : @"Off";
self.labelViewController.label.text = newLabelText;
}
现在怎么会每个人都知道对方的存在?首先每个视图控制器将通知中间人。下面是它的SwitchViewController:
- (void) viewDidLoad
{
[super viewDidLoad];
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.switchViewController = self;
}
其次,应用程序的委托将需要协调的一切:
@interface MyAppDelegate : …
@property (nonatomic, retain) SwitchViewController *switchViewController;
@property (nonatomic, retain) LabelViewController *labelViewController;
@end
@implementation MyAppDelegate
@synthesize switchViewController = _switchViewController;
@synthesize labelViewController = _labelViewController;
- (void) setSwitchViewController:(SwitchViewController *) newSwitchController {
if (newSwitchController != _switchViewController) {
[_switchViewController release];
_switchViewController = [newSwitchController retain];
_switchViewController.labelViewController = _labelViewController;
if (_labelViewController)
_labelViewController.label.text = _switchViewController.switch.isOn ? @"On" : @"Off";
}
}
- (void) setLabelViewController:(LabelViewController *) newLabelController {
if (newLabelController != _labelViewController) {
[_labelViewController release];
_labelViewController = [newLabelController retain];
_labelViewController.switchViewController = _switchViewController;
if (_switchViewController)
_labelViewController.label.text = _switchViewController.switch.isOn ? @"On" : @"Off";
}
}
我留下了一些细节,但我希望大画面清晰。
感谢您的回复!首先,它是一个Tabbar应用程序。所以我想在第一个标签中放一个标签,在第二个标签中放一个开关来控制标签。 是的,有不同的笔尖文件,它是从视图控制器是。 但我真的不明白是谁把它连接在一起:/ - (void)方法是否是最好的方法? – AlexF
你用void方法表示什么意思?如果你想在viewController中使用一个UIKit对象,并将它添加到你的nib文件中,你必须从viewController设置它并将它链接到nib中。 –