iphone - 将值传递到第一个 View 的文本字段

标签 iphone objective-c ios uitextfield

我有两个带有导航 Controller 的 View :第一个 View 有空文本字段,而第二个 View 是用户可以在其中选择一行的 TableView 。触摸行时,会执行一个操作,将值设置为第一个 View 的文本字段。不幸的是,当我返回到第一个 View 字段时,未设置。

这是我的代码:

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];
      }

最佳答案

根据上面的代码,您从未将 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];
      }

关于iphone - 将值传递到第一个 View 的文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8618330/

相关文章:

ios - 使用导航 Controller 在不同 View 之间切换

objective-c - 是否有命令行界面可以列出Mac上范围内的所有蓝牙设备?

iphone - 从核心数据 iphone 中的数据生成 XML

jquery - iOS 地址栏固定在视口(viewport)错误的顶部

iPhone UITableViewCell 图层阴影

ios - 如何使 UITableView 看起来像这样

iphone - 为每个新的 NSOperation 或线程创建一个新的 NSManagedObjectContext 是否足够?

ios - 检测 UIImage 是浅色还是深色

ios - 在 setAttributedTitle 之后无法更改 UIButton 标题

ios - 错误域=NSCocoaErrorDomain 代码=3840 "No string key for value in object around character 1."