委托方法不会被解雇 - iOS

时间:2013-05-09 04:10:05

标签: ios delegates

我有一个tabcontroller,它有两个部分。其中一个是UserInfo,另一个是UserAvatar。一旦用户输入他的信息,然后点击完成按钮并转到主视图,或者用户也可以选择他的个人资料头像然后点击点击按钮然后可以转到mainView。但是,虽然我使用调试模式的代码运行,但它会运行到完成按钮,但在我的userinfo视图中,不会触发delegeta方法!

在我的主头文件中,我声明我的协议如下:

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

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

在我的main.m文件中,我的委托方法

- (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;  在以下回复     在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;

以下是不调用我的委托方法.. 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];

}

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

检查你错过了。它应该是这样的,比如说WeatherAPI class:

@protocol WeatherAPIDelegate <NSObject>

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

@end

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

delegate

中调用WeatherAPI这样的方法
[delegate weatherData:dictWeatherInfo];

现在执行说ViewController

@interface ViewController : UIViewController<WeatherAPIDelegate>
{

}

WeatherAPIDelegate中实施ViewController方法:

#pragma mark -
#pragma mark WeatherAPIDelegate Method

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

}

WeatherAPI

中初始化ViewController
WeatherAPI *objWeatherAPI = [[WeatherAPI alloc]init];
[objWeatherAPI setDelegate:self];