[英]protocol method is not being called by the delegate
I have implemented protocol in my app, 我已经在我的应用中实现了协议,
I have declared protocol method in loginViewController
and i am calling protocolMethod
from confirmViewController
我已经在loginViewController
声明了协议方法,并且我正在从confirmViewController
调用protocolMethod
Here is my code snippet: loginViewController.h
这是我的代码段: 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];
}
Code for confirmViewController.h
用于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];
}
Where i am doing mistake? 我在哪里做错了?
Please help and thanks in advance! 请帮助并提前致谢!
In this lines:- 在这行中:
confirmViewController *obj = [confirmViewController new];
obj.delegate=self;
[self performSegueWithIdentifier:@"confirmloginsegue" sender:self];
--- You are loosing the object here
In your attribute inspector give the identifier for that confirmViewController, now push the vc and set the delegate for the object confirmViewController like this:- 在属性检查器中,为该ConfirmViewController提供标识符,现在推送vc并设置对象ConfirmViewController的委托,如下所示:-
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
ConfirmViewController *confirmViewController = [storyboard instantiateViewControllerWithIdentifier:@"ConfirmViewController"];
confirmViewController.delegate=self;
[self.navigationController pushViewController:confirmViewController animated:YES];
The delegating class does not exist for long enough to matter: 委托类的存在时间不足以引起关注:
- (IBAction)loginBtnClicked:(id)sender {
confirmViewController *obj = [confirmViewController new];
obj.delegate=self;
[self performSegueWithIdentifier:@"confirmloginsegue" sender:self];
// obj is destroyed here
}
You need to implement prepareForSegue
and set delegate
there eg 您需要实现prepareForSegue
并在那里设置delegate
,例如
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
confirmViewController *destViewController = (confirmViewController *)segue.destinationViewController;
destViewController.delegate=self;
}
for further detail check this link:- http://www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/ 有关更多详细信息,请检查此链接:-http: //www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/
You have to set delegate there. 您必须在那里设置代表。 please,make some correction in your code like: 请在您的代码中进行一些更正,例如:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
confirmViewController *destViewController = (confirmViewController *)segue.destinationViewController;
destViewController.delegate=self;
} }
You shoul use preparefrosegue
to set your delegate object. 您应该使用preparefrosegue
设置您的委托对象。 so your code should be like this, 所以你的代码应该是这样的,
- (IBAction)loginBtnClicked:(id)sender {
[self performSegueWithIdentifier:@"confirmloginsegue" sender:self];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
confirmViewController *obj = [confirmViewController new];
//or
confirmViewController *obj = [segue destinationViewController];
obj.delegate=self;
}
hope this will help ;) 希望这会有所帮助;)
If u are using View controller 如果您正在使用View Controller
SettingsHubViewController *settingVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"yourStoryBoardIdentifier"];
settingVC.delegate = self;
If u are using XIB load the NIB after that set the settingVC.delegate = self;
如果您正在使用XIB,则在设置settingVC.delegate = self;
之后加载NIB settingVC.delegate = self;
its working for me (I created OverLay view and loaded in main class) 它为我工作(我创建了OverLay视图并加载到主类中)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.