0

I have a UIViewController and a 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. 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];
}
D4ttatraya
  • 3,344
  • 1
  • 28
  • 50
Abhimanyu
  • 61
  • 3
  • 10
  • refer http://stackoverflow.com/questions/19343519/pass-data-back-to-previous-viewcontroller – Suhit Patil Dec 15 '16 at 07:45
  • 1
    Post a question after searching enough. Your problem has thousand of solutions. [This](http://stackoverflow.com/questions/626898/how-do-i-create-delegates-in-objective-c) will help you. – iPeter Dec 15 '16 at 08:08
  • Custom protocol is the best way to pass data between viewcontrollers as peter provide link and suhit's answer. – ChintaN -Maddy- Ramani Dec 15 '16 at 08:32

3 Answers3

1

Try Using NSNotification. Pass data back to ViewController with NSNotification. First register a notification in viewWillAppear of ViewController. Try below code:

In 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:

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

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

And in tableView's didSelectRowAtIndexPath method, Post the notification:

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

Sushil Sharma
  • 2,321
  • 3
  • 29
  • 49
1
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

- (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
}
Suhit Patil
  • 11,748
  • 3
  • 50
  • 60
0

Follow this:

In AppDelegate.h

@property (strong, nonatomic) NSString *selectedText;

In ViewController.m

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

Make appDelegate.selectedText = @"" while navigating to UITableViewController Class.

In 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). You can fetch data from this in ViewController(viewWillAppear).

Community
  • 1
  • 1
SNarula
  • 548
  • 3
  • 16
  • Also, there are some more methods to populate data in previous ViewController. Blocks, Delegates, NSNotificationCenter. As you said you are new to iOS development. So I have told you the most simple way using singleton class. – SNarula Dec 15 '16 at 07:52