Я новичок в IOS, и я работал с делегатами для вызова метода в родительском ViewController.Метод делегатов, который никогда не называется
ViewControllerRegistration.h
@protocol RegistrationViewControllerDelegate;
@interface ViewControllerRegistration : UIViewController
@property (nonatomic, weak) id<RegistrationViewControllerDelegate> delegate;
- (IBAction)registerNewUser:(id)sender;
@end
// 3. Definition of the delegate's interface
@protocol RegistrationViewControllerDelegate <NSObject>
-(void)loginResult:(NSString*)loginData;
@end
ViewControllerRegistration.m
- (void)registerNewUser:(id)sender {
id<RegistrationViewControllerDelegate> strongDelegate = self.delegate;
// Our delegate method is optional, so we should
// check that the delegate implements it
if ([strongDelegate respondsToSelector:@selector(loginResult:)]) {
[strongDelegate loginResult: @"@WHY YOU NOT BEING CALLED?"];
}
Родители:
ViewController.h
#import "ViewControllerRegistration.h"
@interface ViewController : UIViewController <GPPSignInDelegate,FBSDKLoginButtonDelegate,RegistrationViewControllerDelegate>
@end
ViewController.m
@implementation ViewController
@synthesize signInButton;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
ViewControllerRegistration *detailViewController = [[ViewControllerRegistration alloc] init];
// Assign self as the delegate for the child view controller
detailViewController.delegate = self;
}
//Local Strategy
- (IBAction)navigateToLocalSignUpNavBtn:(id)sender {
[self performSegueWithIdentifier:@"SegueRegisterUserAction" sender:self];
}
// Implement the delegate methods for RegistrationViewControllerDelegate
-(void)loginResult:(NSString*)loginData{
NSLog(@"I am called");
//^Above is never printed. That means delegate method is never called
}
Пожалуйста, обратите внимание, что мой родитель-контроллер встроен в контроллер навигации.
Метод делегата никогда не вызывается. Пробовал отлаживать, но напрасно.
Проблема заключается в том, что создаваемый вами 'detailViewController' - это не то же самое, что вы получите после' performSegueWithIdentifier'. Итак, для текущего видимого делегата 'ViewControllerRegistration' будет nil. Попытайтесь использовать '- prepareForSegue: sender:' и получите 'destinationController', чтобы установить свойство делегирования. –