-2

hello I want to pass value to my second Controller.The second Controller is connected to NavigationController and the storyboard Id has been set in NavigationController, not in SecondViewController

I show ViewControllers like this

var _profile : UINavigationController{
        get{
            return self.storyboard?.instantiateViewControllerWithIdentifier("ShowUserProfileTableViewController") as! UINavigationController
        }
    }

    case 0:

                if (self.revealViewController().frontViewController.isKindOfClass(UINavigationController)){
                    let nav : UINavigationController = self.revealViewController().frontViewController as! UINavigationController
                    if (!nav.viewControllers[0].isKindOfClass(ShowUserProfileTableViewController)){
                        self.revealViewController().setFrontViewController(self._profile, animated: true)
                    }
                }
                else{
                    self.revealViewController().setFrontViewController(self._profile, animated: true)
                }
                break
hellosheikh
  • 2,929
  • 8
  • 49
  • 115
  • Have you tried with the prepareForSegue function? – CTABUYO Apr 06 '16 at 17:58
  • No I want to pass parameter in this function. the code which I wrote ? @ctabuyo – hellosheikh Apr 06 '16 at 18:00
  • I don't understand your question - what is the exact problem you are having? What is the error message? Why does `prepareForSegue:sender:` not do what you want? Have you searched StackOverflow for previous similar questions (http://stackoverflow.com/q/7864371/558933)? – Robotic Cat Apr 06 '16 at 18:05
  • 1
    each time you access (get)`_profile`, you create a new instance. – vikingosegundo Apr 06 '16 at 18:06

1 Answers1

0

The simplest approach is to use segues and implement prepareForSegue, like so:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "ShowSecondViewController" {
        let navigationController = segue.destinationViewController as! UINavigationController
        let targetViewController = navigationController.viewControllers.first as! SecondViewController
        targetViewController.someString = "foo"
    }
}

Now, this assumes that

  • the segue was given a storyboard identifier of ShowSecondViewController;
  • the destination view controller was a navigation controller whose "root view controller" had a base class of SecondViewController; and
  • the SecondViewController had some property called someString.

Clearly, change these identifier, class, and property names accordingly, but this is the basic approach. And you'd either programmatically call performSegueWithIdentifier or just have a segue between some UI control and the next scene (the navigation controller).

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • well @Rob I know how to pass the parameter through segue. But I am into a problem in which i have to use my code which I shared. I have to explain you the whole scenario then why I need that code. so thats why my question is that is it possible somehow to pass the value in my code – hellosheikh Apr 06 '16 at 19:04
  • @hellosheikh - Yeah, but there seem like lots of issues and open questions there. First, as vikingsegundo points out, you're instantiating a new navigation controller every time you access that property, which doesn't seem wise (you have to be very careful when/how you use that property if you do it that way). The decoupling of the instantiation of the navigation controller with the adding of it to the view controller hierarchy seems imprudent. – Rob Apr 06 '16 at 20:46
  • Second, you're referencing functions (e.g. `revealViewController()` and `setFrontViewController()`) and it's not clear what those are doing, so it's hard to advise you further on your code sample. Also, we can't see in what context you're calling this `case 0` code, so it's really hard to follow what you're doing. Are you trying to pass code between view controllers whose views are already in the view hierarchy or are you instantiating a view controller and its views and trying to pass data there. This is a very atypical configuration, so it's hard to even understand what you're trying to do. – Rob Apr 06 '16 at 20:49