我已经在我的应用中实现了协议,
我已经在loginViewController
中声明了协议方法,并且正在从protocolMethod
调用confirmViewController
这是我的代码段:loginViewController.h
@protocol FirstControllerDelegate<NSObject>
@required
-(void)protocolMethod;
@end
@interface loginViewController : UIViewController<FirstControllerDelegate>
@end
loginViewController.m
- (IBAction)loginBtnClicked:(id)sender {
confirmViewController *obj = [confirmViewController new];
obj.delegate=self;
[self performSegueWithIdentifier:@"confirmloginsegue" sender:self];
}
-(void)protocolMethod{
NSLog(@"--- This is not callled ---");
[self dismissViewControllerAnimated:NO completion:nil];
}
代码为
confirmViewController.h
@interface confirmViewController : UIViewController
@property (nonatomic, assign) id <FirstControllerDelegate> delegate;
@end
confirmViewController.m
(IBAction)continueClicked:(id)sender {
[_delegate protocolMethod]; //Calling protocol method...
[self dismissViewControllerAnimated:YES completion:nil];
}
我在哪里做错了?
请帮助并提前致谢!
最佳答案
错误的方法和委托初始化
在这行中:
confirmViewController *obj = [confirmViewController new];
obj.delegate=self;
[self performSegueWithIdentifier:@"confirmloginsegue" sender:self];
--- You are loosing the object here
解
在属性检查器中,为该ConfirmViewController提供标识符,现在按下vc并设置对象ConfirmViewController的委托,如下所示:-
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
ConfirmViewController *confirmViewController = [storyboard instantiateViewControllerWithIdentifier:@"ConfirmViewController"];
confirmViewController.delegate=self;
[self.navigationController pushViewController:confirmViewController animated:YES];
关于ios - 委托(delegate)未调用协议(protocol)方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36710281/