How to pass data to detail view after being selected in a table view?
Solution 1
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
}
}
Solution 2
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;
}
Related videos on Youtube
tarheel
Business Intelligence professional with experience on SQL Server, Snowflake, Google BigQuery, Terada, Oracle, and IBM environments. Always learning more code, and more ways to solve problems creatively.
Updated on June 05, 2022Comments
-
tarheel almost 2 years
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
-
tarheel over 12 yearsConceptually, I definitely agree with your approach, but I am gonna need a little more help(im a noob) on the implementation. My understanding is that the above line would go inside of my didSelectRowAtIndexPath: method in the TableView, and then(this is where I get fuzzy) the actual implementation of the initWithTitle:text: method be in the DetailView. Right?
-
Bourne over 12 years+1 for this approach. I would recommend this over the other approaches mentioned simply because of its simplicity and elegance.
-
tarheel over 12 years@lxt I would love to see more on this solution, but I had to give the credit to AshleyMills because that is the solution I went with in my app. Thanks Everyone!