0

I'm adding a ViewController (MainViewController) inside a scroll view (TourViewController) with the Storyboard below.

Storyboard

The following code is how I add the MainViewController to the TourViewController

     let subView = UIScrollView(frame: CGRectMake(
        (self.scrollView.frame.width * CGFloat(pageImages.count)),
        0,
        self.scrollView.frame.width,
        (self.scrollView.frame.height)))

    //Set the size of the content view
    let contentView = UIView(frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height))

    subView.contentSize = CGSizeMake(contentView.frame.width, self.view.frame.height)

    let newView = self.storyboard?.instantiateViewControllerWithIdentifier("MainView") as? MainViewController


    contentView.addSubview(newView!.view)
    subView.addSubview(contentView)
    scrollView.addSubview(subView)

It appears to work well but I'm not able to add any segue from the MainViewController to any other view as it will raise an exception at runtime.

In my MainViewController I have the following code on the viewDidLoad

  login.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)

When I click on the button it raise the error

Mouhamad Kawas
  • 370
  • 2
  • 12
  • The code you have posted has nothing to do with segues. At the very least edit your question to include what you have tried, what the problem is and an image of your Storyboard. Also, if `MainView` is a `UIViewController` and not a `UIView`, you are not adding the `MainView` view controller into the object hierarchy correctly. See this answer for how to do so correctly: http://stackoverflow.com/a/13617590/558933 – Robotic Cat Feb 19 '15 at 19:40
  • @RoboticCat please check the attached screen, I know there is nothing related to segues. But the idea is the show push is not working with me, and this my question what is the issue in my hierarchy? – Mouhamad Kawas Feb 19 '15 at 19:51
  • `1)` Add the symbolicated stack trace from the exception. That should help narrow the problem down. `2)` You are adding the `MainView` incorrectly - see my previous link. `3.` Your view controllers are blank and empty - how are you triggering the segue. `4.` The app should stop on the exception - add this code to the question indicating the line. – Robotic Cat Feb 19 '15 at 20:14
  • @RoboticCat I was looking into the issue is not only related to segue, also the actions are not working well. By the way, my screens are not empty I have two buttons in the MainViewController – Mouhamad Kawas Feb 19 '15 at 20:18
  • You have to ask a specific question to allow people to try to help you. If there are other problems then they should be a different question. You should aim to get the segue working first. – Robotic Cat Feb 19 '15 at 20:20
  • This is my question the actions are not working :) @RoboticCat – Mouhamad Kawas Feb 19 '15 at 20:23
  • What are "actions"? This is my last comment so you need to be clear about the problem. If it's an exception on a segue then add the symbolicated log and code indicating which line is causing the exception. If it is something else then there is nothing in your question relating to these other problems. Choose ONE problem and write a question that allows people to help. This is how to write a question: http://stackoverflow.com/help/how-to-ask – Robotic Cat Feb 19 '15 at 20:33
  • @RoboticCat I'm adding login.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside) in the viewDidLoad of the MainViewController but when I click on the buttons it raise an error. – Mouhamad Kawas Feb 19 '15 at 20:36
  • Your MainViewController is probably being deallocated. You need to add it as a child view controller when you add its view to TourViewController's view. – rdelmar Feb 19 '15 at 20:38
  • @RoboticCat is there any issue in a way that I'm adding the ViewController? did I do something wrong that not allow the MainViewController to not be able to have any actions? – Mouhamad Kawas Feb 19 '15 at 20:40
  • @rdelmar I'm using the following colde: contentView.addSubview(newView!.view) //this is the MainViewController subView.addSubview(contentView) scrollView.addSubview(subView) Is there something wrong in this way? – Mouhamad Kawas Feb 19 '15 at 20:42
  • That only adds the view. You need to add MainViewController as a child view controller of TourViewController, or it will be deallocated. You should look at the api in the UIViewController class reference for creating custom container view controllers. – rdelmar Feb 19 '15 at 20:43
  • @rdelmar What do you mean by custom container view? – Mouhamad Kawas Feb 19 '15 at 20:47
  • @rdelmar I was looking about allocate a ViewController and it seems instantiateViewControllerWithIdentifier is enough to allocate a viewcontroller, and i'm already using it – Mouhamad Kawas Feb 19 '15 at 20:58
  • @MouhamadKawas: Your code is wrong as that is NOT the correct way to add a view controller to another view controller. My FIRST comment linked to an answer that shows the correct way to add a controller to another controller. Go and take a look at it. Implement it and come back if you have a problem. – Robotic Cat Feb 19 '15 at 20:59
  • @RoboticCat I look into the link, it's the same way that I'm adding the viewcontroller By the way, the link is 5 year old, and please if you want to help be more specific – Mouhamad Kawas Feb 19 '15 at 21:03
  • Yes, instantiateViewControllerWithIdentifier is enough to allocate a view controller, but you are NOT adding it to anything, so it will be deallocated. When you add a view of a view controller to another controller's view, you should add that controller as a child (with addChildViewController). If you don't understand what a container view controller is, then you need to study the documentation. There is a section on it in the "View Controller Guide for iOS". – rdelmar Feb 19 '15 at 23:02

0 Answers0