故事板和xib在同一个项目中使用 [英] Storyboard and xib use in same project
问题描述
我绊倒了我相信可能是一个根本的误解,如何在Objective-C中工作。我使用故事板,但在这个应用程序,我想为我的一个视图控制器上的文本字段创建一个简单的自定义日期选择器视图。但是,我似乎有一个问题从我的视图控制器访问日期选择器类的任何属性:
I am stumbling over what I believe is probably a fundamental misunderstanding of how classes work in Objective-C. I am using Storyboards but in this app I wanted to create a simple custom date picker view for a textfield on one of my view controllers. However, I seem to be having a problem accessing any of the properties of the date picker class from my view controller:
首先我在IB中建模我的CustomDatePicker.xib如下:
First I modeled my CustomDatePicker.xib in IB as follows:
>
我已经创建了一个自定义类通过sublcassing UIView如下:
I have created a custom class by sublcassing UIView as follows:
CustomPicker.h
CustomPicker.h
@interface CustomPickerView : UIView
@property (strong, nonatomic) IBOutlet UIDatePicker* datePicker;
@end
CustomerPicker.m
CustomerPicker.m
@implementation CustomPickerView
- (id)init
{
self = [super initWithFrame:CGRectMake(0, 0, 320, 258)];
if (self) {
[self addSubview:[[[NSBundle mainBundle] loadNibNamed:@"CustomPickerView" owner:self options:nil] objectAtIndex:0]];
}
return self;
}
在我的ViewController中:
In my ViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
self.customPickerView=[[CustomPickerView alloc] init];
// THE MAIN ISSUE IS...
// Following line has no effect on how picker is presented ???
self.customPickerView.datePicker.datePickerMode=UIDatePickerModeTime;
self.dateField.inputView=self.customPickerView;
}
当文本框被点击时,我的CustomDatePicker弹出。但是,我似乎无法从我的ViewController的viewDidLoad方法设置.datePickerMode。我可以改变.datePickerMode的唯一方法是通过IB,当然不能在运行时工作。
When the textfield is tapped, my CustomDatePicker pops up fine. However, I can't seem to set the .datePickerMode either from the viewDidLoad method of my ViewController. The only way I can change the .datePickerMode is through IB and of course that's not going to work at run-time.
我已经连接了IB的插座能够从类中访问datePicker.date,但不能访问ViewController。
I have wired up the outlet in IB and am able to access the datePicker.date from within the class but not the ViewController.
我已经研究并查看了多种方法来实现这个概念。我的问题不是如何实现CustomDatePicker它是为什么我不能从ViewController实例化的CustomDatePicker的属性访问实例化?
I have researched and viewed a number of ways to implement this concept. My question isn't how to implement a CustomDatePicker it is "Why can't I access properties of my CustomDatePicker from the ViewController that instantiated it?
推荐答案
请参阅解决方案:
Please see solution here at:
这篇关于故事板和xib在同一个项目中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!