【问题标题】:Use a UIViewController's Subclass property as IBOutlet in another Subclass在另一个子类中使用 UIViewController 的子类属性作为 IBOutlet
【发布时间】:2013-06-05 13:51:39
【问题描述】:

我有一个 UIViewController 的子类,我们称之为 MySuperClass ,这个子类有一个 UITableView 属性,它不是以编程方式初始化的。 现在我想将 MySuperClass 子类化为 MySubclass 但这次我想通过 Interface Builder 而不是以编程方式设计 tableview。 我想要的是类似于 UIViewController 工作方式的东西,如果您将 UIViewController 子类化,它的视图属性已经初始化,但是当您将它带入 IB 时,您可以将其链接到 Interface Builder 的 UIView 项目,如何我这样做?

我的超类的源代码和这个类似:

//interface

#import <UIKit/UIKit.h>

@interface MySuperClass : UIViewController <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;


//implementation


#import "MySuperClass.h"


@implementation MySuperClass

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        [self initializeProperties];

    }
    return self;
}

- (void) awakeFromNib{

    [super awakeFromNib];

    [self initializeProperties];

}

- (void) initializeProperties{

    self.tableView = [[UITableView alloc] initWithFrame: self.view.frame style: UITableViewStylePlain];
    self.tableView.separatorColor = [UIColor clearColor];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    UIView *tableHeaderView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.width, self.bannerView.frame.size.height+kBannerDistance)];

    tableHeaderView.backgroundColor = [UIColor clearColor];

    self.tableView.tableHeaderView = tableHeaderView;


}

【问题讨论】:

    标签: ios objective-c uiviewcontroller interface-builder iboutlet


    【解决方案1】:

    只需在您的子类中“重新声明”@property

    #import <UIKit/UIKit.h>
    #import "MySuperClass.h"
    
    @interface MySubClass : MySuperClass
    
    @property (nonatomic, strong) IBOutlet UITableView *tableView;
    
    @end
    

    编译器会很聪明地理解您引用的是超类属性,并且 IB 链接到子类的属性没有问题。

    【讨论】:

    • 不是被 awakeFromNib 重新初始化了吗?
    • 来自 IB 的 tableView 集将替换为在 initializeProperties 中初始化的 tableView。所以基本上这个解决方案会使子类无用。
    【解决方案2】:

    这可能不是最好的解决方案,但应该可以完成。

    MySuperClass.h 中定义- initFromSubClassWithNibName: bundle:; 并像这样实现它:

    - (id) initFromSubClassWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
        return [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    }
    

    MySubClass

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
        return [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    }
    

    这样你可以逃避MySuperViewinit方法的实现并使用UIViewController's implementation. You can take the same approach withawakeFromNib`。这将避免以编程方式创建表视图。

    然后您可以从 IB 中获取 GuillaumeA 的答案来初始化 tableView

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-28
      • 1970-01-01
      相关资源
      最近更新 更多