1

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

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

@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> 

翻转委托;在以下响应中 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];

}
4

2 回答 2

1

检查你是否错过了。例如,它应该像这样说WeatherAPI类:

@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
{

}

初始化WeatherAPIViewController

WeatherAPI *objWeatherAPI = [[WeatherAPI alloc]init];
[objWeatherAPI setDelegate:self];
于 2013-05-09T04:36:01.960 回答
1

这是因为您flipDelegate已经nil定义了委托方法main.m,我认为其中没有任何实现您的协议的类。

于 2013-05-09T04:20:31.307 回答