2011-12-23 3 views
0

의 텍스트 필드에 대한 값을 전달 : 두 번째 뷰는 사용자가 행을 선택 테이블 뷰 동안 제보기 빈 텍스트 필드가있다. 행을 터치하면 첫 번째보기의 텍스트 필드에 값을 설정하는 작업이 있습니다. 불행히도 첫 번째보기 필드로 돌아 가면 설정되지 않습니다.I 탐색 컨트롤러 개의 뷰가 처음 볼

 @class FirstViewController; 

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

SeconViewController.m 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

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 

:

내 코드입니다

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

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

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

을 제시해주십시오.SecondViewController를로드합니다. 바로 여기서 SecondViewController를 First에 연결해야합니다. – Walter

+0

위의 내용을 참조하십시오. 위의 내용을 참조하십시오. – Maurizio

답변

0

대리자입니다. 이것은 Object-C에서 매우 일반적으로 사용되는 패턴입니다. 이 SO post에 대한 내 대답을 확인하십시오. 나중에 코드가 필요할 경우 알려주십시오. 어디 FirstViewController에서

0

는 나는 완전히 당신이 무슨 뜻인지 이해가 확실하지 않다,하지만 당신은 왜 당신이 SecondViewController에 필요한 값을 포함하는 NSString를 생성하지 않습니다. 그렇게하면 SecondViewController을 설정할 때 이런 식으로 할 수 있습니다.

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

다음 SecondViewController.h에 그렇게 같이있는 NSString에 액세스 할 수 있습니다 : 위, 먼저
당신을 다시 secondViewController에서 연결을 설정하지 코드를 기반으로

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

두 번째보기에서 테이블의 값을 가져 와서 첫 번째보기의 텍스트 필드에 설정해야합니다. – Maurizio

1

아마도 다음과 같은 것이 필요할 것입니다 :

-(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로 텍스트를 전달하고자하는 경우. 당신은 다음과 같이 할 수 있습니다.

NSString *textToPass = self.firstField.text; 

SecondViewController에는 passedText라는 문자열 개체가 있어야합니다. 먼저 화면의 텍스트를 표시하여 원하는 목적지 지금

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

을 그냥 "passedtext"문자열을 사용하면 firstViewController에 secondViewController의 객체를 생성 할 때, 다음과 같이 작업을 수행합니다.

는 도움이되기를 바랍니다. :

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

은 당신의 코드에서 당신은 followinf 수정을 시도 할 수 있습니다 :

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

그것은 문제의 forsure 해결됩니다. 사용할 필요가있는 무엇

관련 문제