我想创建一个加载 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;