MikeMayer67
MikeMayer67

Reputation: 576

Cannot connect IBOutlet to a UIViewController (or subclass thereof)

I am currently trying to build a demo app in Xcode 8.2.1 using swift to learn how to use UIPageViewController. I added three outlets to my subclass of UIPageViewController which I intended to connect to the three views that the page view controller would control.

class MyPageViewController: UIPageViewController, UIPageViewControllerDataSource
{
  @IBOutlet weak var redController : MyColorViewController!
  @IBOutlet weak var greenController : MyColorViewController!
  @IBOutlet weak var blueController : MyColorViewController!
  //...
}

In my main storyboard, I created one instance of MyPageViewController and three instances of MyColorViewController.

But, for the life of me, I cannot figure out how to get the storyboard to connect the three outlets above to any of the color view controller instances.

controller instances

I have tried both ctrl-dragging from the page view controller itself to each color view controller and from the page view controller's outlets in the inspector. Nothing works.

attempt to connect page controller to blue controller

Suggestions?

Upvotes: 2

Views: 1263

Answers (1)

user5890979
user5890979

Reputation:

You should do this by code. And not with IBOutlets.

First: set a storyboard ID to each of the UIViewControllers.

Second:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier :"blueViewController") as! UIViewController

And connect it to a property.

You do this for all three UIViewControllers.

Upvotes: 2

Related Questions