OSX / macOS - how to migrate a xib to the storyboard - macos

I have an app with a mainmenu.xib and for various reasons I want to migrate it to a storyboard. Is there a way to do this without creating a new project?

This is how to do it:
Create a new project with a storyboard. We're just going to use it to copy one little thing which can't be found in the object library.
Back in your existing project, add a new storyboard ui file (e.g. Main.storyboard) in your existing project. Unlike when starting a new project, the storyboard file will be entirely blank.
From the new project's storyboard, copy the "Application Scene" and paste it into the existing project's storyboard. This is a workaround due to not being able to get an application scene from the object library. After doing this, check that the App Delegate object's "delegate" property there has a referencing outlet to the "Application" by looking in the Connections inspector (right side panel). If it isn't, link them together.
In the object library (right side panel > object library below), add in the window controller. This will add the window controller scene and the view controller scene.
Reference the appropriate classes for the window controller / window / view controller / view if you are using custom classes.
Open your earlier xib file and copy in any views you used before.
In your existing project's xcproj file, go to the info section for the build target and replace the xib dictionary key/value with "NSMainStoryboardFile" for the key and "Main" (or whatever your storyboard file was called sans fileextension) for the value.
Extra notes:
If you use custom viewcontrollers and are migrating them to the storyboard, ensure they allow for being init'ed via initWithCoder.
If you need to acquire the viewcontroller from the appdelegate, you can don't do it via NSStoryboard's instantiateWithName methods, that will create a new instance. Instead do the following:
NSApplication *application = [NSApplication sharedApplication];
NSWindow *window = application.windows[0];
self.myCustomViewController = (MyCustomViewController *)window.contentViewController;

Related

How to find the editor for multi view controllers

I am very new to xcode, got stuck in finding the editor to modify the code when created multiple view controllers. Whenever I click on any of the view controllers, I only see same editor contents and cant move to the others for coding.
I searched the web and stackoverflow but could not find a clear guideline on how to manage the editors for multiple view contollers. I have attached a snapshot.
You don’t “find” code. You create it.
When you drag a view controller into the storyboard, it has the default class of UIViewController. It is up to you to make a new code file where you subclass UIViewController, and then to go back to your storyboard and change the view controller to that subclass in the Identity inspector.

Unable to see custom classes in Interface Builder drop down

I'm using Xcode 6.1.1, and cannot select my custom class from the drop down. Because of this, I believe it is causing several other related issues (see below).
Symptoms:
When using the IB drop down to choose a custom class, none of the custom classes appear.
IB_DESIGNABLE and IBInspectable do not work: When selecting the control in IB, the "Designable" status does not appear; none of the inspectable properties appear either. Debug selected views option is grayed out when selecting a view which is defined as IB_DESIGNABLE.
Ctrl-dragging items to create connections (IBOutlet and IBAction) from IB to source code occasionally doesn't allow you to "drop" the connection into the class's source code (as if there is a class mismatch). (Note: This assumes you manually typed in the class name in the Custom Class section.)
Suspected to be related: WatchKit: unable to find interface controller class
How can I fix this?
Things that worked:
Try on another machine. (This leads me to believe the machine has some setting that is messing this up.)
Reinstall Xcode.
Moving the project to a new location (in this case a git repository), fixed it once.
Things I tried that didn't work (but have worked for others):
Restart Xcode
Restart machine (this worked once before, not this time)
Create a new storyboard.
Create a new subclass (not just rename it).
Create a new project via Apple's single view template.
Cleaning the project
Deleting derived data
Reindex the project
Remove localization on the storyboard file.
Things I tried that didn't work:
Naming the subclass according to Apple's conventions (e.g. instead of View use ABCTestView).
Import the .h of the class in the .h and .m of the view controller.
Try on another version of Xcode, which is already installed (beta 6.2).
Related discussions:
https://discussions.apple.com/thread/3054574?start=15&tstart=0
Storyboard uiviewcontroller, 'custom class' not showing in drop down
This wasn't your specific situation but I was in a similar "rip my hair out" moment. When creating custom controller classes and using the interface builder, you must make sure to click the yellow button at the top of the view you're working with (the view controller button). Otherwise the custom ViewController class won't show up in the custom class drop down menu within the identity inspector.
One thing that worked for me after inexplicably seeing the issue where the "Designables" row would not appear in the Custom Class section of the Identity Inspector:
Before (not functioning: Designables did not appear and Interface Builder did not render my class):
IB_DESIGNABLE
#class MyCustomClass;
#interface MyCustomView : UIView
After (functioning: Designables did appear and Interface Builder did render my class):
#class MyCustomClass;
IB_DESIGNABLE #interface MyCustomView : UIView
So it appears that Xcode is very sensitive to the order of things.
This is what worked for me:
I somehow lost the view reference, so all i did was to drag from a little circle "New Referencing Outlet" to the main view of the .xib, and BOOM!
Here are some possible solutions:
When using the IB drop down to choose a custom class, none of the custom classes appear.
Manually type the name of your custom class instead of trying to find it in the dropdown. Sometimes IB will autocomplete the name of the class as you type, especially if you follow Apple's conventions, i.e. YourView as a subclass of NSView.
IB_DESIGNABLE and IBInspectable do not work: When selecting the control in IB, the "Designable" status does not appear; none of the inspectable properties appear either. Debug selected views option is grayed out when selecting a view which is defined as IB_DESIGNABLE.
If the view does not begin as a Custom View either dragged from the Object library or created from Editor > Embed In > ..., for some reason changing the Custom Class in the Identity inspector doesn't make a difference. To fix this, right-click the .xib and choose Open As > Source Code. Search for the view you want to fix (giving your view a label that is easily identifiable in IB will make this easier). You will find an entry like this:
<view ... customClass="YourView">
...
</view>
Change view to customView so that the entry resembles:
<customView ... customClass="YourView">
...
</customView>
then right-click the .xib again and choose Open As > Interface Builder XIB Document and you should now see a Designables entry under Custom Class in the Identity inspector of IB and Debug Selected Views will be available under the Editor menu.
Ctrl-dragging items to create connections (IBOutlet and IBAction) from IB to source code occasionally doesn't allow you to "drop" the connection into the class's source code (as if there is a class mismatch). (Note: This assumes you manually typed in the class name in the Custom Class section.)
Doesn't sound like your exact problem, but on a dual-monitor/multi-monitor setup, if IB is on a different monitor from the source code window, go to Apple menu > System Preferences... > Mission Control and uncheck Displays have separate Spaces. This may have some visual side-effects (like window drop-shadows bleeding into other monitors) but it will fix the problem of ctrl-dragging onto a separate monitor.

