英文:
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 <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");
}
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
我刚刚尝试了你的代码,它运行得很好。
我改变的唯一一件事是在ShopProfileController
的viewDidLoad()
方法的末尾添加了以下代码行:
[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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论