问题描述
我的应用程序有两个视图,具体取决于它决定加载哪个视图的方向。但IB不允许我将两个PickerView连接到同一个OUTLET,有没有办法在代码中分配连接,这样当我加载视图时我将连接分配到插座?
I have two views for my app depending on what orientation it decides which view to load. But IB wont allow me to connect two PickerViews to the same OUTLET, is there a way to assign connections in code, so that when I load my view I assign the connections to the outlet?
或者我应该为每个视图做双重操作吗?
Or should I do everything double for each view?
或者我应该将两个视图分成不同的nib文件
Or should I split my two views into different nib files
请在此帮助我
谢谢
推荐答案
嗯,请记住,IBOutlet只是一种已经以在IB中可见的方式声明的属性。所以第一个问题的答案是肯定的。如有必要,您始终可以在代码中重新分配该属性。
Well, bear in mind that an IBOutlet is simply a property that has been declared in a way that makes it visible in IB. So the answer to your first question is yes. You can always reassign that property in code if necessary.
我认为您已经有两个IBOutlet用于您的横向和纵向视图 - 如下所示:
I presume you already have two IBOutlets for your landscape and portrait views -- something like this:
@property (nonatomic, retain) IBOutlet UIView *landscapeView;
@property (nonatomic, retain) IBOutlet UIView *portraitView;
听起来你在中选择了合适的视图willAnimateRotationToInterfaceOrientation:duration :
。
同样,您可以为选择器视图声明两个出口:
Similarly you could declare two outlets for your picker views:
@property (nonatomic, retain) IBOutlet UIPickerView *landscapePickerView;
@property (nonatomic, retain) IBOutlet UIPickerView *portraitPickerView;
如果你走这条路线,我会声明一个动态属性,它始终返回选择器视图当前定位。
If you go this route, I'd declare a dynamic property that always returns the picker view for the current orientation.
@property (nonatomic, retain, readonly) UIPickerView *pickerView;
您可以像下面这样实现它:
Rather than synthesizing this property, you could implement it like this:
- (UIPickerView *) pickerView {
if (self.landscapeView.superview) {
return self.landscapePickerView;
}
else {
return self.portraitPickerView;
}
}
但是,如果您有多个或两个子视图有这样的并行属性会使控制器变得混乱可能会很痛苦。在这种情况下,我会考虑创建一个UIView的自定义子类,称为PickerContainer,它有pickerView的出口和你需要访问的任何其他子视图。然后在IB中,您可以将景观和纵向视图的类更改为PickerContainer,并且可以将每个选取器直接连接到其超级视图。然后在你的控制器中你可以创建一个这样的动态属性:
However, if you have more than one or two subviews, having parallel properties like this cluttering your controller could get to be a pain. In that case, I'd consider making a custom subclass of UIView called something like PickerContainer that has outlets for your pickerView and any other subviews you need to access. Then in IB you can change the classes of your landscape and portrait views to PickerContainer, and you can connect each picker directly to its superview. Then in your controller you can just create one dynamic property like this:
@property (nonatomic, retain, readonly) PickerContainer *pickerContainer;
- (PickerContainer *)pickerContainer {
return (PickerContainer *)self.view;
}
然后您可以通过其容器访问当前方向的pickerView这个:
and after that you can access your pickerView for the current orientation via its container like this:
[self.pickerContainer.pickerView reloadAllComponents];
编辑:这是我实施的方式 willAnimateRotationToInterfaceOrientation:duration:
在我的一个项目上:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration {
if (UIInterfaceOrientationIsPortrait(interfaceOrientation)) {
if (self.landscapeView.superview) [self.landscapeView removeFromSuperview];
self.portraitView.center = CGPointMake(self.view.bounds.size.width / 2,
self.view.bounds.size.height / 2);
[self.view addSubview:self.portraitView];
}
else {
if (self.portraitView.superview) [self.portraitView removeFromSuperview];
self.landscapeView.center = CGPointMake(self.view.bounds.size.width / 2,
self.view.bounds.size.height / 2);
[self.view addSubview:self.landscapeView];
}
}
我的横向和纵向视图配置时没有支柱或在IB中弹簧,意味着所有边距都是灵活的,但宽度和高度不是。
My landscape and portrait views were configured with no struts or springs in IB, meaning all the margins are flexible, but the width and height aren't.
这篇关于两个视图多个UIPickerViews单个插座的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!