将数据从表视图传回 viewcontroller 文本字段 Objective - C

Pass data back from tableview to viewcontroller textfield Objective - C

我有一个UIViewController和一个UITableViewController
在 ViewController 中,我有一个 UIButtonUITextfield,当我按下按钮时,它将导航到 TableViewController,并且在 tableview 中使用静态数据,例如 (iPhone 5S, iPhone 6, iPhone 6S, iPhone 7), 当我 select 任何一行时,它应该显示在 ViewController 的文本字段中。试过但无法得到这个。帮我解决这个问题。下面是我的代码:

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];
}

关注这个:

在AppDelegate.h

@property (strong, nonatomic) NSString *selectedText;

在ViewController.m

-(void)viewWillAppear:(BOOL)animated
{
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    txtOne.text = appDelegate.selectedText;
    
}

导航到 UITableViewController 时制作 appDelegate.selectedText = @"" Class.

在家庭详细信息中ViewController.m

pragma mark - Table 查看委托

- (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];
}

通过这种方式,您将选定的值存储在 Singleton class 中。(应用程序委托)。您可以在 ViewController(viewWillAppear).

中从中获取数据

尝试使用 NSNotification。使用 NSNotification 将数据传回 ViewController。首先在 ViewControllerviewWillAppear 注册一个通知。 试试下面的代码:

ViewController中:

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getTextFromTable:) name:@"PassTextBackToViewController" object:nil];
}

然后像这样在ViewController中定义方法:

-(void)getTextFromTable:(NSNotification *)notification
{

    [self.textField setText:[notification.userInfo valueForKey:@"cellText"]];
}

并且在 tableView's didSelectRowAtIndexPath 方法中,Post 通知:

- (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

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

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
}