You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无StoryBoard环境下两个ViewController间代理未触发的解决求助

Hey there! Let's fix that delegate method not firing issue for you—this is super common when working without Storyboards, and it usually boils down to a few easy-to-miss steps. I'll walk you through the corrected code for all three files, plus point out the key mistakes that might have been blocking your proxy call.


Step 1: Correctly Declare the Delegate Protocol (ViewController2.h)

First, we need a properly defined protocol with a weak delegate property (critical to avoid retain cycles):

#import <UIKit/UIKit.h>

// Protocol naming follows iOS conventions: ClassName + Delegate
@protocol ViewController2Delegate <NSObject>
// Define your message-passing method here
- (void)viewController2:(ViewController2 *)sender didSendMessage:(NSString *)message;
@end

@interface ViewController2 : UIViewController

// Weak modifier prevents circular references between VCs
@property (nonatomic, weak) id<ViewController2Delegate> delegate;

// A sample method to trigger message sending (you can tie this to a button action)
- (void)triggerMessageSend;

@end

Step 2: Implement Delegate Method Calls (ViewController2.m)

In the second view controller, we'll add logic to check if the delegate exists and implements the method before calling it:

#import "ViewController2.h"

@implementation ViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.systemBlueColor;

    // Add a test button to trigger message sending
    UIButton *sendBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    sendBtn.frame = CGRectMake(100, 200, 200, 50);
    [sendBtn setTitle:@"Send Message Back" forState:UIControlStateNormal];
    [sendBtn addTarget:self action:@selector(triggerMessageSend) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sendBtn];
}

- (void)triggerMessageSend {
    NSString *message = @"Hey, this is from ViewController2!";
    
    // Critical check: Only call the delegate method if it's implemented
    if ([self.delegate respondsToSelector:@selector(viewController2:didSendMessage:)]) {
        [self.delegate viewController2:self didSendMessage:message];
    }

    // Pop back to the first VC after sending
    [self.navigationController popViewControllerAnimated:YES];
}

@end

Step 3: Set Up the Delegate in the First View Controller (ViewController.m)

Now we need to make the first VC conform to the protocol and set itself as the delegate when pushing VC2:

First, update ViewController.h (or use a class extension in .m)

#import <UIKit/UIKit.h>
#import "ViewController2.h"

@interface ViewController : UIViewController <ViewController2Delegate>

@end

Then implement the logic in ViewController.m

#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.systemBackgroundColor;

    // Add a button to navigate to VC2
    UIButton *navBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    navBtn.frame = CGRectMake(100, 200, 200, 50);
    [navBtn setTitle:@"Go to VC2" forState:UIControlStateNormal];
    [navBtn addTarget:self action:@selector(pushToVC2) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:navBtn];
}

- (void)pushToVC2 {
    ViewController2 *vc2 = [[ViewController2 alloc] init];
    // THIS IS THE MOST COMMON MISTAKE: Forgetting to set the delegate!
    vc2.delegate = self;
    [self.navigationController pushViewController:vc2 animated:YES];
}

// Implement the delegate method to receive messages
- (void)viewController2:(ViewController2 *)sender didSendMessage:(NSString *)message {
    NSLog(@"Received message: %@", message);
    // Add your UI update or logic here
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Message Received" message:message preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
}

@end

Common Reasons Your Delegate Method Wasn't Firing
  1. Forgot to set the delegate: Without Storyboard segues, you have to manually set vc2.delegate = self when initializing VC2—this is the #1 culprit.
  2. Used strong instead of weak for the delegate property: A strong delegate creates a retain cycle, leading to VC2 not being released properly and the delegate becoming invalid.
  3. Didn't conform to the protocol: If your first VC doesn't declare it follows ViewController2Delegate, the compiler might warn you, and runtime calls will fail.
  4. No check for method implementation: While this doesn't break the call, it can cause crashes if the delegate doesn't implement the method—always add that respondsToSelector check.

内容的提问来源于stack exchange,提问作者JinseonKim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:05:25