3. Hello World

Hello World

This module will explain how to use an array, a label, and a button. We will again work off the code we have used in the previous Intensity and RGB Color sliders modules. To start your DOViewController.xib file must be on the main screen, if it’s not, double click on it from the project explorer on the left hand side.

The next step involves putting a label and a button component into the project. This is done the same way as the other modules. The label component can be found in the bottom right side and can be dragged on to the view controller. You can double click on it to change the label text to “Hello World.” This component must be linked like the others.

The .xib file should look about like this when you are finished:

<Insert Screen Shot>

Now we need to link the label and the button components to the view controller and create the method to handle the button press. This is done in the same manner as was accomplished with the other components. Begin by clicking on the tuxedo icon in the top right, this will bring up the header file for this view controller. Now click on the label component and press control and drag over to the header file into the UIViewController body. It will give you a prompt to give it a name.

You will follow the same procedure to the button in order to add the button, but this time you will do it again to right underneath the body, in order to create a method for when the button is pressed for the button component in the .m file.

Next add an array into the header file which holds the hello world string in different languages. Then type NSArray into the header file.

The header file should look something like this now:

@interface DOViewController : UIViewController
NSArray *helloWorldLanguages;
IBOutlet UILabel *redLabel;
IBOutlet UILabel *greenLabel;
IBOutlet UILabel *blueLabel;
IBOutlet UILabel *intensityLabel;
IBOutlet UISlider *redSlider;
IBOutlet UISlider *greenSlider;
IBOutlet UISlider *blueSlider;
IBOutlet UISwitch *onOffSwitch;
IBOutlet UISlider *intensitySlider;
IBOutlet UILabel *helloLabel;
IBOutlet UIButton *pressMe;
– (IBAction)colorSliderStateChanged:(id)sender;
– (IBAction)switchStateChanged:(id)sender;
– (IBAction)intensitySliderStateChanged:(id)sender;
– (IBAction)pressMeButtonTouchUp:(id)sender;

Now we are going to put the following code into DOAppDelegate.m and DOViewController.m to initialize the DOViewController.

– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[DOViewController alloc] initWithNibName:@”DOViewController_iPhone” bundle:nil];
} else {
self.viewController = [[DOViewController alloc] initWithNibName:@”DOViewController_iPad” bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;

– (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
[self becomeFirstResponder];
helloWorldLanguages = [NSArray arrayWithObjects:@”Hello World”, @”Konnichi wa sekai”, @”Hola Mundo”, nil];
return self;

The variable helloWorldLanguages holds the hello worlds in different languages as an array.

Now implement the method to handle the button press. The button press is named pressMeButtonTouchUp in the header file, so you can implement in the .m file.

– (IBAction)pressMeButtonTouchUp:(id)sender
static int languageLocation = 0;
helloLabel.text = (NSString*)[helloWorldLanguages objectAtIndex:languageLocation%helloWorldLanguages.count];

The languageLocation is a variable for an index for the array helloWorldLanugages. Every time a user presses the button, the languageLocation will be count up. Then, the value of the languageLocation divided by the length of the array will be the index for the array.

This is the basic function, but as we do not want the labels to disappear when the intensity slider is set close to or at 0, we also do not want the hello world label to disappear. So, you will modify the intensitySliderStateChanged method.

– (IBAction)intensitySliderStateChanged:(id)sender
[self.view setBackgroundColor:[self.view.backgroundColor colorWithAlphaComponent:intensitySlider.value]];
if (intensitySlider.value < 0.5)
[intensityLabel setTextColor:[UIColor whiteColor]];
[helloLabel setTextColor: [UIColor whiteColor]];
[intensityLabel setTextColor:[UIColor blackColor]];
[helloLabel setTextColor: [UIColor blackColor]];

The components created in DOViewController should be released in viewDidUnLoad. In order to do this you will release the label and the button which we added for this module.

– (void)viewDidUnload
onOffSwitch = nil;
intensitySlider = nil;
redSlider = nil;
greenSlider = nil;
blueSlider = nil;
redLabel = nil;
greenLabel = nil;
blueLabel = nil;
intensityLabel = nil;
helloLabel = nil;
pressMe = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s