4

I have had bad luck finding any examples on the web that closely match what I am trying to do. I am trying to using NSPageController to view and switch between multiple NSPageControllers. My steps.

  1. I create a new OS X swift project
  2. I add an object to the ViewController and make it of NSPageController class.
  3. I add two buttons, one I label "Next" and the other one I label "Back" for the transitions.
  4. I link the buttons to the NSPageController object as navigateForward and navigateBack actions.
  5. I create an outlet in the custom NSViewController class for the NSPageController object and add the specific NSPageController delegate methods.
  6. I add two additional view controllers in storyboard and create an identifier for them to reference back in my custom view controller class: Wizard1, Wizard2.

    import Cocoa

    class ViewController: NSViewController, NSPageControllerDelegate {

    @IBOutlet var myPageController: NSPageController!
    override func viewDidLoad() {
        super.viewDidLoad()
        let vc1: AnyObject? = self.storyboard!.instantiateControllerWithIdentifier("Wizard1")
        let vc2: AnyObject? = self.storyboard!.instantiateControllerWithIdentifier("Wizard2")
        self.myPageController.arrangedObjects.append(vc1!)
        self.myPageController.arrangedObjects.append(vc2!)
        // Do any additional setup after loading the view.
    }
    override init?(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        myPageController = NSPageController()
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil?)
    }
    
     required init?(coder aDecoder: NSCoder) {
    myPageController = NSPageController()
    super.init(coder:aDecoder)
    }
    
    override var representedObject: AnyObject? {
        didSet {
        // Update the view, if already loaded.
        }
    }
    
    func pageController(pageController: NSPageController, identifierForObject object: AnyObject!) -> String! {
        return "View"
    }
    
    func pageController(pageController: NSPageController, viewControllerForIdentifier identifier: String!) -> NSViewController! {
        let vc1: AnyObject? = self.storyboard!.instantiateControllerWithIdentifier("Wizard1")
        return vc1 as NSViewController
    
    }
    func pageController(pageController: NSPageController, prepareViewController viewController: NSViewController!, withObject object: AnyObject!) {
        viewController.representedObject = object
    }
    func pageControllerDidEndLiveTransition(pageController: NSPageController) {
        pageController.completeTransition()
    }
    func pageControllerWillStartLiveTransition(pageController: NSPageController) {
    self.presentViewControllerAsModalWindow(self.storyboard?.instantiateControllerWithIdentifier("Wizard2") as NSViewController)
    
    }
    

    }

The error I get when pressing the Next button is:

-[NSNib initWithNibNamed:bundle:] could not load the nibName: NSPageController in bundle (null).
Regexident
  • 29,441
  • 10
  • 93
  • 100
Alex
  • 122
  • 1
  • 7

2 Answers2

0

Perhaps you are trying to load a nib with the wrong name in AppDelegate.m or wherever you are initializing your page controller.

Otherwise you have missed creating a .xib file and to name it NSPageController. When creating a Cocoa Touch Class there is a checkbox to also create an xib file for your class if needed.

Robert
  • 5,278
  • 43
  • 65
  • 115
Matkey
  • 378
  • 4
  • 17
0

This line is responsible for the error:

myPageController = NSPageController()

You're trying to initialize a view controller without a nib, that's why it does not work. By default the NSViewController's name is taken to identify the nib that corresponds to it. In your case it is "NSPageController".

sebastienhamel
  • 313
  • 2
  • 10