自定义委托方法在 objective-c 中不起作用

Custom Delegate method not working in objective-c

// userprofileform.h(viewcontroller)

@class UserProfileForm;             
@protocol UserProfileFormDelegate <NSObject>
- (void) UserProfileFormDelegateReload: (UserProfileForm *) sender;

@end

@interface UserProfileForm : OTSFormViewController <UIActionSheetDelegate>
@property(nonatomic,assign) id <UserProfileFormDelegate> delegate;

@end

//userprofileform.m

#import "UserProfileForm.h"

@implementation UserProfileForm

- (void)endFlow:(id)sender {
     [self.delegate UserProfileFormDelegateReload:self];
     [self.navigationController popViewControllerAnimated:YES];
}

//shopprofilecontroller.h

#import "UserProfileForm.h"

@interface ShopProfileController : OTSViewController <UserProfileFormDelegate>

@end

//shoprofilecontroller.m

@implementation ShopProfileController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationItem.title = @"Business";

    UserProfileForm * userProfileForm = [[UserProfileForm alloc] init];
    userProfileForm.delegate = self;
}

-(void)UserProfileFormDelegateReload:(UserProfileForm *)sender{
    NSLog(@"delegates fired");
}

但是在弹出 UserProfileForm 时不会调用委托方法。 我不知道为什么它不开火。我试过 NSNotifications 也没有触发。

我刚刚尝试了您的代码,它运行良好。 我唯一改变的是在 ShopProfileControllerviewDidLoad() 末尾我放了以下行:

[userProfileForm endFlow:self];

因此您的委托实现应该是正确的。 在您的 endFlow() 方法中设置一个断点,我敢打赌它不会被调用。