自定义委托方法在Objective-C中不起作用。

huangapple go评论66阅读模式
英文:

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");
}

请注意,我已经删除了HTML实体编码(例如<和>),以使代码更加清晰。

英文:

// userprofileform.h(viewcontroller)

@class UserProfileForm;             
@protocol UserProfileFormDelegate &lt;NSObject&gt;
- (void) UserProfileFormDelegateReload: (UserProfileForm *) sender;

@end

@interface UserProfileForm : OTSFormViewController &lt;UIActionSheetDelegate&gt;
@property(nonatomic,assign) id &lt;UserProfileFormDelegate&gt; delegate;

@end

//userprofileform.m

#import &quot;UserProfileForm.h&quot;

@implementation UserProfileForm

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

//shopprofilecontroller.h

#import &quot;UserProfileForm.h&quot;

@interface ShopProfileController : OTSViewController &lt;UserProfileFormDelegate&gt;

@end

//shoprofilecontroller.m

@implementation ShopProfileController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationItem.title = @&quot;Business&quot;;

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

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

but the delegate method is not called upon popping the UserProfileForm.
I don't know why is it not firing. I have tried NSNotifications and it did not fire too.

答案1

得分: 0

我刚刚尝试了你的代码,它运行得很好。
我改变的唯一一件事是在ShopProfileControllerviewDidLoad()方法的末尾添加了以下代码行:

[userProfileForm endFlow:self];

所以你的代理实现应该是正确的。
endFlow()方法中设置一个断点,我敢打赌它没有被调用。

英文:

I just tried your code and it worked fine.
The only thing I changed was at the end of viewDidLoad() of ShopProfileController I put the following line:

[userProfileForm endFlow:self];

So your delegate implementation should be correct.
Set a breakpoint within your endFlow() method, I bet its not getting called.

huangapple
  • 本文由 发表于 2020年1月6日 20:16:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611961.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定