How to add the code file for a second ViewController - swift2

pardon me for beginner's question.
Besides the first default ViewController with its companion file ViewController.swift, I created a 2nd viewcontroller and know how to move to it.
How do I add a companion SecondViewController.swift for it?
Thanks

If you are using Xib then
1) Goto file->New->iOS->Source->Cocoa Touch Class
2) Select UIViewController as your Base Class and name your class and also check the checkbox with "also create Xib File" click next and click finish.
Now you have a new ViewController with a file associated with it.
And if you are using storyboards then follow these steps.
1) Drag and drop a viewController from object library
2) repeat step 1 and 2 of first method but this time do not create an Xib file
3) select your newly draged droped ViewController and assign it the new class as follows
Now you have a new ViewController with an associated swift file.

Related

How do I see what viewController a class is attached to?

It's easy to tell what class a viewController uses, but what about the other way around: if you have a class and wants to see what viewController it's attached to. Is there a way to do that? I'm now in a peculiar position where I have made changes in a class but don't know how to test the changes because I don't know which viewController to enter in the simulator to see the result.
Follow the screenshot then you will get the name of your viewcontroller class.
select Main Storyboard
select your ViewController
select identity inspector
you will see the class name

Main.storyboard not connecting to viewcontroller.swift

I have kinda big problem:
When I add a view controller to my main.storyboard and then try to hook it up to viewcontroller.swift it won't connect, but my original one does how do I fix this?
Here is a quick guide for how to properly add a new View Controller.
Create a new ViewController.swift using the keyboard command cmd+n. Choose the Cocoa Touch Class
Name it whatever you want & make sure it's a subclass of UIViewController
Create and save the new .swift file. Now, copy the name of your view controller.
In your storyboard, create a new view controller and select it.
On the right hand side, under Custom Class, insert the new view controller's name (you should have copied that, so just hit paste) and hit enter
Hope that helps :)
Each View Controller in story board should have it`s own subclass of UIViewController. You can create new by pressing cmd + n -> Cocoa Class -> Subclass of "UIViewController".

Creating an outlet for a NSProgressIndicator inside a NSToolbar

I have this OSX storyboard-based application that starts with a NSSplitViewController like this:
This splitViewController has two viewControllers: master and detail.
Inside the window I have a NSToolbar. I dragged a NSProgressIndicator to that toolbar and Xcode embedded it inside a NSToolbarItem.
Now I need to create an outlet (not an action as explained on other stackoverflow questions) from the NSProgressIndicator to some class. First question is which one?
Xcode will not let I create an outlet. I have tried these options:
dragged from the ToolbarItem to masterController class file, detailController class file and to NSSplitViewController class.
dragged from the ToolbarItem to the delegate class.
dragged from the NSProgressIndicator to masterController class file, detailController class file and to NSSplitViewController class.
dragged from the NSProgressIndicator to the delegate class.
dragged from both the NSToolbarItem and from the NSProgressIndicator to the Window Controller First Responder.
In all cases dragging does not make a window appear to allow me to create the outlet.
For heavens sake, how do I create an outlet like that? To which class I drag it and how do I do that?
I'll assume your setup is more like this image:
Your Window scene is backed, by default, by an NSWindowController, to which you cannot add new outlets. You need to create a subclass of that, associate it with your Window and then you should be able to create outlets in that.
File > New File > Cocoa Class
Specify a name like "SpaceDogsWindowController", as a subclass of NSWindowController.
Then use select the window controller icon (blue circle) and select the Identity Inspector in Xcode. (CMD+ALT+3). Specify the name of your new class in the "Class" field.
Then try to hookup an outlet:
1) Show the Assistant Editor
2) Use the Jump Bar to ensure your custom class is visible (It's at the top of the assistant editor pane, it should say Automatic and you can tap that to then select your new class; If it says 'Manual', change it to Automatic).
3) If your are control-dragging and it's still not offering to make a connection, try dragging from the document outline (also shown in the screen shot).
You could then edit that progress indicator from other view controllers, which are descendants of that window's view hierarchy, using code like this:
if let windowController = self.view.window?.windowController() as? CustomWindowController {
windowController.progressIndicator.doubleValue = 0.4
}
or, in Objective-C, something like this:
CustomWindowController *myWindowControllerSubclass = self.view.window.windowController;
windowController.progressIndicator.doubleValue = 0.4;
Hope that helps.

Can't ctrl+drag into ViewController.h from second Viewcontroller

I am building an iOS app for fun and here is where I run into trouble. I can insert an outlet and action in the ViewController.h files directly from my first View Controller through the ctrl+drag method; however, when I try ctrl+drag on the second ViewController it will not allow me.
Ctrl+drag on first ViewController
Ctrl+drag on second ViewController
You have the wrong document open in the assistant editor. It should be ViewController.h, but you are displaying UIViewController.h.
Check you have correctly set your second view controller to your custom class ViewController using the Identity Inspector (third of the right hand utility panels) then make sure it's header file is the document you are displaying on the right.
update
From your comments, you are having difficulty setting the second view controller to a custom class.
Here is how you select it in the storyboard. Note that you are selecting the View Controller, not it's topmost View
Copy and paste the class in the field "Class." You then need to press ENTER to take effect. Example

What makes a NIB available to pick from the NIB Name drop down in XCode

I am wondering what makes a NIB ( xib ) available to pick from the NIB Name menu.
I created a new UIViewController Subclass called Login.xib which comes with an XBI, implementation and header file, but it does not show up in that list.
I am just trying to understand what makes a nib available from that drop down ..
Thanks

Resources