我只是想構建一個簡單的委託,它編譯成功,但它似乎沒有做任何事情。 請檢查我的代碼下面,FirstViewContoller和SecondViewController都有單獨的xib文件,SecondViewController有一個按鈕連接到myMethod當然。爲什麼我的自定義iPhone代理不工作?
我一直在學習圍繞目標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
編輯 - 這是一個老的文章,但只是爲了澄清,我只是忘了分配SecondViewController代表在FirstViewController
我沒有看到你指派的SecondViewController的委託屬性,你必須在創建時做到這一點?發送消息給零是合法的... – 2010-01-27 23:09:37
你能舉個例子嗎?這可能是缺少的東西 – Chris 2010-01-27 23:44:24
好吧,我明白了,非常感謝。分配代理屬性是有道理的,我在查看Utility模板時忽略了這部分,因爲我沒有做任何翻轉視圖工作。我現在明白宣佈代表是不夠的,感激它! – Chris 2010-01-27 23:49:05