简体   繁体   English

将数据从Tableview传递回ViewController文本字段Objective-C

[英]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中,我有一个UIButtonUITextfield ,当我按按钮时,它将导航到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中

pragma mark - Table view delegate 实用标记-表格视图委托

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

相关问题 将数据从tableview传递回Viewcontroller - Pass data back to Viewcontroller from tableview TableView嵌套在CollectionViewCell中的VC,我如何将数据从TableView传递回ViewController - VC with TableView nested in CollectionViewCell, how can i pass data from TableView back to ViewController 将数据从 .cpp 文件传递到 Objective c ViewController - Pass data from .cpp file to Objective c ViewController 将数据从ViewController传递回根ViewController? - Pass data from ViewController back to root ViewController? 如何将数据从视图控制器传递到另一个表视图? - How to pass data from a viewcontroller to another tableview? 将数组数据从ViewController传递到TableView - Pass arrays data from a ViewController to a TableView Swift-将数据从TableView传递到第三个ViewController - Swift - pass data from tableview to third viewcontroller 如何将文本字段数据从第三个ViewController传递到第一个ViewController - How to pass textfield data from third viewcontroller to first viewcontroller 将数据从 TableView(以模态呈现)发送到文本字段内的 ViewController - Send data from TableView (present modally) to ViewController inside textfield 从另一个ViewController的Objective-C重载TableView - Objective-c reload tableview from another viewcontroller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM