Sunday, August 15, 2010

Adding an image to a UIView / UIView initWithFrame

In my most recent project, I created a tab bar application. The default tab bar application in XCode is set up oddly compared to normal applications in that you're not working with XIBs and view controllers, but only XIBs (to begin with).

In retrospect, I think I should have created a UIView within each XIN through interface builder and then a UIViewController for each of those. However, I skipped the whole model-view-controller thing and created m files that inherited from UIView. I directed the XIBs to inherit from my explicit UIViews.

This method worked perfectly fine until I wanted to dynamically load a background image into each of those UIViews. I didn't want to apply the images in interface builder because I had already placed all my controls and I didn't feel like removing them all. I turned to Google...


self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"yourimage.png"]];


I stripped off the "view" part because I'm writing a UIView rather than a UIViewController to get:


self.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"yourimage.png"]];


I added this to the XCode generated method in each of my UIView files called "initWithFrame". The method has a section betwixt an if-statement where it directs you to place initialization code. Bingo.

But, it didn't work. Apparently, UIViews don't call initWithFrame upon initialization (or at all?); they call initWithCoder. The solution is to create a the initWithCoder method like so:


- (id)initWithCoder:(NSCoder*)coder {
if (self = [super initWithCoder:coder]) {
self.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"yourimage.png"]];
}
return self;
}

Friday, June 4, 2010

Base SDK Missing

Lately, I've been jumping around between iPhone/iPad SDKs, what with the (currently) four iPhone OS 4 beta releases. I've run into a few problems when I try to open older projects in a newer version of XCode. Common to all of them, the text "Base SDK Missing" shows up in the top-leftmost drop-down menu for choosing the device/simulator to which you're building and installing. On top of this, I have seen other symptoms here and there-- linking errors, error messages that my device doesn't have the correct OS, and so on.

What's causing this? Almost always, this means the "Target Info" for your project is messed up. To solve most any of these issues: expand the "Targets" section in the left sidebar of your project window. Right-click on your main target (sometimes there are more than one if you're also building for, say, iPad). Choose "Get Info" and select the "Build" tab. Change the "Base SDK" value to the most recent SDK that your XCode uses. Also, choose whether you want it to default to building for a device or simulator. Under "C/C++ Compiler Version", make sure you're using the latest GCC.

You're good to go.

Sunday, May 2, 2010

Upgrade current target for iPad

A common problem I have found amongst people trying to convert their current iPhone executables to an iPad-specific format is that the "Upgrade Current Target for iPad" under the Project menu bar item is grayed out. The simple solution to this is to highlight specifically the executable of your project under the "Targets" title in the left sidebar of your project in XCode. Next, go back to Project > Upgrade Current Target for iPad. It should now be enabled.

Selecting this option has several effects on your project. First, in your overview options, you can now change your "Active Target" and "Active Executable" to iPad variants on the options currently selected. Second, the action should have made a copy of all the XIBs in your Resources directory and placed them into a new directory called Resources-iPad. Note, however, that any other resources will not be copied. Also note that the XIBs will not look pretty. They've been scaled to iPad size with no reservations.

Wednesday, March 3, 2010

UITabBar events trigger

Today, I aimed to perform a task within a viewController when the user switched to it using a UITabBar. Not the most difficult thing to do, and I may have completely hacked it, but it seems like the simplest way.

First, each of your UIViewController classes should have been automatically generated containing the function initWithNibName. Uncomment these, and change the contents of the if-condition to your desired nib name and "nil" for bundle. This gives you a way to identify each controller from your app delegate.

Second, go into your app delegate and uncomment the function tabBarController. I have two viewControllers, one of which dons the nib name, "drive". Here's what I put in the function:


// Optional UITabBarControllerDelegate method
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if ([viewController nibName] != @"drive") {
viewController.touching = FALSE;
}
}

Tuesday, March 2, 2010

Saturday, February 27, 2010

OpenGL ES tutorial for the iPhone

An intro for OpenGL ES, as well as 3D programming. Just what I was looking for...

Thursday, February 25, 2010

Reusing views

To transfer views from one project to another, copy the view controller H and M files, as well as the XIB file, to the new project folder. In the new project's class view sidebar, choose to add existing files to whichever folder you want to place the files. I suppose the tip is that there's really nothing else needed, except to add the view to whichever master view or window you're using.