【发布时间】:2017-01-10 06:15:33
【问题描述】:
我有 ViewController 和 TableViewController。在 ViewController 我有 2 个按钮和 2 个文本字段,当单击 button1 时,它将导航到带有静态数据(如(Apple、Samsung、Blackberry、Windows)的 UITableView,单击 button2 时它将导航到 UITableView 静态数据(医生、工程师、商人,员工)当我选择任何一行时,它应该显示在 button1 的 textfield1 中单击,并且 button2 的 textField2 中单击了 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 变量并存储每一行的值,而不是在文本字段中传递这些值
-
在
didSelectRowAtIndexPath上,将数据保存在NSUserdefaults中。在CustomerVC的viewWillAppear方法上,从Userdefaults 中获取数据并更新txtField1和txtField1值。 -
@prasad,你可以使用block传回数据,这是苹果推荐的方法。
标签: ios objective-c iphone xcode uitableview