2014-03-01 57 views
0

所以我有2個視圖控制器,並且希望DOAbrirListaViewController.h成爲AddTaskViewControoler.h的委託,理論上我認爲我有一切設置,但DOAbrirListaView上的方法永遠不會被調用。UIViewController代表團的子類

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,永遠不會被調用。

+0

您是否設置了'委託'?或者它是'零'? –

回答

0

看來代理設置不正確。添加額外的NSLog到cancelButton檢查它:

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

你做了以下幾點?

#import "AddTaskViewController.h" 

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

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

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

以下是你需要一個良好的工作代碼:DOAbrirListaViewController

定義

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

實施DOAbrirListaViewController

的 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"); 
} 
0

的問題是,我並沒有設置委託,我只是需要做的是,在prepareForSegue,執行Segue公司前。

-(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; 



    } 

} 
+0

我們都會對你說。 –