自定义viewcontroller委托不工作

时间:2012-01-09 03:04:37

标签: iphone objective-c ios xcode

我的自定义委托有问题,不知道为什么不工作。我想将委托传递给主视图,以便在添加新记录后自动推送到详细视图。

不确定我错过了什么。我正在与故事板& iOS 5。

感谢您对所有评论的赞赏和赞赏

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];
}

2 个答案:

答案 0 :(得分: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

[self dismissModalViewControllerAnimated:YES];

[[self navigationController] dismissModalViewControllerAnimated:YES];

答案 1 :(得分:0)

似乎我错过了一个功能:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"addNote"]) {
        UINavigationController *navController = [segue destinationViewController];
        addNote *addingNote = (addNote *)navController.topViewController;
        addingNote.delegate = self;
    }
}