-4

How to pass values from viewController B to ViewController A.

For example an user clicks from ViewController A and then navigates to ViewController B.

Now, i want to pass a value from VIewController B to VIewController A.

How can i do that.

In VIewController B

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    AVC *avc=[[AVC alloc] init];

    avc.val =[self.arr objectAtIndex:indexPath.row];

    [self.navigationController popViewControllerAnimated:YES];

} 

In VIewController A

- (void)viewWillAppear:(BOOL)animated {

    [self.la setTitle:self.val forState:UIControlStateNormal];

}
Illep
  • 16,375
  • 46
  • 171
  • 302
  • 3
    Delegate or blocks can do the job for you. Also you can use unwind segue. – Greg Mar 11 '15 at 11:39
  • Can you show me an example. I am new to this – Illep Mar 11 '15 at 11:41
  • Ask google about "how to create and use custom delegate in ios". He will defiantly gives you your solution. Thanks. – V.J. Mar 11 '15 at 11:50
  • 1
    Try a google search on "Passing data between View Controllers". This question has been asked and answered literally **hundreds** of times. Here's one example thread from here on SO: http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – Duncan C Mar 11 '15 at 13:18

1 Answers1

-1

In your B View controller inside Interface file you should declare delegate methods like :

@class AViewController;
@protocol BViewControllerDelegate <NSObject>

- (void)myData:(NSDictionary*)data;

@end

than you should assign delegate instance to A viewcontroller.

@implementation AViewController<BViewControllerDelegate>

BViewController *b = [[BViewController alloc] init];
b.delegate = self;
[b myData:@{@"bla":@"bla"}];
@end
serhat sezer
  • 1,330
  • 1
  • 10
  • 26