0

i'm having issues on my custom delegate, not sure why is not working.i wan to pass the delegate to the mainview so it will auto push to a detail view after adding a new record.

Not sure what i have missed out.i'm doing all the linkage with storyboard & iOS 5.

thanks for looking and appreciated all comments

addnote.h

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

@protocol addNoteDelegate;
@interface addNote : UIViewController
{
    IBOutlet UITextField *memoNameTextField;
    IBOutlet UITextField *memoCommentsTextField;
    id <addNoteDelegate> delegate;
}

@property (nonatomic,assign) id <addNoteDelegate> delegate;


- (IBAction)done:(id)sender;
- (IBAction)cancel:(id)sender;
@end

@protocol addNoteDelegate <NSObject>
-(void)addNoteDidFinish:(addNote *)controller;
-(void)addNoteDidCancel:(addNote *)controller;
@end

addnote.m

-(IBAction)add:(id)sender
{   
   [[self delegate] addNoteDidFinish:self];
}

mainview.h

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

@interface mainView : UITableViewController<NSFetchedResultsControllerDelegate, addNoteDelegate>
{
}

@property (nonatomic, retain) NSArray *memoInfo;
@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, retain) NSManagedObjectContext *ObjectContext;

-(IBAction)backToMain:(id)sender;
- (IBAction)addNote:(id)sender;
@end

mainView.m

-(IBAction)addNote:(id)sender
{
    addNote *addingNote = [[addNote alloc] init];
    addingNote.delegate = self;
    [self performSegueWithIdentifier:@"addNote" sender:self];
}

- (void)addNoteDidFinish:(addNote *)controller{
//PUSH TO NEXT VIEW

    [[self navigationController] dismissModalViewControllerAnimated:YES];
}
Kev
  • 118,037
  • 53
  • 300
  • 385
Desmond
  • 5,001
  • 14
  • 56
  • 115
  • Where have you set the delegate? – Daryl Teo Jan 09 '12 at 03:08
  • what do you mean ? i set it in my header file – Desmond Jan 09 '12 at 04:11
  • 1
    That's not setting the delegate. That's just the declaration. There's your problem. If you do not have something in your code as such "mainView.delegate = self;" or similar, then you have not actually set the delegate. – Daryl Teo Jan 09 '12 at 04:13
  • oh, i forgotten to add in here. -(IBAction)addNote:(id)sender { addNote *addingNote = [[addNote alloc] init]; addingNote.delegate = self; [self performSegueWithIdentifier:@"addNote" sender:self]; } is this the right way ? – Desmond Jan 09 '12 at 04:25
  • 1
    edit it into your post. Also, looks alright at first glance. – Daryl Teo Jan 09 '12 at 04:45

2 Answers2

1
 @protocol addNoteDelegate <NSObject>
-(void)addNoteDidFinish:(addNote *)controller;
-(void)addNoteDidCancel:(addNote *)controller;
@end

@interface addNote : UIViewController
{
    IBOutlet UITextField *memoNameTextField;
    IBOutlet UITextField *memoCommentsTextField;
    id <addNoteDelegate> delegate;
}

@property (nonatomic,assign) id <addNoteDelegate> delegate;


- (IBAction)done:(id)sender;
- (IBAction)cancel:(id)sender;
@end

and:

[self dismissModalViewControllerAnimated:YES];

not:

[[self navigationController] dismissModalViewControllerAnimated:YES];
Hubert Kunnemeyer
  • 2,261
  • 1
  • 15
  • 14
  • 1
    Be aware if your using ARC you need to check out this: http://stackoverflow.com/questions/7021852/arc-error-when-declaring-delegate-ivar – Hubert Kunnemeyer Jan 09 '12 at 03:20
  • Thanks for the reply, hubert. i moved the 1 to the top but it won't work...the error is "Expected a type" – Desmond Jan 09 '12 at 04:10
  • 1
    Make sure your setting the delegate to the mainView if this is where you want to use it. in the viewDidLoad of mainView: addNote.delegate = self. Remember you have to "assign" it to a Class. In your case it's mainView, the Class that is using it. – Hubert Kunnemeyer Jan 09 '12 at 04:16
  • i added this in my mainView.m -(IBAction)addNote:(id)sender { addNote *addingNote = [[addNote alloc] init]; addingNote.delegate = self; [self performSegueWithIdentifier:@"addNote" sender:self]; } – Desmond Jan 09 '12 at 04:25
  • Thanks for the help hubert... i missed out the prepareforSegue which cause it not working....i fixed it now. – Desmond Jan 09 '12 at 04:31
0

Seems I was missing a function:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"addNote"]) {
        UINavigationController *navController = [segue destinationViewController];
        addNote *addingNote = (addNote *)navController.topViewController;
        addingNote.delegate = self;
    }
}
Kev
  • 118,037
  • 53
  • 300
  • 385
Desmond
  • 5,001
  • 14
  • 56
  • 115