我想我正在关注代表团的运作方式,这是我遵循的教程 http://www.iphonedevsdk.com/forum/iphone-sdk-tutorials/81489-how-why-implement-delegation-pattern.html,但我在某个地方搞砸了。我期待着 NSLog 的代表,但事实并非如此。谁能找出我错过了什么或做错了什么?
My MainViewController.h:
@interface MainViewController : UITableViewController < AddClassDelegate >
MainViewController.m:
- (void)cancelAddingClass {
NSLog(@"Canceled Yo");
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
/*
When a row is selected, the segue creates the detail view controller as the destination.
Set the detail view controller's detail item to the item associated with the selected row.
*/
if ([[segue identifier] isEqualToString:@"addClassSegue"]) {
UINavigationController *nc = (UINavigationController *)segue.destinationViewController;
AddClassViewController *addClassVC = (AddClassViewController *)[nc.viewControllers objectAtIndex:0];
addClassVC.delegate = self;
}
我的模态视图控制器添加类ViewController.h:
@protocol AddClassDelegate <NSObject>
- (void)cancelAddingClass;
@end
@interface AddClassViewController : UITableViewController
@property (weak, nonatomic) id< AddClassDelegate > delegate;
- (IBAction)cancelButtonPressed:(id)sender;
添加类ViewController.m:
@synthesize delegate;
- (IBAction)cancelButtonPressed:(id)sender {
[self.delegate cancelAddingClass];
}
cancelButtonPressed:
连接到情节提要中模态视图的取消按钮。
您的代码看起来不错,这表明问题出在我们看不到的地方。我的猜测是:
AddClassViewController *addClassVC = [segue destinationViewController];
addClassVC.delegate = self;
NSLog(@"segued");
您是否将模态视图控制器嵌入到导航控制器中?如果是这样,destinationViewController 会为您提供导航控制器,而不是 AddClassViewController。检查调试器中 addClassVC 实际是什么类。
如果它是一个导航控制器,没问题,你只需要使用.viewControllers
财产。为了更容易理解,分成几行:
UINavigationController *nc = (UINavigationController *)segue.destinationViewController;
AddClassViewController *addClassVC = (AddClassViewController *)[nc.viewControllers objectAtIndex:0];
addClassVC.delegate = self;
您可以用更少的行来完成此操作,但这是一团混乱的转换和嵌套括号,这更难以调试。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)