简体   繁体   中英

Delegate method is not fired- iOS

I have one tabcontroller and it has two segments. One of them is UserInfo, another one is UserAvatar. Once user put into his info, then click on done button and go to mainview, or user could also choose his profile avatar and then click on click button and then could go to mainView. However, evethough I run by code with debug mode, it runs into done button, but in my userinfo view, delegeta method is not fired!

in my Main header file, I declared my protocol is as follow:

@protocol MyUserInfoAvatarViewControllerDelegate <NSObject>
@required
-(void)userInfoEditingDone:(id)sender;
@end

@protocol MyUserInfoSettingsViewControllerDelegate <NSObject>
@required
-(void)userInfoEditingDone:(id)sender;
@end

in my main.m file, my delegate method

- (void)userInfoEditingDone:(id)sender {
NSLog(@"Hello");
}

MyUserInfoAvatarViewController.h

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

@interface UserInfoAvatarViewController : UIViewController <ThumbImageViewDelegate > {
    id __unsafe_unretained flipDelegate;
}

@property (nonatomic,unsafe_unretained) id <MyUserInfoAvatarViewControllerDelegate> 

flipDelegate; in the following responding in MyUserInfoAvatarViewController.m

- (IBAction)doneButtonPressed:(id)sender {
    // Tell the root view to flip back over to the main view
    [self.flipDelegate userInfoEditingDone:self];
}

MyUserInfoSettingsViewController.h

 #import <UIKit/UIKit.h>
    #import "NEATMainViewController.h"
    @interface UserInfoSettingsViewController : UIViewController {
        id __unsafe_unretained flipDelegate;
    }

    @property (nonatomic,unsafe_unretained) id <MyUserInfoSettingsViewControllerDelegate> flipDelegate;

in the following is not calling my delegate method.. MyUserInfoSettingsViewController.m

- (void)viewDidLoad {

    [super viewDidLoad];

    UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(done:)];
    self.navigationItem.rightBarButtonItem = btn;
}
- (void)done:(id)sender {

    // Tell the root view to flip back over to the main view
    [self.flipDelegate userInfoEditingDone:self];

}

这是因为您的flipDelegate nil ,而且您在main.m定义了委托方法,我认为它没有任何实现您协议的类。

Check were you missed. It should like this for example say WeatherAPI class :

@protocol WeatherAPIDelegate <NSObject>

 -(void)weatherData:(NSMutableDictionary *)dictData;

@end

@interface WeatherAPI : NSObject
{
  id <WeatherAPIDelegate>delegate;
}
@property(nonatomic,strong)id <WeatherAPIDelegate> delegate;
@end 

Call delegate method like this in WeatherAPI :

[delegate weatherData:dictWeatherInfo];

Now implement in say ViewController :

@interface ViewController : UIViewController<WeatherAPIDelegate>
{

}

Implement WeatherAPIDelegate method in ViewController :

#pragma mark -
#pragma mark WeatherAPIDelegate Method

-(void)weatherData:(NSMutableDictionary *)dictData
{

}

When intializing WeatherAPI in ViewController

WeatherAPI *objWeatherAPI = [[WeatherAPI alloc]init];
[objWeatherAPI setDelegate:self];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM