Main.storyboard not connecting to viewcontroller.swift - xcode

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".

Related

Adding a second View Controller to storyboard

I'm learning Cocoa, and
I'm trying to understand the logic (behind the scenes) of the IB.
1) On a new xcode project (cocoa/obj-c) the storyboard starts with a
predefined View Controller (VC) which is "associated" with files ViewController.{h,m}.
2) I add a Push Button with name "Switch" to the the View.
3) I add a Second View Controller (VC2)
4) I connect (click-drag) "Switch" --> VC2 which create a "segue" VC-->VC2.
So far so good. If I click "Switch" on VC, the window of VC2 appears.
5) Now I want to add a label with name "Foo" to VC2 and connect
"Foo" to some IBOutlet in my code.
I'm guessing that I need to create a new class "ViewController2"
which inherits from NSViewController and make the connection
VC2 <--> ViewController2
in such a way that I can click-drag from "Foo" to the interface
of ViewController2 in ViewController2.h in order to create
an IBOutlet.
Question (finally): how do I make the connection
VC2 <--> ViewController2?
More generally: is there a good reference for understanding
the logic behind IB?
You are correct that you have to create a NSViewController subclass for the second view controller. The next step to take is to set the second view controller's class to your subclass. Set the view controller's class using the identity inspector.
After setting the second view controller's class to your subclass, you should be able to create outlets and make connections from user interface elements in the second view controller to that view controller's source code file.

Swift, storyboards and core data...a missing persistentstorecoordinator?

I am working on a new Cocoa project using Swift, Core Data and storyboards, and have come across a problem that makes no sense to me. After some fairly extensive hunting around, including on this site, I have come to the conclusion that I must be missing something obvious, but cannot figure out what. Here is what I have done so far:
1.Create a new project, Cocoa Application, using Swift, Storyboards, and Core Data.
2. Create an entity in the .xcdatamodeld. Let’s call it Dataset.
3. Create a subclass of NSSplitViewController (for what i want to do in the rest of the program).
4. Set the window content of the main window to and instance of myVC. I checked, and it loads up and displays fine.
5. In the viewController.swift, get the managedObjectContext like so:
#IBOutlet var moc:NSManagedObjectContext!
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
let appDelegate = NSApplication.sharedApplication().delegate as AppDelegate
moc = appDelegate.managedObjectContext
println("mainsplitviewcontroller moc:")
println(moc)
println("mainsplitviewcontroller psc:")
println(moc.persistentStoreCoordinator)
NSLog("Main split view loaded")
}
(yes, I have about dependency injection, but I want to solve this problem first).
In IB, put a managedObjectContext object in the View Controller instance.
In IB, connect the myVC outlet for the variable moc to the managedObjectContext.
In IB, create an array controller. Set it To Entity. Entity Name is Dataset. Turn on Prepares Content.
Either as an outlet or a binding, connect the array controller to the MOC. Using outlet, just dragging from managed object context in it's right-click popup to the icon for the MOC created in 6 above. For bindings, the old fashioned way, going to the bindings tab, and under Parameters, Bind to: (view controller), Model Key Path: moc. (moc is from 5 above)
Then, I build and run. and I get the error: "Cannot perform operation since managed object context has no persistent store coordinator."
This happens whichever way I try to do 9, above.
Now, the thing is, from my println statements, the objects referred to in both the app delegate and the viewcontroller are the same, both for the managed object context and for the persistent store controller, as below:
appdelegate moc:
appdelegate psc:
mainsplitviewcontroller moc:
mainsplitviewcontroller psc:
I wish I could show images, but I am new here and so cannot do that. Am I doing something clearly wrong? I thought I understood the process: make sure the VC can access the MOC, then put the MOC object into the VC's window in IB, make it an outlet, and connect it to an array controller. Why would the swift file for the view controller seem to show that the PSC is the same as the app delegate, but in IB, the array controller think the MOC has no PSC at all?
Thanks for reading!
I don't know if this is going to help, but I'm not surprised that your project shows that error. You have two managed object contexts - one created by your app delegate, and one created by the storyboard. Your interface code is connecting to that second MOC, which is not connected to your persistent store.

xCode can't Control-drag into .m file

I'm trying to change the results field in the sample app SimpleURLConnections in the AppleDeveloper Library from an Image View Object in the GetController.m to a Text View so that it will display responses that aren't images. (I know some code that checks the response type also needs to change.)
When I drag a Text View object (or ANY object) from the Utilities pane to the MainWindow.xib in the editor pane, it appears to be created ok but when I try to Control drag from the Text View into the #property area of GetController.m, I don't get any "Insert Outlet.." popup or new entry in the GetController.m file? (By experimentation, I can get it to populate an IBOutlet in the AppDelegate.m file.
What am I doing wrong?
Thanks,
Rick
It may be because you are doing it wrong.You added the textview to mainWindow and Getcontroller is a controller entity[its a view controller i think.] .An xib will be connected to the outlet if and only if it has the custom class correctly set to that class.
Add a view controller with custom class in attribute inspector set to that Getcontroller and then you can connect the outlet
When u drag the control to the **.m , u should press the key "control",

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

Create a custom view using a nib and use it in a window

I have been struggling quite a bit with this problem and I can't seem to figure it out by myself.
This is the situation:
I want to have a custom Nib-based view, with its own ViewController. I then want to add this view to a window. Therefore the window should contain my custom view.
So I go about and create a very simple example like this:
In Xcode 4 I create a new blank document-based Cocoa application
I create a new Cocoa Class which should extend from NSViewController (which causes the nib to be created along with the .h and .m file. The name of the created .h, .m und .xib file is FaceViewController for testing purposes it should only display text for now.
I open the NSViewController.xib in InterfaceBuilder and add a multi-line text component and place it in the custom view, which is already in the xib file. I make the text span the whole view.
In MyDocument.xib I also add a Custom View place holder, which should be replaced with my custom FaceView.
From this starting point on, everything I tried to include the created View + ViewController on my MyDocument.xib failed and the area where my text should be shown remains empty (just like the background of the underlying window.
So my question really is what I need to do, so that my FaceView gets loaded into the Custom View which I placed on MyDocument.xib. Here are some things which are unclear to me:
The custom View extends from NSView so I wanted to change this to my FaceView but the view does not exist as a class but is defined in InterfaceBuilder in the xib, do I need to set this to anything other than NSView?
Do I have to alloc, init the FaceViewController in code or is it enough to drag it into either of my two .xibs?
Can I use InterfaceBuilder to place my FaceView or do I have to do this programmatically?
I really thought that creating a custom view like this would be a piece of cake but it turned out quite the opposite so far, so any help would be greatly appreciated.
You can create your FaceViewController either by adding one to the MyDocument.xib or by creating it with alloc, init.
To place your FaceView you'll have to do it programmatically, you can use
[customView addSubview:[FaceViewController view]];
of if you want to replace the view
[customView replaceSubview:oldView with:[FaceViewController view]];

Resources