This is the code of a simple Hello World application. As you can see I uploaded the .m and .h file and there are a lot of comments on each method.
If you need any further help for your application you should definitely check out Apple’s own developer section
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
//
// HelloWorldViewController.m
// HelloWorld
//
// Created by Joris Dubois on 19/02/12.
// Copyright (c) 2012 Xios Hogeschool Limburg. All rights reserved.
//
#import "HelloWorldViewController.h"
@interface HelloWorldViewController ()
@end
@implementation HelloWorldViewController
@synthesize nameField;
@synthesize lblLabel;
@synthesize userName = _userName;
//Autogenerated code.
//Triggered when the view is loaded.
- (void)viewDidLoad
{
[super viewDidLoad];
}
//Autogenerated code.
//Triggered when the view is unloaded.
- (void)viewDidUnload
{
[self setNameField:nil];
[self setLblLabel:nil];
[super viewDidUnload];
}
//Autogenerated code.
//Something about the rotation. Not important!
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
//Self written.
//Method that we want to trigger when we need to dismiss the keyboard.
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
//Checking if the object that triggered the method is 'nameField'.
if (theTextField == self.nameField) {
//Code to dismiss the keyboard by sending a resignFirstResponder.
[theTextField resignFirstResponder];
}
//return a boolean.
return YES;
}
//Self Written.
//Triggered when the clickButton is pressed.
- (IBAction)clickButton:(id)sender {
//The variable 'userName' has been refered to the 'nameField' text.
self.userName = self.nameField.text;
//Making a NSString named nameString and refer it to the variable 'userName'.
NSString *nameString = self.userName;
//If statement to check if the users input was "".
if([nameString length] == 0){
//Set nameString to "World".
nameString = @"World";
}
//Making a new NSString.
//initWithFormat is a method to create a new line of text by filling in a spot '%@'.
//This line of code produces the NSString: "Hello, NAME".
NSString *result = [[NSString alloc] initWithFormat:@"Hello, %@!",nameString];
//Set the text of 'lblLabel' to the result label.
self.lblLabel.text = result;
}
@end |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
//
// HelloWorldViewController.h
// HelloWorld
//
// Created by Joris Dubois on 19/02/12.
// Copyright (c) 2012 Xios Hogeschool Limburg. All rights reserved.
//
#import
//The HelloWorldViewController has implemented the .
//Why it needs to be implemented is going to be explained later.
@interface HelloWorldViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UILabel *lblLabel;
@property (copy,nonatomic) NSString *userName;
- (IBAction)clickButton:(id)sender;
@end |













