Figure 12-16 A view with a switch and a slider
Figure 12-17 The application running in iPhone Simulator
Figure 12-18 Moving the slider to maximum value changes the switch’s value
Listing 12-3 SwitchSliderViewController.h #import <UIKit/UIKit.h> @interface SwitchSliderViewController : UIViewController { IBOutlet UISwitch * mySwitch; } @property(nonatomic, retain) UISwitch * mySwitch;
@end Listing 12-4 SwitchSliderViewController.m #import "SwitchSliderViewController.h" @implementation SwitchSliderViewController @synthesize mySwitch; - (IBAction) handleSwitch: (id) sender { if( [((UISwitch *) sender) isOn] == YES){ NSLog(@"It's on"); } else { NSLog(@"It's off"); } }
if( [((UISlider *) sender) value] == ((UISlider *) sender) .maximumValue) { [mySwitch setOn:YES]; } }
} @end
The iPhone uses the UITextField class to render text fields. A text field is associated with a keyboard that appears when a user taps in the text field. Keyboard styles include Number Pad, Phone Pad, URL, and several others. You can set the keyboard style programmatically or using Interface Builder (Figure 12-19). Figure 12-20 illustrates several other text field properties you might set. You can specify how text should be capitalized. Valid choices are None, Words, Sentences, and All Characters. You can also specify if the text field should attempt to correct spelling errors by setting the correction property to YES. If you wish the text field to be a password, then check the Secure checkbox in Interface Builder. Other properties you might wish changing are the text field’s
Figure 12-19 Setting a text field’s keyboard type
border, background, font, and return key for the keyboard. Figure 12-20 shows the valid choices for a keyboard’s return key. For a complete listing of properties and method available to you, refer to the UITextField Class Reference.
Figure 12-20 Valid choices for a keyboard’s return key
Figure 12-21 The number pad has no Done key.
Figure 12-22 Button is under two textfields
Listing 12-5 TextFieldViewController.h #import <UIKit/UIKit.h> @interface TextFieldViewController : UIViewController { IBOutlet UITextField * numberField; } @property(nonatomic, retain) UITextField * numberField;
@end
Figure 12-23 Changing button’s type to Custom
Figure 12-24 Connecting the numberFieldDone action to the button
Figure 12-25 The number pad appears and disappears from the application
Listing 12-6 TextFieldViewController.m #import "TextFieldViewController.h" @implementation TextFieldViewController @synthesize numberField;(IBAction) textFieldDone: (id) sender { [sender resignFirstResponder]; }(IBAction) numberFieldDone: (id) sender { [numberField resignFirstResponder]; }(void)dealloc { [numberField release]; } @end
Use a UITextView to capture multiple lines of text. Apple’s reference documentation describes this control best: ―The UITextView class implements the behavior for a scrollable, multiline text region.‖ There is not really much more you can say about a text view, other than it’s a text area,it is generally used for entering paragraphs of text rather than a single line. There are several properties you can set to customize the control’s appearance; including, the font, textColor, editable, and textAlignment properties. You can also check if it has text using the hasText method. Figure 12-26 illustrates several properties you might wish setting for a UITextView in Interface Builder. For more information on the UITextView, refer to the UITextView Class Reference.
Figure 12-26 UITextView properties you might wish setting in Interface Builder
A segmented control groups together two or more segments, where each segment acts as an independent button. The next task illustrates a segmented control. Using a UISegmentedControl
Figure 12-27 Modifying the segmented control in Interface builder
Figure 12-28 The application running in iPhone Simulator
contains the debugger console’s logging. Listing 12-7 SegmentViewController.h #import <UIKit/UIKit.h> @interface SegmentViewController : UIViewController { } - (IBAction) handleSegment: (id) sender; @end Listing 12-8 SegmentViewController.m #import "SegmentViewController.h" @implementation SegmentViewController - (IBAction) handleSegment: (id) sender { UISegmentedControl * myseg = (UISegmentedControl *) sender; if(myseg.selectedSegmentIndex == 0) { NSLog(@"selected zero index..."); } else if(myseg.selectedSegmentIndex == 1) { NSLog(@"selected one index..."); } else { NSLog(@"selected two index..."); } } - (void)dealloc { [super dealloc]; } @end Listing 12-9 The debugger console’s logging for application [Session started at 2009-03-15 15:31:19 -0400.] 2009-03-15 15:31:50.201 Segment[366:20b] selected one index... 2009-03-15 15:32:13.027 Segment[366:20b] selected two index... 2009-03-15 15:32:36.908 Segment[366:20b] selected zero index...
You liked the article?
Like : 0
Vote for difficulty
Current difficulty (Avg): Medium
1/15
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.
Stay Updated
Get stories of change makers and innovators from the startup ecosystem in your inbox