我有一个UIViewController
和一个UITableViewController
。
在ViewController中,我有一个UIButton
和UITextfield
,当我按下按钮时,它将导航到TableViewController
与表视图中的静态数据(iPhone 5S,iPhone 6,iPhone 6S,iPhone 7),当我选择任何行应该显示在ViewController的文本框中。试过但不能得到这一个。帮我解决这个问题。下面是我的代码:将数据从tableview传回给viewcontroller文本区目标 - C
ViewController.h
————————————————
#import <UIKit/UIKit.h>
#import "Utility.h"
@interface ViewController : UIViewController
- (IBAction)oneButtonClicked:(id)sender;
@property (strong, nonatomic) IBOutlet UITextField *txtOne;
ViewController.m
————————————————
//Can't understand what to do in this viewcontroller.
TableViewController.h
—————————————————————-
#import <UIKit/UIKit.h>
#import "Utility.h"
@interface FamilyDetailsViewController : UITableViewController
@end
TableViewController.m
—————————————————————-
#import "FamilyDetailsViewController.h"
@interface FamilyDetailsViewController()
{
NSArray *arr;
}
@end
@implementation FamilyDetailsViewController
- (void)viewDidLoad {
[super viewDidLoad];
arr=[[NSArray alloc]initWithObjects:@"Parent",@"Grandparent",@"Sibling",@"Child",@"Other", nil];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [arr 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=[arr objectAtIndex:indexPath.row];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.navigationController popViewControllerAnimated:YES];
}
参考http://stackoverflow.com/questions/19343519/pass-数据返回先前视图控制器 – suhit
足够的搜索后发布问题。你的问题有一千个解决方案。 [This](http://stackoverflow.com/questions/626898/how-do-i-create-delegates-in-objective-c)将帮助你。 – iPeter
自定义协议是在视图控制器之间传递数据的最佳方式,因为peter提供链接和suhit的答案。 –