How do I add custom UIButton to nib file in XCode? - objective-c

I've just begin delving in to the world of iOS development. I am also a newbie to XCode. I've built several little test apps nows using the default UIComponents available by dragging and dropping them from the Objects library on to my .xib file and hooking up outlets and actions just to see how it all works. I wanted to take it to the next step and create my own custom painted button. I have a class named CustomButton that extends UIButton and I can programmatically add it to a view, but I would like to know how to drag and drop it on to a nib file so I can lay it out visually rather than programmatically. I know this is a beginner question but I've searched all night trying to find an example of how to do it with no success. Any help is greatly appreciated.

Drop in a UIView into your nib and set its Custom Class to your UIButton subclass.

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.

XCode 4 - create AppController standard?

I'm using XCode 4, and note that when setting up a new Cocoa Application project, you get an AppDelegate.m and .h file, as well as a .nib (.xib). Using alt-command-return, you get the 3-column editor layout, from which you can control-drag from controls to the AppDelegate.h file to create Outlets or Actions.
My question is, is it recommended to utilise these AppDelegate files to manage your interface controls ie. updating labels, acting on button presses etc. OR is it better to create an AppController class, add an object to the .xib and subclass it to AppController, modifying AppController to mange the controls? If this is the case, what are appropriate uses of AppDelegate?
I would like to say, it is just a matter of development style. What I do is use another class and change the AppDelegate to that class/view and use. Also I do not draw outlets and button on the default window. I make a view and add those as subviews to the mainWindow.

How do I add a new view to a single view application in Xcode

I started my app as a single view application. I have the view with various buttons and labels connected to code. I want to add a button that links to another view.
I found a tutorial that guided me to click on the project and add a UIView subclass. However there is no option for that ( I think because I started it as a single view application).
I'm pretty new to all this so if anyone can outline a simple way for me to do this that would be great! I am using Xcode version 4.3.2.
Thanks!
You need to add a new Objective C class. Please refer to this:
How to create new View Controllers in XCode 4.3
If you started the app as single view using xib file the to add a class just go to name of the project on the left pane and right click on it. Then choose add new file. The pop up will appear to allow you to choose what type of class you want to add. Depending to what the new view is you can choose uiview controller or uitableview controller and so on. Just make sure you check mark with xib for interface user. That should do it. Hope it helps.
Adrian

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

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.

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.