我有ViewController和TableViewController。在ViewController中,我有2个按钮和2个文本框,点击button1时,它将导航到带有静态数据(如Apple,Samsung,Blackberry,Windows)的UITableView,并且button2被点击,它将导航到UITableView静态数据(Doctor,商人,员工),当我选择任何行应该显示在button1的textfield1点击和button2的textField2点击ViewController。下面是我试着学习的东西,我不知道它是否正确。从tableview将数据传回ViewController
CustomerVC.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
- (IBAction)button1Click:(id)sender;
@property (strong, nonatomic) IBOutlet UITextField *txtField1;
- (IBAction)button2Click:(id)sender;
@property (strong, nonatomic) IBOutlet UITextField *txtField2;
CustomerTableVC.h
#import <UIKit/UIKit.h>
@interface DetailsViewController : UITableViewController
@end
CustomerTableVC.m
#import "DetailsViewController.h"
@interface DetailsViewController()
{
NSArray *array1,*array2;
}
@end
@implementation FamilyDetailsViewController
- (void)viewDidLoad {
[super viewDidLoad];
array1=[[NSArray alloc]initWithObjects:@"Apple",@"Samsung",@"Blackberry",@"Windows", nil];
array2=[[NSArray alloc]initWithObjects:@"Doctor",@"Engineer",@"Businessman",@"Employee", nil];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [array1 count];
return [array2 count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text=[arrqy1 objectAtIndex:indexPath.row];
cell.textLabel.text=[arrqy2 objectAtIndex:indexPath.row];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.navigationController popViewControllerAnimated:YES];
}
创建的appdelegate变量和存储在比在文本字段 – iOS
传递这些值在'didSelectRowAtIndexPath'每一行值,在保存'NSUserdefaults'的数据。在'CustomerVC'的'viewWillAppear'方法中,从Userdefaults获取数据并更新'txtField1'和'txtField1'Vlaues。 – pkc456
@prasad,你可以使用block传回数据,这是苹果推荐的方法。 – aircraft