views:

204

answers:

2

Hi there,

So to give a little background:

I've an app that has a UITableViewController- (ContactDetailViewController) In this view at the top, I require a few labels and buttons, followed by a group style tableview. So I've created a nib file containing these elements. (ContactHeaderView.xib) Then in the viewDidLoad of ContactDetailViewController I've loaded this nib as the headerView. See implementation file below:

#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

Here's the Header File:

#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

However when I run it, I get the following error message:

* Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key nameLabel.'

In IB I've hooked up the labels/buttons/textbox to the File's Owner (set the File's Owner Class to: ContactDetailViewController)

Anyone any idea what I'm doing wrong?

Edit: added a screen shot of IB and connections: Go to: http://www.freeimagehosting.net/uploads/44c65c7ef3.png

Regards, Fiona

A: 

Are you switching between platforms? As in like nib built on old platform and used on the new one? Try cleaning all targets may that would help.

jAmi
Hi jAmi, Thanks for taking the time to look at it... No, I'm not switching between platforms... And I've tried cleaning.. I've deleted the nib and redone it. I was concerned that my method might be wrong.. does it look good to you? Also added an link to a screen shot of IB and connections in original message...
Fiona
OK, may be the error is because you are subclassing UITableViewController.Try UIViewController instead like @interface ContactDetailViewController : UIViewController<UITableViewDelegate, UITableViewDataSource> {
jAmi
Ok.. i tried it but getting the same error... same place.. where I attempt to return the view from the viewForHeaderInSection method.Any other ideas?!
Fiona
ContactHeaderView.xib has fileOwner type ContactDetailView and is being allocated with UIViewController class type. I think you should refine that(try making a seperate ContactHeaderView class if you dont already have one and then assign that as the FileOwner of ContactHeaderView.xib). If even this does not work then I m sorry I can't help you right now because I do not hav XCode installed . May be i will be able to help you when I get bak home after 3-4 hours.
jAmi
A: 

Ok I found a solution. In ContactDetailViewController, I create and IBOutlet UIView. Then in viewForHeaderInSection I did the following:

- (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;
    }
}

Finally in IB I connected the IBOutlet UIView created in ContactDetailViewController, to the view.

Thanks for your help jAmi. Fiona

Fiona