[英]Pass data back from tableview to viewcontroller textfield Objective - C
I have a UIViewController
and a UITableViewController
. 我有一个
UIViewController
和一个UITableViewController
。
In ViewController I have a UIButton
and UITextfield
, when I press button it will navigate to TableViewController
with static data in the tableview like (iPhone 5S, iPhone 6, iPhone 6S, iPhone 7), when I select any of the row it should be displayed in the textfield of ViewController. 在ViewController中,我有一个
UIButton
和UITextfield
,当我按按钮时,它将导航到TableViewController
UITextfield
TableViewController
具有静态数据,例如(iPhone 5S,iPhone 6,iPhone 6S,iPhone 7),当我选择应显示的任何行时在ViewController的文本字段中。 Tried but cannot get this one. 尝试过但无法得到这个。 Help me to solve this.
帮我解决这个问题。 below is my code:
下面是我的代码:
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];
}
Try Using NSNotification
. 尝试使用
NSNotification
。 Pass data back to ViewController
with NSNotification
. 使用
NSNotification
数据传递回ViewController
。 First register a notification in viewWillAppear
of ViewController
. 首先在
ViewController
viewWillAppear
中注册一个通知。 Try below code: 试试下面的代码:
In ViewController
: 在
ViewController
:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getTextFromTable:) name:@"PassTextBackToViewController" object:nil];
}
Then define Method in ViewController
like this: 然后在
ViewController
定义如下方法:
-(void)getTextFromTable:(NSNotification *)notification
{
[self.textField setText:[notification.userInfo valueForKey:@"cellText"]];
}
And in tableView's
didSelectRowAtIndexPath
method, Post the notification: 在
tableView's
didSelectRowAtIndexPath
方法中,发布通知:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
NSDictionary *dictionary=[NSDictionary dictionaryWithObjectsAndKeys:cell.textLabel.text,@"cellText", nil]
[[NSNotificationCenter defaultCenter]postNotificationName:@"PassTextBackToViewController" object:dictionary];
[self.navigationController popViewControllerAnimated:YES];
}
Make Sure, Notification's name must be same in both ViewController and TableViewController
确保,在ViewController和TableViewController中,通知的名称必须相同
in `ViewControllerB.h` which is a TableViewController declare a delegate to pass the data to ViewControllerA and create a delegate object
#import <UIKit/UIKit.h>
@protocol DataDelegate <NSObject>
- (void)passData:(NSString *)data;
@end
@interface ViewControllerB : UIViewController
@property (weak, nonatomic) id<DataDelegate> delegate;
@end
in ViewControllerB.m
file 在
ViewControllerB.m
文件中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selectedText = titleArray[indexPath.row];
if [self.delegate respondsToSelector:@selector(passData:)]) {
[self.delegate passData:"hello"];
}
[self.navigationController popViewControllerAnimated:YES];
}
**in ViewControllerA**
#import "ViewControllerA.h"
#import "ViewControllerB.h"
@interface ViewControllerA () <DataDelegate>
@property (strong, nonatomic) ViewControllerB *viewControllerB;
@end
@implementation ViewControllerA
- (void)viewDidLoad {
_viewControllerB.delegate = self;
}
- (void)passData:(NSString *)data {
self.textField.text = data
}
Follow this: 请遵循以下步骤:
In AppDelegate.h 在AppDelegate.h中
@property (strong, nonatomic) NSString *selectedText;
In ViewController.m 在ViewController.m中
-(void)viewWillAppear:(BOOL)animated
{
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
txtOne.text = appDelegate.selectedText;
}
Make appDelegate.selectedText = @""
while navigating to UITableViewController Class. 导航到UITableViewController类时,使
appDelegate.selectedText = @""
。
In FamilyDetailsViewController.m 在FamilyDetailsViewController.m中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selected= [arr objectAtIndex:indexPath.row];
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
appDelegate.selectedText = selected;
[self.navigationController popViewControllerAnimated:YES];
}
In this way you are storing selected value in Singleton class.(App delegate). 这样,您可以将所选值存储在Singleton类中。(应用程序委托)。 You can fetch data from this in ViewController(viewWillAppear).
您可以在ViewController(viewWillAppear)中从中获取数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.