I am currently reading 'Beginning iOS 5 Games Development: Using the iOS SDK for iPhone, iPad and iPod Touch' by Lucas Jordan. In this book, there is a section in where you are instructed to make a 'Rock, Paper, Scissors' game using a variety of UIViewControllers. My problem is that I cannot create an IBOutlet from the ViewController_iphone.xib (which, as the name suggests, is made for the iPhone) to the ViewController.h file that comes with every new project.
In ViewController_iPhone.xib I have created a UIView and set the file's owner of the .xib to ViewController_iPhone.xib. When i ctrl+click and try to link the view to ViewController.h, it simply does not give me the option to do so. When I change the file's owner to ViewController, it is not a problem to create IBOutlets in ViewController.h, however that is not the correct file's owner that will allow the program to work correctly.
I have downloaded the source code for the book, and the author of the book seemed to have no problems whatsoever creating the oultlets. I have compared my project to his and I can't seem to find what is wrong.
If anybody could help me, I would be very grateful.
Thanks!
Fitzy
You should set the file owner to the name of the associated UIViewController which is ViewController in your case.
In general, if you set the file owner to XViewController, then you can only link IBoutlets to that view controller.
The MVC model, Model-View-Controller model, isn't intended to have an action in one view touch the controller of another view. In InterfaceBuilder, you should only ever be able to attach actions to the controller for that specific view.
What you may want is some way to relay information from one view controller to another -- I tend to use delegates for that, but without knowing more about what you're doing, I don't know if that's the correct answer.
Related
I am hoping that someone else has run into this problem before.
In an Xcode Cocoa Application project I create a *.xib file with a single view, and some random sub views. I create a *ViewController.m and inherit from NSViewController. I set the file owner in the *.xib to my controller and set the outlet view to the main view. I then create a new controller instance in the AppDelegate's applicationDidFinishLaunching: and add its view as a child to self.window.contentView.
Here is the weird part... this works JUST fine in a fresh Xcode Cocoa project. The view from the *.xib is added to the main window as expected. However in the project I am currently working on I get [NSViewController loadView] loaded the "(null)" nib but no view was set no matter what I do. At one point in time this worked in this project, it now errors every time. I have even reverted the entire repo from an old Git commit! I assume i'm seeing an Xcode bug?
EDIT More info:
This seems to be connected to having the project as a child of another Xcode project. If I open the project individually (Rather than the parent project) it runs as expected.
I had a similar problem: I wrote a ViewController then added a xib and changed the File's Owner there. Then I added a ViewController object in my MainMenu.xib and changed the Custom Class of this object to my ViewController.
What I forgot and what caused the same exception: In the MainMenu.xib I had to click on the ViewController object and in the Attributes Inspector I needed to specify the Nib Name which should be loaded when the ViewController gets created. Sadly Xcode needed a restart for getting my changes.
Anyway, I hope this helps everyone who checked every view connection twice but still got the exception.
Although not an explanation of why it was happening, I found out how to fix this. I changed the entire project over to a workspace and my runtime error vanished. Originally it was a parent project, with 2 children projects.
Well knowing well that this code SHOULD work.
I did a simple experiment.
I simply recreated the xib for that particular item and the issue went away.
So IMHO this is a bug from apple.
I sure have a long list of them piling up here and there.
...
This whole thing started after i noticed very slow performance on the NSCollectionView, originally i had the cells into their own nib, but it appears this is not needed anymore, since you can define the custom NSCollectionViewItem in the same xib as the NSCollectionView.
Cocoa will then call a copyWithZone: and will be fine.
I guess my fault for not having adapted to some of the new patters from Apple.
Alright, I will try and make this short and sweet. I recently created my first iOS app, and in my app I decided to go the storyboard route by selecting the checkbox when creating the project. On a side note, I recently just started developing in Xcode, keep that in mind. So I started by designing the GUI elements of my app, and before I knew it, I had 8 scenes in my storyboard file and one view controller. Needless to say the view controller has been populated with code from different scenes thus making it difficult to understand what does what in the view controller. In the spirit OO design principles, I thought it would be a good idea to create a separate view controller for each scene. So I created some class files for the project. When I try to associate the newly created class file with the scene my computer just sounds a beep / donk sound. I am trying to associate the newly created class file to a scene by selecting the scene in the storyboard / Interface Builder view, then displaying the Utilities pane on the right, then selecting the Identity Inspector at the top of the Utilities pane, then setting the Custom Class to my newly created class file, but when I type the name of the class and press enter I just hear a beep.
If any one has any insight or knows of a tutorial explaining this process please post. Part of the reason I am trying to do this is for code readability, better code management, and a better code structure for the application. I came across this stack thread explaining some of what I am talking about.
Also here's a picture of what my project looks like if that helps shed any light.
You need to consider the parent class of your controllers, UIViewController for example.
To do so, you must check the .h file and your xib/nib file.
I. In your .h file, you will be seing:
#interface ViewControllerWelcome : NSObject
Change 'NSObject' to 'UIViewController' - this will mean that ViewControllerWelcome has a parent class UIViewController.
II. In your nib/xib file:
1. Click on the controller that you are going to set from the storyboard.
2. Go to interface builder and click the "Identity Inspector" (third item from the left) from the Utilities panel.
3. You need to specifically set each controller's name (eg. ViewControllerWelcome)
Do these to all controllers from your storyboard.
Here's something you can read about ViewControllers and Storyboards.
I'm totally new with iOS development, having background with ActionScript3 with MVC. I love to code and I don't want to use any NIB/XIB files for my project (I've been told I will learn Objective C faster then) and I'm looking for a good example of iOS application using MVC pattern from scratch.
I know there is a bunch of good examples (like Good example code for Objective-C) but I haven't found any with no NIB/XIB usage.
Any ideas?
I'll try to give you some guidelines.
First of all. If you need some good tutorials, I suggest you to watch Brad Larson or Standford courses on iTunes. They are absolutely fantastic.
Then, if you want to create your MVC from scratch, I suggest you to take a look at UIViewController class reference.
Each UIViewController is a controller as the name suggest. The model could be contained in the controller itself (e.g. a NSArray) or provided by an "external" entity (e.g. Core Data). Each controller has a view property. The view is that element that is presented on screen. Usually could be provided by XIB or Storyboard files. As the apple documentation suggested:
If you cannot define your views in a storyboard or a nib file,
override the loadView method to manually instantiate a view hierarchy
and assign it to the view property.
In other words, within your view controller class you need to add this:
- (void)loadView
{
UIView* myCustomView = ...
self.view = myCustomView;
}
By means of this, you have a full control of the view presented on screen. This means that you need to provide the sizing and the positioning of the elements of your view. While a similar arrangement can be done by means of an user friendly interface in XIB or Storyboard files, you need to do it manually in other cases (e.g. deal with frame, autosizing mask, etc.).
Hope it helps.
Just a newbie with iPhone development.. I just have a small problem but huge confusion with regards to NIB files. In Cocoa, how can we change the File's owner and First responder of a nib? And BTW, when can we actually say that we need more than one NIB file for a project in the first place?
Change the class of File's owner in Interface builder (select File's owner and open Identity inspector). You don't need to change anything in First responder.
As to your second question - you can build an entire project programmatically without nibs. I build mine with nibs for every view, controller, custom control etc.
Open the nib, click on "File's Owner"
Select the third inspection tool, "Identity Inspector" (?)
Type in the class this nib needs to be associated with.
I have basic *.xib file, which have NSView.
How can I use another nib files for this Custom View? What is NSViewController and how should I use it?
Generally, you create a new nib, selecting "View" as the nib template. Then you select the File's Owner and set its class to NSViewController (or your own subclass of it, in which case you may have to add the nib to your Xcode project first) in the last tab of the Inspector. Then you connect the view controller's "view" outlet to the view.
You should read Apple's docs on NSViewController, it's actually a very simple class. However, before you start drawing and coding I would suggest you should carefully structure your app in MVC terms. If you make a mistake in the design phase, you will have to redo a lot of your work later. Using view controllers is not always justified, it depends on the complexity of the app.
NSViewController as its name suggests is a Controller class, means it connects the View to the Model, in a perfect MVC environment.
Each ViewController is bound to one View, you can build that view in code or using the Interface Builder.
For more help, I would suggest to watch Stanford University iPhone programming course, it's available on Stanford iTunes (iTunes link), iPhone SDK share the same underground with the OSX SDK so it's exactly the same for ViewControllers.