Xcode 4.3: how to create a nib linked to a view controller

Sorry, I'm quite new with xcode with a quite big project.
I upgrade xcode to 4.3.
I have a project (that doesn't use storyboard) and I need to add a view controller with a nib file.
Before upgrading I did File --> Ne File --> Cocoa Objective-C subclass and xcode created for me the m/h and their nib file.
Now that that option is disappear, how can I add a view controller?
I created the Objective-C class and, separately, a nib file, but how can I link the H/m files to the nib??
How can the m file call is nib??
Please can you help me? The project is real big and I can rewrite the code with the storyboard option!
Thanks
When editing your XIB, go to the Identity Inspector (it's the third button show in the following image). Then where it says Class, choose the controller you want to associate with the XIB.

Binding to NSCollectionViewItem crashes Xcode

Using XCode 4.1 I've come across a crash whenever I try to use a CollectionView.
These are the steps I'm doing:
From a new project, edit the MainMenu.nib file.
Add an NSCollectionView to the window, which also adds a Collection View
Item, and a new View In the new View
Add a control like an NSTextField
Select the control, and switch the inspector to the binding pane.
Attempt to bind something to the Collection View Item.
Once the Collection View Item is selected in the dropdown menu, an
"Internal Error" dialog appears. Attempting to continue from this dialog makes the
binding pane disappear until you restart XCode
Does this happen for anyone else or is it just something I'm coming across?
Is there a way to work around it to allow me to bind to the Collection View Item (I want ultimately to bind to the representedObject value) either in XCode or in code.
I attempted to create a custom NSCollectionViewItem subclass that uses a separate nib file and set that as the itemPrototype of the NSCollectionView but things went very wrong then.
I found that splitting the collection view item view into its own XIB and then rewiring the connections so that the collection view item prototype loads the new XIB will allow for you to create the bindings in interface builder without it crashing. I followed these steps...
Delete the collection view item view from the default xib.
Create a new view and XIB inheriting from NSCollectionViewItem.
Create your collection view item in the new view.
Bind the fields to the files owner of the new view.
Back in the collection view xib, update the properties of collection view item to load the bin name of the new xib.
I'm not sure that it's quicker than doing it programmatically, but it does allow you to manage the bindings without writing code. I've got a few apps working this way now.
I've found a temporary work around:
Select the "Collection View Item" and under the "Attributes Inspector" → "View Controller" settings, set "Nib Name" to "MainMenu".
Once you've done this, it won't crash, and you can set the bindings. Be sure to clear the "Nib Name" setting when building your app.
Yup, I can confirm this bug too, even on Interface Builder 3.
The only workaround is to do the binding programmatically:
[textField bind:#"value" toObject:collectionViewItem withKeyPath:#"representedObject.foo" options:nil];

How to create new window/form in MonoTouch/MonoDevelop/Interface Builder. (Mac)

I have created my first application in MonoDevelop. I'm having difficulty to create a second window (form).
If I create a new UIView it has .cs and .designer.cs files attached to it. It looks good, but it doesn't have appdelegate to attach events to.
If I use the new UIWindow I don't have .cs and .designer.cs files attached to it. And I don't have delegates either.
If I copy the original MainWindow and rename it, it almost works, I have delegate and .designer.cs but it complains about duplicating certain objects. Like "Window", "AppDelegate"
So how am I supposed to create some new forms that needs some buttons and events to deal with?
In an iPhone app, you should generally only have 1 window.
Create a new UIViewController with your desired elements, and present it as a "modal" view controller over your other controllers. It will function similar to ShowDialog(), except it won't be a blocking call.
The methods are PresentModalViewController() and DismissModalViewControllerAnimated().
Here is a decent example.

Resources