我只是尝试构建一个简单的委托,它可以成功编译,但现在似乎什么都做不了。
请查看下面的代码,FirstViewController和SecondViewController都有单独的xib文件,SecondViewController有一个连接到myMethod的按钮。
几个月来,我一直在学习objective c和iPhone SDK,我一直在使用一本书和lynda.com教程来辅助,我实际上使用xcode模板复制了一个实用程序应用程序的大部分代码,我相信我理解这里发生了什么,但我是否遗漏了一些东西,因为按钮没有响应。
如果您需要更多信息,请询问。
克里斯
#import <UIKit/UIKit.h>
@protocol SecondViewControllerDelegate;
@interface SecondViewController : UIViewController {
id <SecondViewControllerDelegate> delegate;
}
@property (nonatomic, assign) id <SecondViewControllerDelegate> delegate;
-(IBAction) myMethod;
@end
@protocol SecondViewControllerDelegate
-(void)viewControllerDidFinish:(SecondViewController *)controller;
@end
@implementation SecondViewController
@synthesize delegate;
-(IBAction) myMethod
{
[self.delegate viewControllerDidFinish:self];
}
//
@end
@interface FirstViewController : UIViewController <SecondViewControllerDelegate>{
//
@end
FirstViewController.m
@implementation FirstViewController
-(void)viewControllerDidFinish:(SecondViewControllerDelegate *)controller
{
NSLog(@"delegate is being used");
}
//
@end
编辑
-这是一篇老文章,但为了澄清,我只是忘记在FirstViewController中分配SecondViewController委托