[英]protocol method is not being called by the delegate
我已經在我的應用中實現了協議,
我已經在loginViewController
聲明了協議方法,並且我正在從confirmViewController
調用protocolMethod
這是我的代碼段: 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];
委托類的存在時間不足以引起關注:
- (IBAction)loginBtnClicked:(id)sender {
confirmViewController *obj = [confirmViewController new];
obj.delegate=self;
[self performSegueWithIdentifier:@"confirmloginsegue" sender:self];
// obj is destroyed here
}
您需要實現prepareForSegue
並在那里設置delegate
,例如
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
confirmViewController *destViewController = (confirmViewController *)segue.destinationViewController;
destViewController.delegate=self;
}
有關更多詳細信息,請檢查此鏈接:-http: //www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/
您必須在那里設置代表。 請在您的代碼中進行一些更正,例如:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
confirmViewController *destViewController = (confirmViewController *)segue.destinationViewController;
destViewController.delegate=self;
}
您應該使用preparefrosegue
設置您的委托對象。 所以你的代碼應該是這樣的,
- (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;
}
希望這會有所幫助;)
如果您正在使用View Controller
SettingsHubViewController *settingVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"yourStoryBoardIdentifier"];
settingVC.delegate = self;
如果您正在使用XIB,則在設置settingVC.delegate = self;
之后加載NIB settingVC.delegate = self;
它為我工作(我創建了OverLay視圖並加載到主類中)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.