2012-07-01 39 views
2

在視圖中調用,讓我們把它的firstView我創建了一個secondView如下推它,如果某些事情發生在的firstView:委託方法沒有得到來自第二視圖

SecondViewController *secondVC = [[secondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; 


    [self.navigationController pushViewController:secondVC animated:YES]; 
    [secondVC release]; 

現在當我在第二個視圖中,如果讓我說一個按鈕被按下,我想回到firstView,並從secondView傳回第一個視圖的值(比如說從secondView到第一個視圖的textfield的整數值)。

這裏是我的嘗試:

@protocol SecondViewControllerDelegate; 

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

@interface SecondViewController : UIViewController <UITextFieldDelegate> 
{ 
    UITextField *xInput; 
    id <SecondViewControllerDelegate> delegate; 
} 

- (IBAction)useXPressed:(UIButton *)sender; 

@property (assign) id <SecondViewControllerDelegate> delegate; 

@property (retain) IBOutlet UITextField *xInput; 

@end 

@protocol SecondViewControllerDelegate 
- (void)secondViewController:(SecondViewController *)sender xValue:(int)value; 

@end 

而在M檔

- (IBAction)useXPressed:(UIButton *)sender 
{ 
    [self.delegate secondViewController:self xValue:1234]; // 1234 is just for test 
} 

然後在的firstView我所做的:

#import "SecondViewController.h" 

@interface FirstViewController : UITableViewController <SecondViewControllerDelegate> { 

} 

@end 

並實現了:

- (void) secondViewController:(SecondViewController *)sender xValue:(int)value 
{ 
    [self.navigationController popViewControllerAnimated:YES]; 
} 

現在,問題是在FirstViewController中的一個我得到的警告「找不到協議」SecondViewControllerDelegate「的定義,並且對於兩個委託方法(上面的最後一段代碼)根本不會被調用。有人可以告訴我什麼是錯的嗎?

+1

是你確定委託!= nil? – Mehdzor

+0

我應該做什麼以及在哪裏確保代表不是零? – Dogahe

+1

您是否設置了代表?像secondVC.delegate = self; – Mehdzor

回答

1

此行之後

SecondViewController *secondVC = [[secondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; 

添加

secondVC.delegate = self; 

而且不是

- (void) secondViewController:(SecondViewController *)sender xValue:(int)value 
{ 
    [self.navigationController popViewControllerAnimated:YES]; 
} 

您應該使用

- (void) secondViewController:(SecondViewController *)sender xValue:(int)value 
{ 
    [sender popViewControllerAnimated:YES]; 
} 
+0

非常感謝Omar Abdelhafith!設置代表的確是解決方案。 – Dogahe

+0

如果有一個模態地呈現FirstViewController的ZerothViewController將如何更改設置委託。基本上我希望SecondViewController將數據傳遞給ZerothViewController,但我無法弄清楚正確的語句。你能幫我嗎? – Dogahe

0

對於問題1:SecondViewControllerDelegate@protocol定義看起來像它在secondViewController.h;你確定這個文件是導入firstViewController.h?否則它將不知道該協議。

問題2:它可能與問題1完全無關。您確定該操作已正確連接嗎?您是否可以在useXPressed:中撥打NSLog()來確保該方法實際上在您期望的時候被調用?

+0

問題1:是的,SecondViewController是在FirstViewController中導入的。問題2:我把一個斷點,是的程序進入useXPressed:但沒有進入 - (void)secondViewController:(SecondViewController *)sender xValue:(int)value;因爲我在那裏也設置了一個斷點。 – Dogahe

1

FirstViewController .h文件中:

#import "SecondViewController.h" 

@interface FirstViewController : UITableViewController <SecondViewControllerDelegate> { 

SecondViewController *secondViewController; 

} 

@end 

在實現文件中,在那裏你初始化SecondViewController例如下一行分配自委託財產:

secondViewController.delegate = self; 

下一個定義的委託方法:

- (void)secondViewController:(SecondViewController *)sender xValue:(int)value 
{ 
NSLog ("This is a Second View Controller with value %i",value) 
} 
+1

非常感謝mancunianetz!我希望我可以選擇更多這些作爲正確的答案。你所說的確是解決方案。再次感謝! – Dogahe

相關問題