Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass data to detail view after being selected in a table view?

I have a UITableViewController that has data populated from an NSMutableArray called userList. When specific user is selected, it goes to a detail view, and updates the title in the NavBar, but it wont pass the data to update a UILabel in the UIView.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

[tableView deselectRowAtIndexPath:indexPath animated:NO];

//Attempt at a singleton to pass the data
DetailView *details = [[DetailView alloc] init];
details.userNameLbl = [userList objectAtIndex:indexPath.row];


DetailView *detailVC = [[DetailView alloc] initWithNibName:nil bundle:nil];

//This line doesn't pass the data
detailVC.userNameLbl.text = [userList objectAtIndex:indexPath.row];

//This line does update the title in the NavBar
detailVC.navigationItem.title = [userList objectAtIndex:indexPath.row];

[self.navigationController pushViewController:detailVC animated:YES];

[details release];
[detailVC release];

}

Should I be trying to push the data from the table view to the detail view, or have the detail view try to pull the data from the table view?

DetailView.h has the following line that is in fact hooked up in IB.

IBOutlet UILabel *userNameLbl
like image 577
tarheel Avatar asked Sep 26 '11 22:09

tarheel


2 Answers

The userNamelbl isn't instantiated until the view is loaded from the nib file. This doesn't happen immediately at initialisation, but will have happened by the time viewDidLoad is called.

So, you should to declare a property in DetailView to store your title, and then assign that value to userNamelbl.text in the viewDidLoad method.

For example, in your table viewController:

DetailView *detailVC = [[DetailView alloc] initWithNibName:nil bundle:nil];
detailVC.userName = [userList objectAtIndex: indexPath.row];

and in your detail viewController:

- (void) viewDidLoad
{
   [super viewDidLoad];
   self.userNameLbl.text = self.userName;
}

The viewController's navigationItem property is created when the viewController is initialised, hence you can assign to the navigationItem.title immediately.


Swift code

let detailVC = DetailView(nibName: nil, bundle: nil)
detailVC.userName = userList.objectAtIndex(indexPath.row) as? NSString

and

class DetailView: UIViewController {

    @IBOutlet var userNameLbl: UILabel
    var userName:NSString?

    override func viewDidLoad() {
        super.viewDidLoad()
        self.userNameLbl.text = self.userName
    }    
} 
like image 132
Ashley Mills Avatar answered Oct 26 '22 08:10

Ashley Mills


Are you mixing UIViews and UIViewControllers? You should have a DetailViewController class that inherits from UIViewController or some sublcass (like UITableViewController); it will have DetailViewController.m and DetailViewController.h files to declare and define your class. It should have a corresponding nib that defines the UIView that the UIViewController loads; it will have a DetailView.xib file.

You can't assign the value to the UILabel directly because UIView hasn't been loaded at the time you need to assign the user name value.

In order to do what you want, you should declare a public property (userName) to "push" the value onto the detail view controller from the master controller. Once the detail view is loaded, it can assign the value from the property to the label and nav bar.

In your master view controller (UITableViewController):

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    DetailViewController *detailVC = [[DetailView alloc] initWithNibName:@"DetailView" bundle:nil];

    detailVC.userName = [userList objectAtIndex:indexPath.row];

    [self.navigationController pushViewController:detailVC animated:YES];

    [detailVC release];
}

In your detail view controller:

DetailViewController.h

@property (retain) NSString* userName;
@property (nonatomic, retain) IBOutlet UILabel *userNameLbl;

DetailViewController.m

@synthesize userName;
@synthesize userNameLbl;

-(void) viewDidLoad {
    [super viewDidLoad];
    self.userNameLbl.text = self.userName;
    self.navigationItem.title = self.userName;
}
like image 25
nekno Avatar answered Oct 26 '22 07:10

nekno