2011-12-23 57 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。

相关问题