0

I'm having two view controller A and B. when click button on view controllerA it will navigate to view controllerB and their is a tableview with textlabel,when i click on tableviewcell i want to navigate back into viewcontrollerA and want to pass that textlabel value back to viewcontrollerA

Moin Shirazi
  • 4,372
  • 2
  • 26
  • 38
vignesh
  • 9
  • 4
  • Use the concept of Unwind segues to post data back : http://stackoverflow.com/questions/34714845/how-to-create-a-back-button-in-a-view-controller-to-go-to-parent-view-controller/34714903#34714903 – Bista Sep 08 '16 at 09:51
  • Check my answer and reply.. – KSR Sep 08 '16 at 10:40

2 Answers2

0

if you are using push seque is the worst way, reason you increase the memory in your stack trace.

you can do this in multiple ways, search once in google you get multiple answer related to this

  1. UnWind Segue

  2. Protocols & delegates

  3. local database method

  4. using Singleton class.

  5. Using bean object method.

  6. using Plist

  7. finally go for NSUserDefault

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
0

I created a sample using Protocols and Delegates, as follows:

ViewController.m

#import "ViewController.h"
#import "ColorsTableViewController.h"

@interface ViewController () <ColorsDelegate>

 @property (weak, nonatomic) IBOutlet UILabel *textDataLabel;
- (IBAction)goButtonTapped:(id)sender;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)sendColor:(NSString *)colorName{
    self.textDataLabel.text = colorName;
}

- (IBAction)goButtonTapped:(id)sender {

    ColorsTableViewController *colorsTableViewController=(ColorsTableViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"colorTableIdentifier"];
    colorsTableViewController.delegate = self;
    [self.navigationController pushViewController:colorsTableViewController animated:YES];
}
@end

ColorsTableViewController.h

#import <UIKit/UIKit.h>

@protocol ColorsDelegate <NSObject>

@optional

- (void)sendColor:(NSString *)colorName;

@end

@interface ColorsTableViewController : UIViewController

@property (nonatomic, assign) id<ColorsDelegate> delegate;

@end

ColorsTableViewController.m

#import "ColorsTableViewController.h"

@interface ColorsTableViewController () <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *colorsTableView;
@property (strong, nonatomic) NSArray *colorsArray;

@end

@implementation ColorsTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.colorsArray = @[@"Red",@"Green",@"Blue",@"Brown",@"Yellow"];

    [self.colorsTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"colorCell"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.colorsArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *myCell = [self.colorsTableView dequeueReusableCellWithIdentifier:@"colorCell" forIndexPath:indexPath];

    UILabel *myTextLabel = [myCell.contentView viewWithTag:100];
    if(!myTextLabel)
    {
        myTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, CGRectGetWidth(myCell.frame), CGRectGetHeight(myCell.frame))];
        myTextLabel.tag = 100;
    }
    myTextLabel.text = self.colorsArray[indexPath.row];
    [myCell.contentView addSubview:myTextLabel];
    return myCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     UITableViewCell *myCell = (UITableViewCell *)[self.colorsTableView cellForRowAtIndexPath:indexPath];
     UILabel *myTextLabel = [myCell.contentView viewWithTag:100];
    if(myTextLabel)
    {
        [_delegate sendColor:myTextLabel.text];
    }
}

Screenshot:

enter image description here

Here is my GitHub Link, please download and check:

https://github.com/k-sathireddy/sendDataBack

KSR
  • 1,699
  • 15
  • 22