【发布时间】:2010-01-27 22:58:10
【问题描述】:
我只是想构建一个简单的委托,它编译成功,但它现在似乎没有做任何事情。 请在下面查看我的代码,FirstViewContoller 和 SecondViewController 都有单独的 xib 文件,当然,SecondViewController 有一个连接到 myMethod 的按钮。
几个月来,我一直在学习有关 Objective c 和 iPhone SDK 的方法,我一直在使用一本书和 lynda.com 教程来帮助,实际上我使用 xcode 模板复制了大部分代码实用程序应用程序,我相信我了解这里发生了什么,但是我错过了什么,因为按钮没有响应。
如果您需要更多信息,请询问。
非常感谢, 克里斯
SecondViewController.h
#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
SecondViewController.m
@implementation SecondViewController
@synthesize delegate;
-(IBAction) myMethod
{
[self.delegate viewControllerDidFinish:self];
}
//
@end
FirstViewController.h
@interface FirstViewController : UIViewController <SecondViewControllerDelegate>{
//
@end
FirstViewController.m
@implementation FirstViewController
-(void)viewControllerDidFinish:(SecondViewControllerDelegate *)controller
{
NSLog(@"delegate is being used");
}
//
@end
编辑 - 这是一篇旧帖子,但只是为了澄清一下,我只是忘记在 FirstViewController 中分配 SecondViewController 委托
【问题讨论】:
-
我没有看到您分配 SecondViewController 的委托属性,您必须在创建时这样做?向 nil 发送消息是合法的...
-
你能举个例子吗?这可能是缺少的
-
好的,我知道了,非常感谢。分配委托属性是有意义的,我在查看实用程序模板时忽略了这部分,因为我没有做任何反面视图工作。我现在明白仅声明委托是不够的,感谢!
标签: iphone objective-c xcode delegates