-1

How do I pass a value from one view controller to its previous prant view controller?

Consider this case: I have two view controller. The first screen has one lable and a button and the second view controller has one EditText and a back button.

If I click the first button then it has to move to second view controller and here user has to type something in the text box. If he presses the button from the second screen then the values from the text box should move to the first view controller and that should be displayed in the first view controller.

5 Answers5

3

in secondViewController create protocol - SecondViewController.h

#import <UIKit/UIKit.h>

@protocol  MyDelegate

-(void)PassString : (NSString *)str;

@end

@interface SecondViewController : UIViewController


@property (nonatomic)id <MyDelegate> delegate;


-(IBAction)btnBack:(id)sender;

than after in SecondViewController.m file

-(IBAction)btnBack:(id)sender
{
    [delegate PassString:txt.text];

    [self.navigationController popViewControllerAnimated:YES];

}

than after in FirstViewController set protocol of SecondViewController in ViewController.h file

#import <UIKit/UIKit.h>
#import "SecondViewController.h"

@interface ViewController : UIViewController<MyDelegate>

than after in ViewController.m file

-(void)PassString:(NSString *)str
{
    lbl.text=str;

}
Nikunj5294
  • 391
  • 3
  • 14
0

You need to use a delegate for passing data from secondViewController to firstViewController:

- (IBAction)backToViewController:(id)sender {

[self.delegate viewController:self sendMessageToFirstViewController:@"EditText"];

}

This is and example for delegate.

Peppo
  • 1,107
  • 1
  • 12
  • 19
0

This may help :

yourFirstViewController *viewController=(yourFirstViewController *)[self presentingViewController];
viewController.lbl.text=@"yourValue";

If you are using navigation controller to navigate, then use this :

yourFirstViewController *viewController=[self.navigationController.viewControllers objectAtIndex:numberOfViewControllers - 2];
viewController.lbl.text=@"yourValue";
Vivek Deore
  • 71
  • 10
  • this is in case that he wants to transfer data from first to second. In this case he wants to transfer data from second to first, so he must use delegate. – Stefan Aug 19 '15 at 12:05
0

First you create one variable with property, ex.

@property (nonatomic, retain) NSString *strForUsername;

Then you

#import "SecondViewController".

Then you need to click event of the button.

Create Object of SecondViewController class, example

SecondViewController *mSecondViewController = [SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
mSecondViewController.strForUsername = @"Hello I m from first view controller";
[self.navigationController pushViewController:mHomeViewController animated:YES];

You should use variable with value in SecondViewController.

user3182143
  • 9,459
  • 3
  • 32
  • 39
Jayesh Mardiya
  • 770
  • 7
  • 17
0

Basically you have to pass value from one view controller to another.Right ! You can use NSUserDefaults

Step 1. Save the text in an String

NSString *Text=@"Hello !"
[[NSUserDefaults standardUserDefaults] setObject:Text  forKey:@"passingValue"];
[[NSUserDefaults standardUserDefaults]synchronize];

Step 2. Now you have your textsaved in string "text". Now lets pass this text into a view controller where you want.

 NSString *savedtext =[[NSUserDefaults standardUserDefaults]

                        stringForKey:@"passingValue"];

Here you can display the text stored in "savedtext" into a textview or label. All The Best

Anand Prakash
  • 313
  • 5
  • 20
  • i am using navigation view controller and want that pass data 2nd view controller to 1st view controller click on navigation back button.through -(IBAction ) i can,t do this .because i can,t connect button . i am new to iphone. please explain me properly .if possible. – rajani kumari gupta Aug 20 '15 at 11:27
  • there is no need to connect button. you can save value in viewWillAppear(). I mean you have to do step 1 in viewWillAppear() of 2nd ViewController. – Anand Prakash Aug 20 '15 at 12:38
  • if you confuse little bit then send me the code of 2nd VC where you enter text. – Anand Prakash Aug 20 '15 at 12:47