Sure, you can drag and drop a button onto your windows using IB, but they do not look nice, nor is that as much fun as doing it from scratch.
Using the loadView method we will load fancy background by loading a nice image. But first we need to declare some variables. In the viewController.h file we need to set up our contentView.
UIImageView *contentView;
Now in our viewController.m file we will set up our window inside the loadView method.
contentView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[contentView setImage:[UIImage imageNamed:@"image.jpg"]];
[contentView setUserInteractionEnabled:YES];
self.view = contentView;
[contentView release];
Now that our windows is all nice and pretty, we can add our button(s). For the purpose of this tutorial, we will be making one button in the center of the window.
Now, lets create the button.
UIButton *startButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 180.0f, 130.0f)];
Now we can add an image as the background of the button
[startButton setBackgroundImage:[[UIImage imageNamed:@"squareButton.png"] stretchableImageWithLeftCapWidth:110.0 topCapHeight:0.0] forState:UIControlStateNormal];
Now lets center our button
[startButton setCenter:CGPointMake(195.0f, 208.0f)];
Then you need to add the button to the view
[contentView addSubview:startButton];
So, there we go, a nice looking button, well depending on your photoshop skills. So your loadView should look like this:
- (void)viewDidLoad {
contentView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[contentView setImage:[UIImage imageNamed:@"284.jpg"]];
[contentView setUserInteractionEnabled:YES];
self.view = contentView;
[contentView release];
UIButton *startButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 180.0f, 130.0f)];
[startButton setBackgroundImage:[[UIImage imageNamed:@"squareButton.png"] stretchableImageWithLeftCapWidth:110.0 topCapHeight:0.0] forState:UIControlStateNormal];
[startButton setCenter:CGPointMake(195.0f, 208.0f)];
[startButton addTarget:self action:@selector(playSound:) forControlEvents:UIControlEventTouchUpInside];
[contentView addSubview:startButton];
[super viewDidLoad];
}
Hope you enjoyed
~Jeremy
I came across this little gem at about 1am. Thanks to Matt Ball, this takes an NSView and NSControl to make this cool spreadsheet…. thing…
Anyway, here is the Link
I was reading a magazine the other day and came across an article about this game engine. It is called the Torque Game Engine. With it, you can make games for Mac, PC, iPhone, Xbox 360, and the Wii. You can download the free trial and check out the documentation.
I came across this up-and-coming OS. It is a linux (i think) distro made especially for netbooks. Take a gander at their twitter page.
http://twitter.com/jolicloud
If you want to follow me on twitter:
http://www.twitter.com/jlegendre
You may already know that Stanford is offering an iPhone Programming class. Well, after some googleing, i found the lesson plan online, so here it is, I’m sure this will come in handy for at least some of you out there
If you ever need help with anything from Security, to a Cisco cert, to how to design your website. Check out Dyar Help.
After working with the iPhone SDK, and reading bits and pieces of the iPhone Developer Cookbook (thanks George). I have created a Password Generator, Amazon Book Search, and a Weather App.
If you like the site and have found it helpful, the apps will be available on the App Store for 99 Cents (US Dollars) when I relieve the email from Apple with my Info. All proceeds go to hosting the site and buying equipment to make this site work.
I will post links when they are up.
Lets say you have a program that needs to run something and you don’t want the user to keep clicking and clicking and clicking because the program will crash if that happens, well here is the answer to your problems:
[buttonName setTitle:@"Running..."];
[buttonName setEnabled:NO];
//code goes here
[buttonName setTitle:@"Original Title"];
[buttonName setEnabled:YES];
Aleksander Grande wrote this amazing guide that steps you through everything from IB to the code. I wish this was around when I started programming for the iPhone.


