Implementing a basic callback in Objective C
How does one implement a raw callback in objective C?
I just want to notify a ViewController wh开发者_StackOverflow中文版en one of my UITableViewCell objects(custom cell) detects touch.
I need to notify my RootViewController so that it can create an object of another ViewController and push it on the navigation stack.
You can use a custom init method like below for secondVC and store the _sender in global or class variable. like
id sender;
- (id)initWithSender:(id)_sender { self = [super init]; if (self) { sender=_sender; } return self; }
from RootVC initialize secondvc as follows and define a method named -(void) touchDetected; in rootvc.
secondvc=[[SecondVC alloc] initWithSender:self]; [[self navigationController] pushViewController: secondvc animated:YES];
when the touch is detected in secondvc call, this will notify your rootvc that the touch is detected in secondvc.
[sender touchDetected];
You can probably override the Table view delegate method didSelectRowAtIndex: to detect the touch.Then u can push the view controller using pushViewController: method of Navigation controller
If the cell object you are referring is a button then you can do by [button addTarget:self action:@selector(methodYouWantToInvoke)];
You need to set your ViewController
to be the delegate of your table view, then implement the tableView:didSelectRowAtIndexPath:
method. This is sample code directly from Apple.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
BATTrailsViewController *trailsController = [[BATTrailsViewController alloc] initWithStyle:UITableViewStylePlain];
trailsController.selectedRegion = [regions objectAtIndex:indexPath.row];
[[self navigationController] pushViewController:trailsController animated:YES];
[trailsController release];
}
精彩评论