我想创建一个节标题来加载一个 nib 文件并将其设置为标题 UIView。这个 nib 文件还将有一个关联的类,其中连接了 outlets 和 action,所以我想像平常一样用 nib 加载那个类。
我已经在网上搜索并找到了几个类似的答案,但我无法找到适合我的答案。玩了几天后,我设法让 View 正确显示,但尽管添加了连接并告诉文本以不同方式显示,但它什么也没做。
例如,如果它是用 nil 初始化的,它应该清除部分标题文本并且它这样做但它仍然显示相同的文本,尝试更改它不会反射(reflect)任何并且不会触发与按钮建立的任何连接。
这是我的 View Controller
@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
在实际的 TableView Controller 中它被列为
@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;
}
这是 Action 的方法声明
- (IBAction)addNewRow:(id)sender;
最佳答案
您应该创建一个 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
的文件所有者。并将 XIB 的顶级 View 的引用导出连接到上面的 view
属性(即在 File's Owner 中)。
然后您有一个 UIView 子类,您可以将其放置在另一个 XIB 上(作为一个 UIView,您将其类设置为 ADUNewRowHeaderView),或者在代码中实例化并添加为 subview 。
(或者,您可以在代码中创建 UIView 及其 subview (按钮、标签等);这样您就不需要 XIB。但这当然只有在 UIView 很简单且几乎没有自己的逻辑时才有效,以及一些易于在代码中布局的 UI 元素。)
关于ios - 使用 Nib 作为带有 Nib 类的表节标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16256403/