How Do I?
UIView, UIImage, UIImageView,
UILabel, UIButton,
Handling Touches,
NSTimer
Dismiss the keyboard
Create a UIView
A UIView represents a rectangular area of the screen that can hold UITextFields, UIButtons, UIImages, etc. You generally create a UIView in a view controller's loadView method. You then create and add buttons, text fields, etc. to the view (in your loadView method).
UIView *myView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view = myView;
[myView release];
Create a UIImage
A UIImage is an object that holds a picture. It is generally set on a UIImageView, UITableViewCell, etc.
UIImage *theImage = [UIImage imageNamed:@"mypic.png"];
Create a UIImageView
A UIImageView is an object that holds a UIImage and displays it on the screen in the object's frame.
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,381,60,30)];
UIImage *theImage = [UIImage imageNamed:@"mypic.png"];
[myImageView setImage:theImage];
myImageView.userInteractionEnabled = NO;
[myView addSubview:myImageView];
[myImageView release];
// you can change it's location on the screen by calling it's setFrame: method.
// [myImageView setFrame:CGRectMake(100,200,60,30)];
// myImageView.frame.size.width, myImageView.frame.size.height
// myImageView.origin.x, myImageView.origin.y
Create a UILabel
A UILabel is an object that displays text in a rectangular area.
UILabel *myLabel = [ [UILabel alloc] initWithFrame:CGRectMake(0,170,320,50) ];
myLabel.text = @"Hello";
myLabel.font = [UIFont systemFontOfSize:24];
myLabel.textColor = [UIColor blackColor];
myLabel.textAlignment = UITextAlignmentCenter;
myLabel.backgroundColor = [UIColor lightGrayColor];
[myView addSubview:myLabel];
[myLabel release];
Create a UIButton
A UIButton represents a rectangular button that the user can tap. When a user taps the button, a method that you write is executed.
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(0,381,60,30)];
[button setTitle:@"Larger" forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor redColor]];
[button addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
[myView addSubview:button];
// now you must write the method that you declared in the
// @selector statement (buttonTouched:)
-(void) buttonTouched:(id) theButton
{
// do whatever you want to do
}
Create a NSTimer
An NSTimer object calls a selected method every so many seconds if the repeat factor is set to YES. Otherwise it calls the selected method once if the repeat factor is set to NO.
// create the start button on a start screen
UIButton *startButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[startButton setFrame:CGRectMake(100,439,150,40)];
[startButton setTitle:@"Start Game" forState:UIControlStateNormal];
[startButton addTarget:self action:@selector(startButtonTouched:) forControlEvents:UIControlEventTouchUpInside];
[myView addSubview:startButton];
// now you must write the method that you declared in the
// @selector statement (startButtonTouched:)
- (void) startButtonTouched:(UIButton *) myButton
{
myTimer = [NSTimer scheduledTimerWithTimeInterval:1.5f target:self selector: @selector(handleTimer:) userInfo: nil repeats: YES];
}
// now you must write the method that you declared in the
// @selector statement (handleTimer:)
// Do not put this method header in your
// interface
- (void) handleTimer:(NSTimer *) mytimer
{
// do whatever you want to do here
// this method will be executed every 1.5 seconds
}
// to stop a timer
[myTimer invalidate];
myTimer = nil;
Handle a Touch
A UITouch holds information about where a touch occurred.
You can handle touches in any UIView (UIImageView, ...) by writing a subclass
of UIView (UIImageView, ...) and adding one or more of the following methods:
// don't forget to call [yourObject setUserInteractionEnabled:YES];
// declare CGPoint touchedStartPos; as an instance variable
// DO NOT put these methods in your UIViewController class
// this method is called when the UIView is touched
-(void) touchesBegan:(NSSet*) touches withEvent:(UIEvent*) event
{
touchedStartPos = [[touches anyObject] locationInView:self];
// do whatever you want to do
}
// this method is called when the UIView is dragged
-(void) touchesMoved:(NSSet*) touches withEvent:(UIEvent*) event
{
CGPoint touchedEndPos = [[touches anyObject] locationInView:self];
// do whatever you want to do
}
// this method is called when the touch of the UIView has ended
-(void) touchesEnded:(NSSet*) touches withEvent:(UIEvent*) event
{
CGPoint touchedEndPos = [[touches anyObject] locationInView:self];
// do whatever you want to do
}
Dismiss the keyboard
A keyboard pops up when a user
taps on a UITextField object or
taps on a UITextView. There are
several ways to dismiss the keyboard.
Suppose you have:
IBOutlet UITextField *nameField;
declared in your interface.
You can call the UITextField's
resignFirstResponder method
[nameField resignFirstResponder];