3

我想创建一个加载 nib 文件并将其设置为标题 UIView 的节标题。这个 nib 文件还将有一个关联的类,其中连接了出口和操作,所以我想像往常一样用 nib 加载该类。

我在网上搜索并找到了几个类似的答案,但我无法为我工作。在玩了几天之后,我设法让视图正确显示,但是尽管添加了连接并告诉文本以不同方式显示,但它什么也没做。

例如,如果它是用 nil 初始化的,它应该清除部分标题文本,但它仍然显示相同的文本,尝试更改它不会反映任何一个,并且不会触发与按钮建立的任何连接。

这是我的视图控制器

@interface ADUNewRowHeaderViewController : UIViewController

@property (strong, nonatomic) IBOutlet UILabel *sectionTitleField;
@property (strong, nonatomic) IBOutlet UIButton *addRowBtn;

@property(strong,nonatomic) NSString* sectionTitle;

- (id)initWithNibName:(NSString *)nibNameOrNil
               bundle:(NSBundle *)nibBundleOrNil
                title:(NSString*) titleOrNil;

@end

这是实现

@implementation ADUNewRowHeaderViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil title:(NSString *)titleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        if(titleOrNil != nil)
        {
            [self setSectionTitle:titleOrNil];
        }
        else
        {
            [self setSectionTitle:@""];
        }
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setSectionTitle:(NSString *)sectionTitle
{
    _sectionTitle = sectionTitle;
    [[self sectionTitleField] setText:sectionTitle];
}
@end

在实际的表视图控制器中,它被列为

@property(nonatomic, strong) ADUNewRowHeaderViewController* secHeader;

并在 viewDidLoad 下的实现文件中为

[self setSecHeader:[[ADUNewRowHeaderViewController alloc] initWithNibName:nil bundle:nil title:nil]];
[[[self secHeader] addRowBtn] addTarget:self action:@selector(addNewRow:) forControlEvents:UIControlEventTouchUpInside];

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [[self secHeader] view];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return [[self secHeader] view].bounds.size.height;
}

这是动作的方法声明

- (IBAction)addNewRow:(id)sender;
4

1 回答 1

1

你应该创建一个UIView,而不是一个UIViewController,子类。将.m包含:

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        [self setUp];
    }

    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self setUp];
}

- (void)setUp
{    
    [[NSBundle mainBundle] loadNibNamed:@"YourNibName" owner:self options:nil];
    CGRect frame = self.frame;
    self.view.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
    [self addSubview:self.view];

    ... do other initialisations here ...
}

.h

@interface ADUNewRowHeaderView : UIView

@property (nonatomic, strong) IBOutlet UIView*   view;

然后在 XIB 中:ADUNewRowHeaderView像往常一样将文件的所有者设为 class 。并将XIB的顶层View的Referencing Outlet连接到view上面的属性(即在File's Owner中)。

然后,您有一个 UIView 子类,您可以将其放置在另一个 XIB 上(作为您将 Class 设置为 ADUNewRowHeaderView 的 UIView),或者在代码中实例化并添加为子视图。

(或者,您可以在代码中创建 UIView 及其子视图(按钮、标签等);那么您就不需要 XIB。但这当然只有在 UIView 很简单并且几乎没有自己的逻辑并且很少有 UI 的情况下才有效易于在代码中布局的元素。)

于 2013-04-27T21:12:41.897 回答