How to associate new class files to be a view controller for a specific scene? - objective-c

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.

Related

Insert Outlets in AppDelegate or Custom Cocoa Class?

I was an iOS programmer and recently switched over to making Cocoa apps for Mac. When creating a project in Xcode, it seems there are two options:
(1) Using the MainMenu.xib default, insert buttons and link to AppDelegate. I have tried creating an NSView class and linking to that with no success.
(2) Creating an NSViewController class and linking the view from MainMenu.xib and any buttons to that.
Which of these methods is better when creating larger applications, with multiple views and windows?
Edit: What would be the best way to start out if not connecting anything to AppDelegate? Create an NSWindowController class with an xib and connect its view to a NSViewController class?
I guess overall I am confused as to which class I start with and what elements (window, views, buttons) to connect to what class file.
You can create a window object in your MainMenu xib and create a reference to it in your AppDelegate class. You will be able access items in it. But this is good only for a little learning.
You should start with an NSWindowController. For a first try, don't make any view controllers yet. Use the xib file and build your first window in the subclass of NSWindowController.
In AppDelegate, create an instance of the new class and open it.
Once you have mastered that, you can define view controllers for parts (or all) of you window's view.
If you need sample code, let me know, but it's a good exercise to work it out.

iOS, objective C - good example of application without XIB with MVC

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.

creating IBOutlets in ViewController.h from another UIViewController's .xib file

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.

Putting an ADBannerView on top of a UINavigationController

I'm trying to put an iAd banner in an app that is based on a UINavigationController (it's not the standard nav-base app proposed by xcode, cause I don't need the table view).
I'd like to place an ADBanner on its bottom, to be always visible, no matter how the user pops and pushes views.
I studied the iAdSuite example in the apple sample code, but, though it's reported among the "best practices", I don't think it's the best practice for what I need. It basically declares an ADBannerView in the app delegate class and then implements the ADBannerViewDelegate methods for every single view the app needs. That means implementing the ADBannerViewDelegate methods over and over again on every view controller class you need! It doesn't seem too smart... :(
I'd rather prefer to have an approach more similar to what Apple itself does in the tab bar based app, where you have a part of the window always occupied by the tab controller and all the views switching above without affecting the tab bar view below.
You can't directly put an ADBannerView along with the nav controller in the app delegate, because ADBanner needs to be placed in a view controller (you get a runtime error otherwise).
I tried to subclass from UIViewController, implementing the ADBannerViewDelegate in this class, and place it in the rootViewController along with a UINavigationController but I'm not having good luck with this approach...
Has anybody found a good, simple way to to this? Any hint?
Thank you for any help...
You can have just one class for ADBannerViewDelegate, and just one instance of ADBanner itself. When the currently active view changes, remove ADBanner from the old view, add it as a subview to the new view.
EDIT:
to clarify, you don't need each view implement the ADBannerViewDelegate. You only should have one class implement it (that class doesn't have to be a view controller for that matter).
You would also need to maintain a somewhere a property that would point to the currently active view (e.g. you can update that property in your Navigation Controller's navigationController:didShowViewController:animated:, or come up with your own protocol for that if your views appear in a more complex way).
Then in your ADBannerViewDelegate you'd just resize the view currently pointed to by that current view property. The actual view doesn't even have to know it has an ad in it ;)

NSView inside self window

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.