Creating the View
One label and two buttons
One butoon will generate the seed, another button will generate the random number, and the label shows the random number generated by the app.
Drag a Label from the library Pane Controls section to the View Window.
Drag two rounded rect buttons
Click the top button and label the button Seed Random Number Generator
quickly and easy connect our Outlests and Actions to our code.
Click the Assistant Editor icon at the top right of the screen.This will display the .h file for the XIB file we are working on ,
Using Outlets
Insert curly brackets for your instance variables.
.h file
A popup window will appear .This enables us to name and specify the type of Outlet.
@interface ViewController : UIViewController
{
IBOutlet UILabel *randomNumber;
}
@end
As a reminder,outlets(pointers) are declared in our object's interface file and connected to specific instance variables.
simply as an outlet.
Outlets signal your controller that this instance variable is a pointer to another object that is set up in Intrface Builder, we can connect these instance variables to the appropriate object.
Connecting Actions and Objects
connect the object actions to the buttons
Control-Drag from the Seed Random Number Generator button to below the last curly bracket and drop.
connection is an Action
Repeat Step 8 for the Generate Random Number button.
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
IBOutlet UILabel *randomNumber;
}
_ (IBAction)seed:(id)sender;
_(IBAction)generate:(id)sender;
@end
Implementation File
ViewController.m file and complete the seed: and generate: method
_ (IBAction)seed:(id)sender{
srandom(time(NULL));
[randomNumber setText: @"Generator seeded"];
}
_ (IBAction)generate:(id)sender {
int generated;
generated = (random() % 101);
[randomNumber setText:[NSString stringWithFormat:@"%i",generated]];
set the UILabel value in your view.
}
sets the UILabel value in your view.
XIB files
Model-View-Controller
Architectural pattern
Human interface guidelines(HIGS)
Outlets
Actions