1

How to pass data to ViewController using this code?

UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

UIViewController *vc = [main instantiateViewControllerWithIdentifier:@"Catalog"];

[self presentViewController:vc animated:YES completion:nil];

I want to pass data in example to itemId, like here:

ViewController *vc = [[ViewController alloc] initWithNibName:nil bundle:nil];

vc.itemId = 0;

[[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:vc animated:YES completion:nil];
4
  • Search the interwebs for prepareForSegue. Commented Apr 30, 2014 at 9:34
  • 2
    You already have the vc.itemId = 0; line. What is it that you don't understand? Commented Apr 30, 2014 at 9:35
  • 1
    @Wain I think he means "I want to initialise the presented view controller as if I had done the following...". Commented Apr 30, 2014 at 9:38
  • @trojanfoe yes, that I meant. Commented Apr 30, 2014 at 9:46

2 Answers 2

2

Type cast your UIViewController if it's ViewController

ViewController *vc = (ViewController*)[main instantiateViewControllerWithIdentifier:@"Catalog"];
vc.itemId = 0;
[self presentViewController:vc animated:YES completion:nil];
Sign up to request clarification or add additional context in comments.

Comments

0

This should also work for you.

ViewController *vc=[self.storyboard instantiateViewControllerWithIdentifier:@"Catalog"];
vc.itemId =0;
[self presentViewController:vc animated:YES completion:Nil];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.