Navigation Controller View Handling
I have a navigation view controller that will show the table view of my开发者_如何学运维 application, but I can't get it to move to a new view when I tap an entry in the table. How do I link a new view to an element in the parent view?
You have a navigation view controller
?
You either have a navigation controller or a view controller.
A navigation controller has a stack of view controllers and pushes and pops between them.
In regards to your question you need to define the tableview's delegate as self
. If you're subclassing UITableViewController
then you don't have to worry about this.
Then implement this method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// here is where you can "push" a new view controller
MyNextViewController *nextVC = [[MyNextViewController alloc] initWithNibName:@"MyNextViewController" bundle:nil];
[nextVC setTitle:@"Next View Controller"];
[self.navigationController pushViewController:nextVC animated:YES]; // this is where the next page is "pushed"
// remember to release
[nextVC release];
}
So in the code above we're using the delegate method didSelectRowAtIndexPath
which gets called when the user "clicks" a table view cell.
You can use the indexPath
object to find out which row was tapped. int rowIndex = [indexPath row];
will get you the row index of the cell that was tapped.
精彩评论