所以给一点背景:
我的应用程序有一个UITableViewController-(ContactDetailViewController) 在顶部的这个视图中,我需要一些标签和按钮,然后是一个组样式的tableview。 所以我创建了一个包含这些元素的nib文件。 (ContactHeaderView.xib) 然后在ContactDetailViewController的viewDidLoad中我将这个nib作为headerView加载。请参阅下面的实施文件:
#import "ContactDetailViewController.h"
#import "DisplayInfoViewController.h"
#import "ActionViewController.h"
@implementation ContactDetailViewController
@synthesize name;
@synthesize date;
@synthesize nextAction;
@synthesize nameLabel;
@synthesize usernameLabel;
@synthesize nextActionTextField;
@synthesize dateLabel;
@synthesize contactInfoButton;
@synthesize backgroundInfoButton;
@synthesize actionDoneButton;
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 3;
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if (section == 0){
UIViewController *chv = [[[UIViewController alloc] initWithNibName:@"ContactHeaderView" bundle:nil] autorelease];
// self.nameLabel.text = self.name;
return chv.view;
}else{
return nil;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 300.0;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
// [self.navigationController pushViewController:anotherViewController];
// [anotherViewController release];
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
- (void)dealloc {
[name release];
[date release];
[nextAction release];
[nameLabel release];
[usernameLabel release];
[nextActionTextField release];
[dateLabel release];
[contactInfoButton release];
[backgroundInfoButton release];
[actionDoneButton release];
[super dealloc];
}
-(IBAction)displayContactInfo:(id)sender{
DisplayInfoViewController *divc = [[DisplayInfoViewController alloc] init];
divc.textView = self.nextAction;
divc.title = @"Contact Info";
[self.navigationController pushViewController:divc animated:YES];
[divc release];
}
-(IBAction)displayBackgroundInfo:(id)sender{
DisplayInfoViewController *divc = [[DisplayInfoViewController alloc] init];
divc.textView = self.nextAction;
divc.title = @"Background Info";
[self.navigationController pushViewController:divc animated:YES];
[divc release];
}
-(IBAction)actionDone:(id)sender{
ActionViewController *avc = [[ActionViewController alloc] init];
avc.title = @"Action";
avc.nextAction = self.nextAction;
[self.navigationController pushViewController:avc animated:YES];
[avc release];
}
@end
这是头文件:
#import <UIKit/UIKit.h>
@interface ContactDetailViewController : UITableViewController {
NSString *name;
NSString *date;
NSString *nextAction;
IBOutlet UILabel *nameLabel;
IBOutlet UILabel *usernameLabel;
IBOutlet UITextField *nextActionTextField;
IBOutlet UILabel *dateLabel;
IBOutlet UIButton *contactInfoButton;
IBOutlet UIButton *backgroundInfoButton;
IBOutlet UIButton *actionDoneButton;
}
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *date;
@property (nonatomic, retain) NSString *nextAction;
@property (nonatomic, retain) IBOutlet UILabel *nameLabel;
@property (nonatomic, retain) IBOutlet UILabel *usernameLabel;
@property (nonatomic, retain) IBOutlet UITextField *nextActionTextField;
@property (nonatomic, retain) IBOutlet UILabel *dateLabel;
@property (nonatomic, retain) IBOutlet UIButton *contactInfoButton;
@property (nonatomic, retain) IBOutlet UIButton *backgroundInfoButton;
@property (nonatomic, retain) IBOutlet UIButton *actionDoneButton;
-(IBAction)displayContactInfo: (id)sender;
-(IBAction)displayBackgroundInfo: (id)sender;
-(IBAction)actionDone: (id)sender;
@end
然而,当我运行它时,我收到以下错误消息:
***由于未捕获的异常'NSUnknownKeyException'终止应用程序,原因:'[setValue:forUndefinedKey:]:此类不是密钥nameLabel的密钥值编码兼容。'
在IB中,我将标签/按钮/文本框连接到文件所有者(将文件所有者类设置为:ContactDetailViewController)
任何人都知道我做错了什么?
编辑:添加了IB和连接的屏幕截图: 去: http://www.freeimagehosting.net/uploads/44c65c7ef3.png
此致 菲奥娜
答案 0 :(得分:0)
您是否在平台之间切换? 就像在旧平台上构建的nib一样,在新平台上使用? 尝试清理所有可能有帮助的目标。
答案 1 :(得分:0)
好的,我找到了解决方案。 在ContactDetailViewController中,我创建了IBOutlet UIView。 然后在viewForHeaderInSection中我执行了以下操作:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if (section == 0){
//UIViewController *chv = [[[UIViewController alloc] initWithNibName:@"ContactHeaderView" bundle:nil] autorelease];
//headerView = chv.view;
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ContactHeaderView" owner:self options:nil];
headerView = [nib objectAtIndex:0];
self.nameLabel.text = self.name;
self.nextActionTextField.text = self.nextAction;
self.dateLabel.text = self.date;
return headerView;
}else{
return nil;
}
}
最后在IB中,我将在ContactDetailViewController中创建的IBOutlet UIView连接到视图。
感谢您的帮助jAmi。 菲奥娜