-1

I want to pass string data from second view to first view. My first view contains a UITableView with 4 rows. If user taps on 1st row, programmatically, I push a new view controller. For example:

 if(indexPath.row == 0)
{
    UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"NewView"];
    [self.navigationController pushViewController:controller animated:YES];
}

My second view is a tableviewController, wherein user can select any one option from second view which should get passed to first view.

There is no back button as special; since I have used Navigation controller I get navigation back button. So when user presses navigation back button, data from second view should get passed to first view.

Yunus Nedim Mehel
  • 12,089
  • 4
  • 50
  • 56

4 Answers4

0

There is multiple ways for doing that.

  1. You can use NSUserDefaults for storing data.

  2. You can use Delegate Methods.

Community
  • 1
  • 1
Rahul
  • 5,594
  • 7
  • 38
  • 92
0

I would recommend against using NSUserDefaults as you do not need persistent behavior, you want to handle messaging between 2 objects. NSUserDefaults are designed to support customization based on a user's preferences. See the snippet from the documentation below.

I would recommend delegation as it is a design pattern used throughout Apple's code and would be very beneficial for you to know well!

The NSUserDefaults class provides a programmatic interface for interacting with the defaults system. The defaults system allows an application to customize its behavior to match a user’s preferences. For example, you can allow users to determine what units of measurement your application displays or how often documents are automatically saved. Applications record such preferences by assigning values to a set of parameters in a user’s defaults database. The parameters are referred to as defaults since they’re commonly used to determine an application’s default state at startup or the way it acts by default.

There are a number of ways of going this.

  1. Create a weak instance variable in the second view that references the first view and use that to get the data to the first view: weak ViewController* firstView. Remember to add an instance variable to capture the data to firstView. This works but introduces tight coupling between the views. They are now dependent on each other to meet the required functionality.

    if (indexPath == 0) {
        UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"NewView"];
        MyController *myController = (MyController *)controller;
        myController.firstView = self;
        [self.navigationController pushViewController:controller animated:YES];
    }
    
  2. Use a block as a completion handler the second view can use call once the data in question has been selected. Define a block on the secondView. Now in your if statement set the completion handler block o the secondView. Now when the data is selected call the completion handler block.

  3. Use the delegate pattern and define a delegate protocol and delegate property for secondView. The firstView will implement the delegate protocol. So when the data is collected you would call in the secondView.

    [delegate SecondViewDidCollectData:myData];

Property on SecondView

@property (weak, nonatomic) id<SecondViewDelegate> delegate;

Delegate Protocol defined in SecondView header.

// define the protocol for the delegate
@protocol SecondViewDelegate 

// define protocol functions that can be used in any class using this delegate
-(void)SecondViewDidCollectData:(NSData *)data;

@end
jcaron
  • 17,302
  • 6
  • 32
  • 46
Peter Hornsby
  • 4,208
  • 1
  • 25
  • 44
-1

You can achieve this using following ways;

  1. You can use NSUserDefaults for storing data.
  2. You can pass the data like below :

    yourViewController *vc = [[YourViewController  alloc]init];     
    vc.xyzString = "yourStringToPass";
    [self.navigationController popToViewController: vc animated:YES];
    

Hope this will help.

user3182143
  • 9,459
  • 3
  • 32
  • 39
Rohan
  • 2,939
  • 5
  • 36
  • 65
-2

Add below Code in your tableView didSelectRowAtIndexPath Method.

 [self.navigationController.viewControllers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    if([obj isKindOfClass:[firstViewController class]]) {
        firstViewController *objfirstViewController = obj;

        objfirstViewController.xyzString = "yourString";

        [self.navigationController popToViewController: objfirstViewController animated:YES];
        *stop = YES;
    }
}];
Vvk
  • 4,031
  • 29
  • 51