ModalViewController委托混淆

时间:2010-09-28 13:48:23

标签: iphone objective-c ipad

我需要提出一个模态视图控制器,并在被解雇或通知我需要解雇它时得到通知,看这里我仍然感到困惑:

http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html#//apple_ref/doc/uid/TP40007457-CH111-SW14

我有我的mainViewController和myModalView控制器,我有以下代码需要实现,但不确定在哪里 - 首先代表protocal:

@protocol DataSyncDelegate <NSObject>
-(void) doneWithSync;
@end

哪个controller.h进入?我假设我的modalViewController.h

第二是我的实施:

-(void) doneWithSync {
    [self dismissModalViewControllerAnimated:YES];
}

哪个controller.m会这样做?我假设我的mainViewController.m

我还有需要添加的委托属性:

id delegate;
@property (nonatomic, retain) id delegate;

哪个controller.m需要进入?我假设我的modalViewController.h

以下是我如何从MainViewController呈现modalViewController:

DataSyncViewController *dataSyncViewController = [[DataSyncViewController alloc] initWithOptions:FALSE];
dataSyncViewController.delegate = self;
[self presentModalViewController:dataSyncViewController animated:NO];
[dataSyncViewController release];

截至目前,这给了我以下错误:

-[DataSyncViewController setDelegate:]: unrecognized selector sent to instance 0x5952e20

我在这里缺少什么?

编辑 - 这是我的模态视图控制器.H

#import <UIKit/UIKit.h>

@protocol DataSyncDelegate
-(void) doneWithSync;
@end

@interface DataSyncViewController : UIViewController {
    id <DataSyncDelegate>   delegate;
}

@property (nonatomic, retain) id <DataSyncDelegate> delegate;

@end

编辑 - 主视图控制器.H和.M

#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#import "DataSyncViewController.h"

@interface LoginViewController : UIViewController <DataSyncDelegate>{

}

@end

这是模式的创造:

DataSyncViewController *dataSyncViewController = [[DataSyncViewController alloc] initWithOptions:FALSE];
dataSyncViewController.delegate = self;
[self presentModalViewController:dataSyncViewController animated:NO];
[dataSyncViewController release];

这是我对代表的实施:

-(void) doneWithSync {
    [self dismissModalViewControllerAnimated:YES];
}

现在所有内容都在编译器中正确连接,但是我收到以下错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[DataSyncViewController setDelegate:]: unrecognized selector sent to instance 0x59e4b40'

1 个答案:

答案 0 :(得分:3)

您的主视图控制器是委托,应该实现协议。你的modalView有一个它被解雇时调用的委托。