ios - 永远不会调用委托(delegate)方法

标签 ios delegates

我是 IOS 的新手,我正在使用委托(delegate)来调用父 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?"];
    }

parent :

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
}

请注意,我的父 View Controller 嵌入在导航 Controller 中。

永远不会调用委托(delegate)方法。尝试调试,但没有成功。

最佳答案

您在 viewDidLoad 中创建的 View Controller 与您将要转到的 View Controller 不同。这是将在方法结束时释放的另一个实例。您需要在 prepareForSegue 中访问正确的实例并在那里设置委托(delegate)。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"SegueRegistrationUserAction"]) {
        [(ViewControllerRegistration *)segue.destinationViewController setDelegate:self];
    }
}

关于ios - 永远不会调用委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32589962/

相关文章:

iphone - 获取对 UIApplication 委托(delegate)的引用

ios - 未调用 BEMCheckBox 委托(delegate)

iOS 模拟器、CLLocationManager、startUpdatingLocation 和 locationManager :didUpdateLocations

objective-c - iOS - 如何访问设备的文件库?

iOS ARC 应用程序和一行或两行对象初始化

c# - 委托(delegate)操作 : new Action or casting Action?

.net - 使用反射添加事件处理程序时抛出 TargetException

ios - 找不到捕获的视频 URL

ios - aws dynamodb 如何在 ios swift 中使用对象映射器和批处理获取

delegates - “Xcode 找不到 "UIViewControllerAnimatedTransitioning"的协议(protocol)声明”