-2

Helo all,

I am moving from one view controller to another using below code.

UIStoryboard  *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"RoloBusinessCard"];
[self.navigationController pushViewController:vc animated:YES];

Now i want to pass some data from one view controller to another like some variable.I have got this code to do this task but i am not making instance of another view controller.I am starting another view controller using storyboard.

SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
secondViewController.str1 = str;
[self.navigationController pushViewController:secondViewController animated:YES];

Please tell me how can i pass data between view controllers in my way?

Solution does not work decalre NSString in .h file.

@property (nonatomic, strong) NSString *UserId;

Synthesize in .m file

@synthesize UserId;

Code for navigation

 UIStoryboard  *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"RoloBusinessCard"];
  vc.UserId=@"abc";
 [self.navigationController pushViewController:vc animated:YES];

But vc.UserId does not recognized.

TechChain
  • 8,404
  • 29
  • 103
  • 228
  • 2
    Please try to google before asking questions. Duplicate of http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers?rq=1 – iAnurag Aug 11 '15 at 07:08

3 Answers3

2

You can pass data between two ViewControler with the help of properties. First in RoloBusinessCard.h ViewControler make a property like this

@property (nonatomic, strong) NSString *str1;

in .m file of this class synthesize it like this

@synthesize str2;

Now like this you can pass value

UIStoryboard  *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"RoloBusinessCard"];
vc.str2=str2
[self.navigationController pushViewController:vc animated:YES];

If you are not able to get str2 in RoloBusinessCard

First Replace UIViewController with Your Viewcontroller Name like this

RoloBusinessCard *vc = [storyboard instantiateViewControllerWithIdentifier:@"RoloBusinessCard"];

or

typecase UIViewController like this

UIStoryboard  *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"RoloBusinessCard"];

(RoloBusinessCard *)vc1=vc
    vc1.str2=str2
    [self.navigationController pushViewController:vc1 animated:YES];

Hope it help.

chakshu
  • 1,372
  • 2
  • 10
  • 21
  • Not able to get 'str1' as string in my view controller.I have made it property & also synthesize it.But it does not work for me.@chakshu – TechChain Aug 11 '15 at 07:43
  • edited my answer please check.If you find it correct please accept my answer. Thanks – chakshu Aug 11 '15 at 08:17
1

Try Like this, Hope it will work.

UIStoryboard  *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
SecondViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"RoloBusinessCard"];
vc.str1 = str;
[self.navigationController pushViewController:vc animated:YES];
Vishnuvardhan
  • 5,081
  • 1
  • 17
  • 33
0

Here we are just using Navigation stack for passing data to the next controller

create a property in 2nd view controller

@property (nonatomic, strong) NSString *yourString;

in firstView controller action method object of view controller which you will create on push method do

-(void)btnAtion{
object.yourstring = txtyourfirstvcObject.text
}

In swift, we can do the same

let's suppose we want to pass a Bool Value to the next controller so we can do :

Class ToViewController : UIViewController {
      public var isSelected : Bool? 
}

Class FromViewController : UIViewController {

    @IBAction func onClickFlyingFrom(_ sender: UIButton) {
         let tvc = self.storyboard?.instantiateViewController(withIdentifier: "ToViewController") as! ToViewController
         tvc.isSelected = true
         self.navigationController?.present(vc, animated: true, completion: nil)
    }
}
argus7
  • 95
  • 1
  • 11