I'm working with a view controller that I created (MyViewController) that has a UIButton called开发者_开发技巧 "button", this is the code that I've been trying to get it to work:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
MyViewController *myViewController=[[[MyViewController alloc] init] autorelease];
[myViewController.button addTarget:self action:@selector(action) forControlEvents:UIControlEventTouchUpInside];
return footerController.view;
}
I'm not sure what's wrong with it, but apparently I can fix it if I do this:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
MyViewController *myViewController=[[[MyViewController alloc] init] autorelease];
[myViewController.view addSubview:nil];
[myViewController.button addTarget:self action:@selector(action) forControlEvents:UIControlEventTouchUpInside];
return footerController.view;
}
Can someone please tell me why is this happening? And is there a way around this, because I don't think it's ok to do that..
The nib is not loaded until it is needed to save memory, as signaled by the view
property being accessed. If possible, it would be better to do that sort of initialization in MyViewController's viewDidLoad
method.
What Anomie said is right.. I found the way around it.. I can't access the button directly with myViewController.button
, instead I have to use the method: [myViewController.view viewWithTag:buttonTag]
. That way I'm making sure the button has already been initialized.
精彩评论