我有两个带有导航 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/