2013-11-20 25 views
0

FirstViewController我有一個tableview。單擊一個按鈕以推動SecondViewController,在該位置輸入一個項目並按下按鈕以添加該項目。自定義代表null

SecondViewController.h文件有:

@protocol SecondViewControllerDelegate <NSObject> 

-(void)itemAdded:(NSString *)item; 

@property (nonatomic, weak)id <SecondViewControllerDelegate> delegate; 

在`SecondViewController.m

- (IBAction)myButton:(id)sender { 
[self.delegate itemAdded:@"someText"]; 

} 

FirstViewController.h

@interface SecondViewController: UITableViewController <UIAlertViewDelegate, SecondViewControllerDelegate> 

FirstViewController.m

-(void)itemAdded: (NSString *) item{ 
[self.items addObject: item]; 
} 

一切工作正常,除了[self.delegate itemAdded:@"someText"];不調用功能FirstViewController任何人都可以幫忙嗎?

+1

你在哪裏*設置了第二個視圖控制器的代表? –

+0

我不,我如何設置它? – user3001526

回答

1

問題很可能是第二個視圖控制器的委託爲零。在創建第二個視圖控制器的地方,大概在第一個視圖控制器的某個地方,你需要設置secondViewController.delegate = self,這樣當第二個視圖控制器需要回調委託時,它指向第一個控制器而不是nil。

另外,您已將SecondViewController類聲明爲SecondViewControllerDelegate,但這是不正確的。第二個視圖控制器不會是它自己的委託,而是一個FirstViewController。您需要移動到FirstViewController的界面,然後當您嘗試將FirstViewController設置爲SecondViewController的代理時,編譯器不會抱怨。

+0

謝謝,現在就開始工作吧 – user3001526