views:

61

answers:

1

I need to present a modal view controller and be notified when it is dismissed or notified that I need to dismiss it, looking here I am still confused:

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

I have my mainViewController and myModalView controller and I have the following code that needs to be implemented but not sure where - first up delegate protocal:

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

which controller.h does this go in? I am assuming my modalViewController.h

second is my implementation:

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

which controller.m does this go in? I am assuming my mainViewController.m

I also have the delegate properties that needs to be aded:

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

which controller.m does this need to go in? I am assuming my modalViewController.h

and here is how I am presenting the modalViewController from my MainViewController:

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

As of right now this gives me the following error:

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

What am I missing here?

EDIT - HERE IS MY MODAL VIEW CONTROLLER .H

#import <UIKit/UIKit.h>

@protocol DataSyncDelegate
-(void) doneWithSync;
@end

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

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

@end

EDIT - MAIN VIEW CONTROLLER .H AND .M

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

@interface LoginViewController : UIViewController <DataSyncDelegate>{

}

@end

HERE IS THE CREATION OF THE MODAL:

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

HERE IS MY IMPLEMENTATION OF THE DELEGATE:

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

And now everything looks to wire up correctly in the compiler but I get the following error:

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

Your main view controller IS the delegate and should implement the protocol. Your modalView has a delegate that it calls when it is being dismissed.

willcodejavaforfood
I understand the concept but I have some code in the wrong place, that's why I tried to be so detailed above - what do I have in the wrong spot here?
Slee
did you declare the @delegate property in DataSyncViewController?
willcodejavaforfood
Yes, I have this in my dataSyncController: id delegate;@property (nonatomic, retain) id delegate;
Slee
Is your mainViewController implementing the DataSyncDelegate protocol?
willcodejavaforfood
I believe so, like this in the .m file:-(void) doneWithSync { [self dismissModalViewControllerAnimated:YES];}
Slee
when I add this to my mainViewCOntroller.h: @interface LoginViewController : UIViewController <DataSyncComplete> I get a 'Cannot find protocol declaration' error - I am getting so damn frustrated.
Slee