2011-12-23 10 views
0

のテキストフィールドに値を渡す:第2のビューは、ユーザーが行を選択することができるテーブルビューである第一のビューを、空のテキストフィールドがあります。行をタッチすると、最初のビューのテキストフィールドに値を設定するアクションがあります。残念なことに私が最初のビューフィールドに戻ると、フィールドは設定されません。私はナビゲーションコントローラを持つ2つのビュー有する第一のビュー

これは私のコードです:

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

+0

上記を参照してください、私はその要求を入れます – Maurizio

答えて

0

はデリゲートです。 Object-Cでは非常によく使用されるパターンです。このSO postに私の答えをチェックしてください。まだコードが必要な場合は教えてください。

0

私は、私は完全にあなたが何を意味するかを理解するかどうか分からないが、なぜあなたはSecondViewControllerに必要な値を含むNSStringを作成しないでください。そうすれば、SecondViewControllerを設定するときにこのようなことをすることができます。

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

はその後SecondViewController.hにあなたがそうのようにNSStringのにもアクセスできるようになります:上記のコードに基づいて

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

2番目のビューでテーブルの値を取って最初のビューのテキストフィールドに設定する必要があります – Maurizio

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が見つかりません – Maurizio

+0

私は試してみました:controllerSecond。 FirstViewController =自己;動作していません – Maurizio

+0

SecondViewControllerでコントローラーを@合成してください。 SecondViewControllerでFirstViewControllerプロパティを合成すると、この回答がうまくいくはずです。または、Notificationを使用してFirstViewControllerにデータを送り返すこともできます。 –

0

あなたが欠けている主なものは、ビュー間を渡す値です。値を別のビューに渡しているビューでオブジェクトを使用していません。同様に、あなたはFirstViewControllerからSecondViewControllerにテキストを渡したい場合。あなたは以下のようにすることができます。

SecondViewControllerには、たとえばpassedTextという文字列オブジェクトが必要です。あなたは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を解決します。あなたが使用する必要がどのような

関連する問題