I have a UITableView
and I add cells with a button on the UIViewController.
If the cells are tapped, you are brought to a new viewController
which has a picker.
How can I set the the selected cell's text as title for next viewController?
I have a UITableView
and I add cells with a button on the UIViewController.
If the cells are tapped, you are brought to a new viewController
which has a picker.
How can I set the the selected cell's text as title for next viewController?
IF YOU WANT TO SEND SELECTED ROW's TITLE TO NEXT VIEW CONTROLLER (push)
Create property (say title) in your new view controller's .h file to hold title of selected row and synthesize title property in your new view controller's .m file.
update didSelectRowAtIndexPath
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
YourNewViewController *vc= [[YourNewViewController
alloc]initWithNibName:@"YourNewViewControllerNIB" bundle:nil];
vc.title=[[titleArray objectAtIndexPath]indexPath.row];
[self.navigationController pushViewController:vc animated:YES];
}
now you have selected row's title in your new view controller.
for setting this title as navigation bar title
write this code to viewDidLoad
of your new view controller.
self.navigationItem.title=title.
IF YOU WANT TO SEND SELECTED ROW's TITLE TO PREVIOUS VIEW CONTROLLER (POP)
for this you have to use delegate pattern and you have to make custom protocols.
follow this. - Passing Data between View Controllers
// This is how to get selected cell
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[tableView indexPathForSelectedRow]];
cell.textLabel.text = @"Some title";
In the didSelectRowAtIndexpath
method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *Cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *NewTitle = @"Add your new title here";
Write this code in either button's method OR your picker's method
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[tableView indexPathForSelectedRow]];
cell.textLabel.text = self.MyButtonName.titleLabel.text;
[self.tableView reloadData];
Are you looking for the selected row?
- (void)tableView:(UITableView *)_tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [_tableView cellForRowAtIndexPath:indexPath];
[cell.textLabel setText:@"Your Title"];
[_tableView reloadData];
}