UIViewController委托的子类

时间:2014-03-01 17:51:07

标签: ios uiapplicationdelegate

所以我有2个视图控制器,并希望DOAbrirListaViewController.h成为AddTaskViewControoler.h的委托,理论上我认为我已经完成了所有设置,但DOAbrirListaView上的方法永远不会被调用。

DOAbrirViewController.h

#import "AddTaskViewController.h"


@interface DOAbrirListaViewController : UITableViewController <NSFetchedResultsControllerDelegate, UIAlertViewDelegate, AddTaskViewControllerDelegate>

DOAbrirViewController.m

-(void)addTaskDidCancel {
    NSLog(@"AddTask Did Cancel");

}
-(void)addTaskDidSave {
    NSLog(@"AddTask Did Save");
}

AddTaskViewController.h

@protocol AddTaskViewControllerDelegate

-(void)addTaskDidSave;
-(void)addTaskDidCancel;

@end

@interface AddTaskViewController : UIViewController

@property (nonatomic, weak) id <AddTaskViewControllerDelegate> delegate;
@property (nonatomic, strong) CDMyLists *currentList;


@end

AddTaskViewController.m

- (IBAction)createButton:(id)sender {
    [self.delegate addTaskDidSave];
    NSLog(@"Create Button");
}

- (IBAction)cancelButton:(id)sender {
    [self.delegate addTaskDidCancel];
    NSLog(@"Cancel Button");
}

单击按钮但addTaskDidSaveAddTaskDidCancel上的NSLOG永远不会被调用。

4 个答案:

答案 0 :(得分:0)

似乎没有正确设置委托。将其他NSLog添加到cancelButton进行检查:

- (IBAction)cancelButton:(id)sender {
    [self.delegate addTaskDidCancel];
    NSLog(@"Cancel Button");
    NSLog(@"self.delegate = %@", self.delegate);
}

答案 1 :(得分:0)

您是否执行了以下操作?

#import "AddTaskViewController.h"

@interface DOAbrirListaViewController : UITableViewController <NSFetchedResultsControllerDelegate, UIAlertViewDelegate, AddTaskViewControllerDelegate>

@property (nonatomic, strong) AddTaskViewController *addTaskController;
在.m文件中

_addTaskController = [[AddTaskViewController alloc] init];
[_addTaskController setDelegate:self];

答案 2 :(得分:0)

以下是您需要的正常工作代码:

DOAbrirListaViewController的定义

@interface DOAbrirListaViewController : UITableViewController <NSFetchedResultsControllerDelegate, UIAlertViewDelegate, AddTaskViewControllerDelegate>

实施DOAbrirListaViewController

-(void)viewDidLoad
{
    // set self as a delegate of your taskAddViewController
    [addTaskViewController setDelegate:self];
}

-(void)addTaskDidSaveWithTaskAddViewController:(TaskAddViewController*)taskAddViewController; {
    NSLog(@"AddTask Did Cancel");

}
-(void)addTaskDidCancelWithTaskAddViewController:(TaskAddViewController*)taskAddViewController {
    NSLog(@"AddTask Did Save");
}

AddTaskViewController的定义

@protocol AddTaskViewControllerDelegate;

@interface AddTaskViewController : UIViewController

@property (nonatomic, weak) id <AddTaskViewControllerDelegate> delegate; 
// you need
//a reference
//to the delegate
@property (nonatomic, strong) CDMyLists *currentList;

@end

//So you can transmit your controller to the delegate as a parameter
@protocol AddTaskViewControllerDelegate

-(void)addTaskDidSaveWithTaskAddViewController:(TaskAddViewController*)taskAddViewController;
-(void)addTaskDidCancelWithTaskAddViewController:(TaskAddViewController*)taskAddViewController;

@end

实现AddTaskViewController

- (IBAction)createButton:(id)sender 
{
    [self.delegate addTaskDidSaveWithTaskAddViewController:self];
    NSLog(@"Create Button");
}

- (IBAction)cancelButton:(id)sender {
    [self.delegate addTaskDidCancelWithTaskAddViewController:self];
    NSLog(@"Cancel Button");
}

答案 3 :(得分:0)

问题是,我没有设置代理,我只需要在执行Segue之前在prepareForSegue中执行此操作。

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@"addTask"]) {
        NSLog(@"addTask Segue");

        AddTaskViewController *atvc = (AddTaskViewController *)[segue destinationViewController];
        atvc.delegate = self; // Was missing that
        atvc.currentList = self.currentList;



    }

}