2011-12-23 82 views
0

我有導航控制器的兩個視圖:第一個視圖中,有空文本字段,而第二個視圖是用戶可以選擇行的表視圖。只需觸摸一行,就會爲第一個視圖的文本字段設置一個值。不幸的是,當我回到第一個視圖字段沒有設置。將值傳遞給第一個視圖的文本字段

這是我的代碼:

FirtViewController.h

 @interface FirstViewController : UIViewController 
     { 
      UITextField *firstField; 
      UITextField *secondField; 
     } 
     @property(nonatomic, retain) IBOutlet UITextField *firstField; 
     @property(nonatomic, retain) IBOutlet UITextField *secondField; 
     @property(copy) NSString *selectedRow; 
     -(IBAction)showTable:(id)sender 

FirstViewController.m

 #import "FirstViewController.h" 
     #import "AppDelegate.h" 
     #import "SecondViewController.h" 

     @implementation FirstViewController 
     @synthesize ..... 
     ............. 


     -(void)viewDidAppear:(BOOL)animated 
     { 
     [super viewDidAppear:animated]; 
     self.firstField.text = selectedRow; 
     } 

     -(IBAction)showTable:(id)sender 
     { 
     SecondViewController *controllerSecond = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; 
     [self.navigationController pushViewController:controllerSecond animated:YES]; 
     } 

SecondViewController.h

 @class FirstViewController; 

     @interface ListaViewController : UIViewController 
     <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate> 
     { 
     UITableView *table; 
     UISearchBar *search; 
     FirstViewController *controller; 
     } 
     @property (nonatomic, retain) FirstViewController *controller; 

SeconViewController.m

 - (void)tableView:(UITableView *)tableView 
     didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

      NSString *selectedRow = [tableData objectAtIndex:indexPath.row]; 

      controller.selectedRow = selectedRow; 
      [self.navigationController popViewControllerAnimated:YES]; 
     } 
+1

請在FirstViewController中顯示。你加載SecondViewController。這是您必須將SecondViewController重新連接到First的地方。 – Walter 2011-12-23 16:33:37

+0

請參閱上面,我把這個請求 – Maurizio 2011-12-23 16:56:34

回答

0

你需要使用的是委託。它在Object-C中是非常常用的模式。看看我對這個SO post的回答。讓我知道你是否還需要代碼。

0

我不能肯定我完全明白你的意思,但你爲什麼不創建一個包含NSString您在SecondViewController所需的值。這樣,當你設置SecondViewController時,你可以做這樣的事情。

SecondViewController *nextView = [[SecondViewController alloc] init]; 
nextView.myNSString = @"Value you need in the next view"; 

然後在SecondViewController.h你就可以訪問的NSString像這樣:

@NSLog(@"%@", self.myNSString); 
+0

我必須在第二個視圖中的表的值,並將其設置在第一個視圖的文本字段 – Maurizio 2011-12-23 17:02:32

1

基於您的代碼上面,你永遠不設置secondViewController連接回第一
你可能需要這樣的東西:

-(IBAction)showTable:(id)sender 
     { 
     SecondViewController *controllerSecond = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; 
     [controllerSecond setController:self]; //this sets the reference back 
     [self.navigationController pushViewController:controllerSecond animated:YES]; 
     } 
+0

收到此警告:setController not found – Maurizio 2011-12-23 17:12:34

+0

我試着用:controllerSecond。 FirstViewController = self;但不起作用 – Maurizio 2011-12-23 17:28:39

+0

確保你@synthesize在SecondViewController中的控制器。如果綜合SecondViewController中的FirstViewController屬性,此答案應該可行。或者您可以使用通知將數據發送回FirstViewController。 – 2011-12-23 17:30:41

0

你缺少的主要是值之間傳遞的意見。在任何將值傳遞給另一個視圖的視圖中,您都沒有使用任何對象。就像,如果你想從FirstViewController傳遞一個文本到SecondViewController。你可以做它作爲follwos。

NSString *textToPass = self.firstField.text; 

在你的SecondViewController中需要有一個字符串對象say passText。當您在firstViewController創建secondViewController的對象,如下行動:

SecondViewController *controllerSecond = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; 
controllerSecond.passedText = textToPass; 
[self.navigationController pushViewController:controllerSecond animated:YES]; 

現在,無論你想顯示第一屏的文本,只要使用「passedtext」字符串。

希望它有助於。 :)

=========================================== ====

在你的代碼可能會嘗試followinf修改:

NSString *selectedRow = [tableData objectAtIndex:indexPath.row]; 
      controller = (FirstViewController *)[self.navigationController topViewController]; 
      controller.selectedRow = selectedRow; 
      [self.navigationController popViewControllerAnimated:YES]; 

它能夠解決您的問題forsure。

相關問題