I have a project in Xcode 4.5 with storyboard.
I want to use a separate xib file for one piece of UI (long story) but I don't know how to connect this to the ViewController for this file. I created a separate FooViewController which is a subclass of UIViewController. However, it won't let me set this as the class for my xib (I get a beep indicating the name is invalid)
Weirdly, I can set it to some nonsense string that isn't a class in my project!? What am I doing wrong?
Rob answered in the comments to my question. Basically I was setting the class for the View and not the File's Owner as I should have been.
Related
I come from a Cocoa touch background and there's one point I'm stuck on, I'm trying to create a ViewController from scratch and connect objects in my xib to actions in my new ViewController. I changed the file's owner to my ViewController and added outlets, and the compiler seems to be happy with what I've done.
But when I run my application, the connections seem to fail with: 'Could not connect the action start: to target of class NSApplication,' but where is this NSApplication reference hiding? To the best of my knowledge my ViewController has a class of NSViewController.
ViewController : NSViewController
I changed the file's owner to my ViewController and added outlets, and the compiler seems to be happy with what I've done.
Sure, the compiler won't care -- this is a run-time error. It sounds like you're working in your MainMenu.xib file. That file is loaded by the application object. Even though you changed the type of File's Owner in the .xib, the object that's loading the .xib is still the application.
You'll probably want to create a separate .xib file for your view controller to load. You change the type of File's Owner in that file to you view controller class! hook things up to File's Owner, and it should work fine since your view controller really will be the object that's loading the file.
This question already has an answer here:
Changing xib name [duplicate]
(1 answer)
Closed 8 years ago.
I am using iOS 5 and Xcode 4.2. In my project i have a UIViewController with xib. Now i want to give seperate name for UIViewController and xib.
Is it Possible?
After changing the name of UIViewController the controllers showing warning and i can not connect connect controllers to this class.
Within your header file (SomeViewController.h), highlight the class name and from Xcode menu > Edit > Refactor > Rename
And follow the on screen instructions.
Good Luck.
Renaming a class file names and interface files are possible. However, renaming class name by refactor option on Xcode is appreciated than doing it manually.
For refactoring GOTO: "symbol navigator(next to project navigator)">>
right click on your file name >> "REFACTOR">> RENAME.
Having different names for classes and interface do not bring up issues, however its the standard to keep the interface file name same as that of the corresponding class files.
You just change the .h and .m file names by click on those files. What ever new name you gave to those files use that name while importing those files other than that don't change those name any where. try it once..
After changing the name of UIViewController the controllers showing
warning and i can not connect connect controllers to this class.
Find the code where you're instantiating the view controller. It should look something like:
MyViewController *mvc = [[MyViewController alloc] initWithNibName:... bundle:...];
When you find that line, look at the values you're passing for the nib name and bundle. Chances are, you'll need to change the nib name that you're passing in. Either you're passing an incorrect name, or you're passing nil. If if the first case, simply correct the name. If you're passing nil, again, just change that to reflect the actual name of the .xib file. (It's common to use nil because UIViewController will use the class name as the nib name if you pass in nil, so nil is a handy shortcut.)
Hi initially I had an application where the app delegate defined the initial view controller to be an .xib of some other class. I deleted this class, its header and its .xib because I wanted the initial view controller to be of another class.
In the app delegate class I deleted the original reference from the class I did not want and typed
self.window.rootViewController = [[NewClass alloc] initWithNibName:#"NewView" bundle:nil];
This works like a charm as the initial view is now "NewView". However the console gives me an error of the title above. I did a search on the whole project of the deleted class and there are no references to it. Furthermore I even deleted the actual files. Also, I deleted the simulator's cache.
Thanks in advance.
Well the error says that there is an unknown class in IB. So look through all of your xib/storyboards. I am almost certain a view, controller, etc... (whatever your subclass was of), is declared using that class.
Searching all of your files or code for that class will not help since it is an Interface Builder error, so check around there.
Beginning with XCode 4.2, when you create an empty project using XCode 4.2, a MainWindow.xib is no longer created and hooked up for you. I've found an article that describes how to do this and I've done it and it works, but if this process has taught me anything, it has shown me that I have no idea how main(), AppDelegate and the MainWindow.xib exist together.
http://www.trappers.tk/site/2011/06/16/mainwindow-xib/
Why is MainWindow.xib class updated to UIApplication?
Why is an object placed on the xib, and then AppDelegate class is selected for the class?
Why is the delegate outlet of the File Owner connected the AppDelegate object?
Why is the the window outlet of the AppDelegate to the Window?
why does - (BOOL) application:didFinishLaunchingWithOptions: need to be commented out.
I've researched around but, I'm still not 100% sure how everything is loaded up once the application starts, and why this is setup is needed just to have a MainWindow. I have a feeling though that I should probably get all these concepts down to continue to advance in iOS development.
Why is MainWindow.xib class updated to UIApplication?
At the lowest level, nib files are loaded with the method -[UINib instantiateWithOwner:options:]. The File's Owner in a nib file is a placeholder. In Xcode, it isn't a specific object yet. It will resolve to an actual object when the nib file is loaded. Its purpose is to relate, via outlets and actions, objects inside of the nib with the object that loaded the nib. The object passed as the "instantiateWithOwner:" parameter of that UINib method is what the File's Owner placeholder in Interface Builder will resolve to.
UIApplication loads the nib file specified in the info.plist and passes 'self' for the owner parameter when loading the nib file.
By setting the class name, you're just hinting to the tools so that they can suggest the set of actions and outlets you're allowed to establish.
Why is an object placed on the xib, and then AppDelegate class is selected for the class?
When you place the generic object in the xib and change its class to 'AppDelegate' you're telling Xcode to instantiate an instance of 'AppDelegate' when the file is loaded.
Why is the delegate outlet of the File Owner connected the AppDelegate object?
UIApplication has a 'delegate' that it delegates responsibility to and notifies when interesting events occur. When you make this connection, you're setting the delegate property of the application to be the instance that you specified above. From that point on, this instance will receive the delegate messages from UIApplication, like -application:didFinishLaunchingWithOptions:.
Why is the the window outlet of the AppDelegate to the Window?
Outlets are a way to refer to objects inside of a xib. They cause properties or instance variables to be set to refer to the object pointed to by the outlet. When you make this outlet, you're making it so that the app delegate instance you created above has a way to refer to the window that's also created when the xib is loaded.
why does - (BOOL) application:didFinishLaunchingWithOptions: need to be commented out.
It represents the code-focused way to do some of the same things that are happening in the xib, and if they were both present, they would be overwriting each other.
The object placed on the XIB file is the AppDelegate because it delegates all connections in the Interface Builder, meaning if you write a method that when a button is clicked it displays text, that method would be connected to either the App Delegate or the File's Owner, preferably the delegate. The File's Owner, since it is the UIApplication, connects to the delegate because it assigns that certain object to be the App Delegate. Sorry that I couldn't answer the rest of your questions, they didn't really make sense.
Hope this helps
So here is what my interface looks like at the moment:
Here is what I have changed it to in Interface Builder:
This is what is shown after I run it in Xcode:
Obviously the two programs are not communicating - if someone could point me in the right direction it would be great.
Thanks heaps!
If stuff isn't in sync, try cleaning your build. Product>Clean should do the trick.
The programs communicate through the NIB/XIB files. Make sure you have saved your changes from Interface Builder before rebuilding in XCode (this does not happen automatically). Also double check that the file Interface Builder is editing is the exact same file (not a copy) of the one in your XCode project.
Hope this helps.
This happens if you rename a nib but forget to change name of nib name passed in to a ViewController in its initWithNibName: bundle initialiser.
For example. If I have a nib named ViewOne.xib which I'm passing in to a ViewController like this:
ExampleViewController *exampleViewController = [[ExampleViewController alloc] initWithNibName:#"ViewOne" bundle:nil];
And I change the name of the nib to ViewTwo, Xcode isn't smart enough to amend this reference in the initialiser, so now a xib that noi longer exists is being passed in to the ViewController. For reasons that I cannot fathom, despite the fact there is no longer a nib called ViewOne.xib, Xcode maintains some sort of ghost of the file and you won't get an error because of the missing nib. Cleaning and deleting derived data did not get rid of this ghost reference, at least in my case.
The fix is easy - just amend the nib name in the initialiser to your new name:
ExampleViewController *exampleViewController = [[ExampleViewController alloc] initWithNibName:#"ViewTwo" bundle:nil];