加载UIView子类的Nib的正确方法
我知道这个问题以前已经问过,但答案是矛盾的,我很困惑,所以请不要阻止我。
  我想在我的应用程序中有一个可重用的UIView子类。  我想用一个nib文件描述接口。 
现在我们假设它是一个带有活动指标的加载指标视图。 我希望在某个事件中实例化这个视图并将其动画化为视图控制器的视图。 我可以通过编程方式描述视图的接口没有问题,以编程方式创建元素并在init方法内设置它们的框架等。
我怎样才能使用笔尖做到这一点? 保持界面生成器中给定的大小而不必设置框架。
我已经设法做到这一点,但我确定它是错误的(这只是一个带有选择器的视图):
 - (id)initWithDataSource:(NSDictionary *)dataSource {
        self = [super init];
        if (self){
            self = [[[NSBundle mainBundle] loadNibNamed:[NSString stringWithFormat:@"%@", [self class]] owner:self options:nil] objectAtIndex:0];
            self.pickerViewData = dataSource;
            [self configurePickerView];
        }
        return self;
    }
但是我覆盖自我,当我实例化它时:
FSASelectView *selectView = [[FSASelectView alloc] initWithDataSource:selectViewDictionary];
    selectView.delegate = self;
    selectView.frame = CGRectMake(0, self.view.bottom + 50, [FSASelectView width], [FSASelectView height]);
我必须手动设置框架,而不是从IB中拾取框架。
编辑:我想在视图控制器中创建此自定义视图,并有权控制视图的元素。 我不想要一个新的视图控制器。
谢谢
编辑:我不知道这是否是最佳做法,我敢肯定它不是,但这是我做到的:
FSASelectView *selectView = [[[NSBundle mainBundle] loadNibNamed:[NSString stringWithFormat:@"%@",[FSASelectView class]] owner:self options:nil] objectAtIndex:0];
    selectView.delegate = self;
    [selectView configurePickerViewWithData:ds];
    selectView.frame = CGRectMake(0, self.view.bottom + 50, selectView.width, selectView.height);
    selectView.alpha = 0.9;
    [self.view addSubview:selectView];
    [UIView animateWithDuration: 0.25 delay: 0 options:UIViewAnimationOptionAllowUserInteraction |UIViewAnimationOptionCurveEaseInOut animations:^{
                            selectView.frame = CGRectMake(0, self.view.bottom - selectView.height, selectView.width, selectView.height);
                            selectView.alpha = 1;
                        } completion:^(BOOL finished) {
                        }];
仍然需要正确的做法
这是否应该使用视图控制器和使用nib名称的init来完成? 我应该在代码中的某个UIView初始化方法中设置nib吗? 或者我做得很好?
MyViewClass *myViewObject = [[[NSBundle mainBundle] loadNibNamed:@"MyViewClassNib" owner:self options:nil] objectAtIndex:0]
我用这个来初始化我有的可重用的自定义视图。
请注意,您可以在末尾使用“firstObject” ,它会更清洁一些。 “firstObject”是NSArray和NSMutableArray的一个方便的方法。
下面是一个典型的例子,加载一个xib用作表头。 在你的文件YourClass.m
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[NSBundle mainBundle] loadNibNamed:@"TopArea" owner:self options:nil].firstObject;
}
  通常,在TopArea.xib ,您可以单击文件所有者并将文件所有者设置为YourClass 。  然后,实际上在YourClass.h中,您将拥有IBOutlet属性。  在TopArea.xib ,您可以将控件拖放到这些插座。 
  不要忘记,在TopArea.xib ,你可能必须点击视图本身并将其拖到某个插座,以便在必要时控制它。  (一个非常有价值的提示是,当你为表格单元格行这样做时,你必须这样做 - 你必须将视图本身连接到代码中的相关属性。) 
  如果您想让自CustomView及其xib独立于File's Owner ,请按照下列步骤操作 
File's Owner字段留空。 xib的文件CustomView并设置其Custom Class为CustomView (自定义视图类的名称) .h文件中添加IBOutlet 。 .xib文件中,单击视图并进入Connection Inspector 。  在这里,你将所有你在.h文件中定义的IBOutlets   在你的CustomView类的.m文件中,如下覆盖init方法 
-(CustomView *) init{
    CustomView *result = nil;
    NSArray* elements = [[NSBundle mainBundle] loadNibNamed: NSStringFromClass([self class]) owner:self options: nil];
    for (id anObject in elements)
    {
        if ([anObject isKindOfClass:[self class]])
        {
            result = anObject;
            break;
        }
    }
    return result;
}
  现在,当你想加载你的CustomView ,使用下面这行代码[[CustomView alloc] init]; 
按照以下步骤
UIView 。 MyView.xib相同的MyView.xib 。 NSObject更改为UIViewController 。  看到下面的图片 
  将文件所有者视图连接到您的视图。  看到下面的图片 
  将视图的类更改为MyView 。  与3相同。 
这里是加载视图的代码:
UIViewController *controller=[[UIViewController alloc] initWithNibName:@"MyView" bundle:nil];
MyView* view=(MyView*)controller.view;
[self.view addSubview:myview];
希望能帮助到你。
澄清 :
  UIViewController用来加载你的厦门国际银行和该视图UIViewController实际上已经是MyView ,你在厦门国际银行MyView的已指定.. 
演示我在这里做了一个演示
链接地址: http://www.djcxy.com/p/87695.html