2

I have UITextField in secondViewController and a button in same view.

I want to get that UITextField text as a string to firstViewController.

Here my code.

SecondViewController.m

-(void)backToFirstView
{

NSString *str=disTxtFld.text;
FirstView *firstView=[[FirstView alloc]init];
firstView.discountStr=str;
[self.navigationController popViewControllerAnimated:YES];

}

FirstViewController.h

@property (nonatomic,strong) NSString   *disStr;

FirstViewController.m

@synthesize disStr;

 -(void)viewWillAppear:(BOOL)animated
 {
 NSLog(@"discount:%@",disStr);
 }

When i`am trying to print that string in first view..it showing null value..

any suggestions...

4 Answers4

3

The easiest way to do that is by using delegates. Please take a look here: http://krodev.wordpress.com/2012/10/08/objective-c-delegates/ It's a step by step guide how to define and implement delegate.

Delegates are used to do just that what you are looking.

JPetric
  • 3,838
  • 28
  • 26
  • 1
    Delegate is for sure the better approach. This link also explain well the problem of passing back datahttp://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – Lucas Eduardo Aug 13 '13 at 13:22
  • Voted down only b/c the best way isn't using delegates, that's writing a ton of extra code. Best way is unwind segues as I stated in my answer. – bpapa Sep 10 '14 at 13:49
0

Best way: Use an unwind segue in your storyboard to get back to the previous VC and pass the data along. For more info on this, watch the Storyboards WWDC session from 2012.

Other way: Use the delegate pattern. Create a protocol in your SecondViewController class called SecondViewControllerDelegate. Give it methods like "secondViewControllerDidFinish:" and "secondViewControllerDidCancel:". Have FirstViewController conform to that protocol and provide implementations for those methods. In those implementations, make sure the presented SecondViewController is dismissed. In the SecondViewController, create a delegate property of type id.

When FirstViewController presents SecondViewController, have it set itself as the SecondViewController's delegate. When SecondViewController completes whatever work that needs to be done, have them call the delegate methods and pass in the relevant data.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
bpapa
  • 21,409
  • 25
  • 99
  • 147
  • 2
    Maulik - what was the point of that edit? – bpapa Aug 13 '13 at 13:34
  • It was not only pointless, it was a bad edit. Only use a block quote when you're actually *quoting*. And then you need to provide attribution to the actual source. As best Google and I can tell, this answer isn't copied from anywhere else, so it should not be quoted. – Cody Gray - on strike Sep 10 '14 at 08:41
0

make a function in 1st-viewController

-(void)setStringFromSecondView:(NSStirng *)_value{
my_String=[NSString stringwithFormate:@"%@",_value];
}

and add this in header file

-(void)setStringFromSecondView:(NSStirng *)_value;

Now add a delegate variable in secondViewController

{
id delegateOfPrevious;
}

-(void)setDelegate:(id)_delegate{
delegateOfPrevious=_delegate
}

Now before Pushing Second View Controller in First viewController you have to set the delegate of FirstView Like this

secondViewController *secondView=[[secondViewController alloc] init....];
secondView.setDelegate=self;

........... Now every Thing is set. you are one step away from success:

before popping the SecondViewController you can call this function in second view controller and it will set the value in first view controller.

[delegateOfPrevious setStringFromSecondView:textView.txt];

Do not forget to include firstViewController.h in SecondViewController. I did this a day before for my own project and it is working fine.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
AsifHabib
  • 944
  • 9
  • 25
-2

one best approach is declare your nsstring as global in app delegate.h synthesize it fill it with data in second view controller then try to acess this nsstring in first view controller.

Agent Chocks.
  • 1,312
  • 8
  • 19