我想爲我自己的委託方法,我在forDelegate.h文件中聲明'Mydelegate'協議。如何製作自己的委託方法?
@protocol Mydelegate <NSObject>
-(void)show:(NSString *)msg;
@end
然後在我的第一個的viewController我做財產申報this.I的方法clickMe這是按鍵記者電話時,按鈕出現在我的firstviewcontroller的.xib文件。
@class SecondViewController;
@interface ViewController : UIViewController
{
id <Mydelegate> delegate1;
SecondViewController *secondController;
}
@property (assign) id <Mydelegate> delegate1;
-(IBAction)clickMe:(id)sender;
@end
,然後我採用在我的第二視圖控制器此協議和定義此協議的
SecondViewController.h文件的方法後
@class ViewController;
@interface SecondViewController : UIViewController <Mydelegate>
{
ViewController *viewController;
}
SecondViewController.m文件
-(void)show:(NSString *)msg
{
NSLog(@"in delegate mathod");
}
-(void)viewDidLoad
{
viewController =[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
viewController.delegate1=self;
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
程序正在運行,但Mydelegate方法'show'不是調用。
第一件事你打電話給你的演出方法我在無效的情況下,你應該使用一些地方。 – vishiphone
我將編輯您的代碼並在下面發佈,只需查看它。 – vishiphone
在SecondViewController中是否定義了「 - (void)show:(NSString *)msg」?並且您還在SecondViewController中使用了delegate1。 –