There are no active ads.

Advertisement

Introduction to iOS Development: Random Number Generator

How do you develop apps for iOS?  It’s a common question that many computer enthusiasts ask, and one we here at TechnoBuffalo hope to answer with this series of posts.

Learning how to develop software can be one of the most intimidating prospects for the average computer user, and with the growing saturation of applications in mobile marketplaces, it is becoming increasingly difficult to get your work noticed. That’s what this series is for, helping you learn iOS development from a conceptual perspective. No prior knowledge of computer programming will be necessary. Over the coming weeks, we’ll examine the iPhone’s ability to deliver immersive, intuitive content, a unique opportunity for both developers and consumers.

Today, we’ll be building a simple random number generator, a relatively simple application that will be able to randomly choose a number from a set of possible outcomes.

1. First, we must open up Xcode 4, Apple’s latest Integrated Development Environment. Select New Project and follow the on-screen prompts until you are met with the template page. Select View-based Application and make sure that you are targeting the iPhone.

2.  After following Xcode’s instructions on setting up the application, entitle the program RandomNumberGen and save the file in a convenient place, preferably one that can be easily remembered and accessed in the future.

3.  First, head over to your initialization file for your view controller (RandomNumberGenViewController.h). This is where we declare variables that will be defined in our implementation file. Begin by creating an integer, generating a label, and defining an action.

——————————————————————-

#import <UIKit/UIKit.h>

@interface RandomNumberGenViewController : UIViewController {

int number;
IBOutlet UILabel *label;

}

-(IBAction)generateNumber:(id)sender;

@end

——————————————————————-

4.  Now, we have to further define our action by jumping over to RandomNumberGenViewController.m. Here, we will begin by defining the generation tool and then making it fill in the label. For the purposes of this lesson, the generator will work from 1 to 100. Feel free to change the number yourself by simply manipulating that line of code.

——————————————————————-

@implementation RandomNumberGenViewController

-(IBAction)generateNumber:(id)sender {

number = (arc4random()%100)+1; //Generates Number from 1 to 100.
NSString *string = [NSString stringWithFormat:@”%i”, number];
label.text = string

}

——————————————————————-

5.  It’s time to work on the applications interface. Open up RandomNumberGenViewController.xib and drag in a button alongside a label. In the next step, we will define the what those elements do, but for now, just make sure that everything is lined up in a visually pleasing manner.

6.  Now, it’s time to connect the code to the interface. Double-click on File’s Owner and head on over to the Connections Inspector. This is where we can define how all of our elements work. Hold control and drag from the open circles to the appropriate places until your application’s connections appear like the image below.

7.  We’re now completely finished. Build the application and send it to the iOS Simulator, where you can randomly generate numbers for all the time in the world.

Questions?

If you have any questions about the topics discussed in this lesson, feel free to voice them in the comments below. We will do our best to ensure that you have a relatively painless experience developing for iOS. Stay tuned for the next installment of this series, which is released weekly.

To read previous installments from this series, check the links below.