0

Following code fragment is part of the prepareForSegue method. In that I want to pass self.mutaArr (MutableArray) to SecViewController (Which is a UIViewController).

This is not getting passed because of the following line:

[navController setViewControllers:@[dvc] animated: NO ];

If I pass s I end up getting a black screen. Therefore, how do i sort this?

Please note that I am using SWRevealViewController as well.

if ([segue isKindOfClass:[SWRevealViewControllerSegue class]]) {

    SWRevealViewControllerSegue *swSegue = (SWRevealViewControllerSegue*) segue;    
    swSegue.performBlock = ^(SWRevealViewControllerSegue* rvc_segue, UIViewController* svc, UIViewController* dvc) {

    if ([[segue identifier] isEqualToString:@"gosegue"]) {

        UINavigationController* navController = (UINavigationController*)self.revealViewController.frontViewController;
        SecViewController *s = [[SecViewController alloc] init];
        s.myMutableArr= self.mutaArr;
        [navController setViewControllers:@[dvc] animated: NO ];
        [self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
....
Kampai
  • 22,848
  • 21
  • 95
  • 95
Illep
  • 16,375
  • 46
  • 171
  • 302
  • What is `svc` during the segue? And what is `s` supposed to be as you are switching to view `dvc` – Wain Sep 02 '15 at 12:09
  • Why do you use `setViewControllers` in `prepareForSegue` in the first place? Also, you never use the `s` variable, so it comes as no surprise, that `self.mutaArr` is not passed anywhere. – FreeNickname Sep 02 '15 at 12:17
  • one point, **if indeed you're going to put it in a singleton**, of course simply put it in the AppDelegate. Just add a property to AppDelegate.h, as easy as that. – Fattie Sep 02 '15 at 12:45

3 Answers3

0

I didn't see any role of your variable s except setting a mutable array, have you pushed this controller anywhere? Perhaps:

SecViewController *s = [[SecViewController alloc] init];
s.myMutableArr= self.mutaArr;

navController setViewControllers:@[s] animated: NO ];  // Notice `s` replaced `dvc`

IMHO, the best to pass a value from one controller/view to other controller/view is to use session (i.e., Singleton class). Set value in a session inside swSegue.performBlock block and get value in viewDidLoad of SecViewController.

EDIT:

Look at this tutorial for singleton implementation. In the second example someProperty can be your array.

For getting black screen,i just noticed that you didn't reflect the navController to self.revealViewController.

Perhaps using this:

- (void)pushFrontViewController:(UIViewController *)frontViewController animated:(BOOL)animated; 

to push navController on self.revealViewController may fix black screen issue.

EDIT:

Try like this:

if ([[segue identifier] isEqualToString:@"gosegue"]) {

    UINavigationController* navController = (UINavigationController*)self.revealViewController.frontViewController;
    SecViewController *s = [[SecViewController alloc] init];
    s.myMutableArr= self.mutaArr;
    [navController setViewControllers:@[s] animated: NO ];
    [self.revealViewController pushFrontViewController:s animated: YES];
    // OR
    //[self.revealViewController pushFrontViewController:navController animated: YES];
    ....
NeverHopeless
  • 11,077
  • 4
  • 35
  • 56
  • When i set `@['s']` i get a blank screen. Can you show me a detail example on how to set to a singleton class – Illep Sep 02 '15 at 12:13
  • Can you show me how to pass it too. I am a bit confused. – Illep Sep 02 '15 at 12:24
  • I get this error `No visible @interface for 'SWRevealViewController' declares the selector 'pushFrontViewController:animated:'` – Illep Sep 02 '15 at 12:29
  • Are you using this control: https://github.com/John-Lluch/SWRevealViewController See this function is list in the section `Basic API Description`. May be you have to update this API ? – NeverHopeless Sep 02 '15 at 12:59
0

I see a couple of trouble from the code you provided:

  1. You have to define a property in the destination view controller of your segue that can hold your array and set it in the prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender method of your source view controller. That ensures you set your data structure (i.e. your array) before the transition. You can retrieve the destination view controller of your segue by doing

    UIViewController *viewController = [segue destinationViewController];
    SecViewController *destination = (SecViewController *)viewController;
    destination.arrayHoldingProperty = self.myArray;
    

    Here it's assumed you have defined the arrayHoldingProperty in your SecViewController class.

  2. Having done the first step, you can then perform the segue from wherever you want using the performSegueWithIdentifier:@"whatever" sender:nil method.

Dree
  • 702
  • 9
  • 29
0

You are making another instance of SecViewController class

  SecViewController *s = [[SecViewController alloc] init];

Don't Use this instead of use this:

 SecViewController *s = (SecViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"SecViewController"];
 s.myMutableArr= self.mutaArr;
Rahul
  • 5,594
  • 7
  • 38
  • 92