31 Button control 32 switch control 33 slider control 34 segment control 34 toolbar 35 WebView
3.1 Button control
3.2 Switch Control
3.3 slider Control
3.4 Toolbar
3.5 WebView
3.1 Button control
The Button control of the iPhone can be very beautiful, and the Button can be in multiple States:
"Default State
"Highlighted State
"Selected State
"Disabled State
Effect: Create a ButtonsBackground project:
ButtonsBackgroundViewController. h file
@interface ButtonsBackgroundViewController : UIViewController { UIButton * clearButton; UIButton * smallButton;}@property (nonatomic, retain) IBOutlet UIButton * clearButton;@property (nonatomic, retain) IBOutlet UIButton * smallButton;- (IBAction) disableBut: (id) sender;@end
ButtonsBackgroundViewController. m file
@synthesize clearButton;@synthesize smallButton;- (IBAction) disableBut: (id) sender { if(clearButton.enabled == YES) { clearButton.enabled = NO; smallButton.enabled = NO; [((UIButton *) sender) setTitle:@"Enable" forState:UIControlStateNormal]; } else { clearButton.enabled = YES; smallButton.enabled = YES; [((UIButton *) sender) setTitle:@"Disable" forState:UIControlStateNormal]; }}- (void)dealloc { [clearButton release]; [smallButton release]; [super dealloc];}
When you click the Disable button, call the disableBut method. In this method, the clearButton and smallButton are switched between the available and unavailable statuses. When the status is switched, the above "title" and "status" of the Disable button must be changed ":
[(UIButton *) sender) setTitle: @ "Enable" forState: UIControlStateNormal];
Sender is the event source, that is, the clicked button object.
Interface Builder design page:
When you click the Disable button, the titles of clearButton and smallButton are changed. Therefore, you need to connect the File's Owner to the output ports of the two buttons (clearButton and smallButton.
To respond to the Disable button event, connect the disableBut event defined by the File's Owner from the Disable button.
ClearButton attribute settings
Attribute box to make the Shows Touch on Highlight is checked option selected.
When the Default State configurationoption is selected, set the "paybyimage power.pngbackground image" butbackgray.png.
When the Highlighted State Configuration is selected, set the image power.png as the click button and the background image butbackbluegray.png.
When the Disabled State Configuration is selected, set the "paybybandwidth" image powerdisabled.png background image and "butbackgraydisabled.png.
SmallButton attribute settings
When the Default State configurationis selected, set the button image butbackgray.png background image and the title "Shock ".
When the Highlighted State Configuration is selected, set the background image butbackbluegray.png and the title "Shocking ".
3.2 Switch Control
Switch, which has two statuses: true and false.
You can use this method to change the status of the switch control.
-(Void) setOn: (BOOL) on animated: (BOOL) animated
3.3 slider Control
Slider control (Slider), horizontally placed, you can touch it to change its value.
By default, the minimum value is 0 and the maximum value is 1.00, while. 50 is the initial value. We can set the value through the following method:
-(Void) setValue :( float) value animated :( BOOL) animated
Switch and slider instance
We can understand their usage by placing the switch and slider controls on the page.
Create a project SwitchSlider:
SwitchSliderViewController. h
@interface SwitchSliderViewController : UIViewController { UISwitch * mySwitch;}@property(nonatomic, retain)IBOutlet UISwitch * mySwitch;-(IBAction) handleSwitch: (id) sender; -(IBAction) handleSlider: (id) sender;@end
SwitchSliderViewController. m
@implementation SwitchSliderViewController@synthesize mySwitch;- (IBAction) handleSwitch: (id) sender { if( [((UISwitch *) sender) isOn] == YES){ NSLog(@"It's on"); } else { NSLog(@"It's off"); }} - (IBAction) handleSlider: (id) sender { NSLog(@"value: %f", ((UISlider *)sender).value); if( [((UISlider *) sender) value] == ((UISlider *) sender) .maximumValue) { [mySwitch setOn:YES animated:YES]; }}- (void)dealloc { [mySwitch release]; [super dealloc];}
Connect the output port and Action event
HandleSwitch to which the switch control is connected: action.
HandleSlider connected to the slider control: action.
Specifies the output port of the switch control.
3.4 segment Control
A Segment control is a group composed of two or more segments. It is quite independent of buttons.
Create a project Segment:
SegmentViewController. h
Define an Action event
@interface SegmentViewController : UIViewController {}- (IBAction) handleSegment: (id) sender;
SegmentViewController. m
- (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..."); }}
IB design view
Connection Action event
Connection segment control to handleSegment of File's Owner: action.
3.4 Toolbar
A toolbar (UIToolBar) is usually placed at the bottom of the screen. Multiple buttons and controls can be placed inside the toolbar.
Create a project ToolBar:
ToolBarViewController. h
Define two action events
@interface ToolBarViewController : UIViewController { IBOutlet UIActivityIndicatorView * myActivityView;}@property (nonatomic, retain) IBOutlet UIActivityIndicatorView * myActivityView;-(IBAction)onClickStartButton: (id)sender;-(IBAction)onClickOpenButton: (id)sender;@end
ToolBarViewController. m
@ Implementation ToolBarViewController @ synthesize myActivityView;-(IBAction) onClickStartButton: (id) sender {if ([myActivityView isAnimating]) {[myActivityView stopAnimating];} else {[myActivityView startAnimating] ;}}-(IBAction) onClickOpenButton: (id) sender {UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "prompt message: @ "you clicked the OPEN button" delegate: self cancelButtonTitle: @ "Done" otherButtonTitles: nil]; [alert show]; [alert release];}-(void) didReceiveMemoryWarning {[super didReceiveMemoryWarning];}-(void) viewDidUnload {}-(void) dealloc {[myActivityView release]; [super dealloc];} @ end
IB design view
Connection Action event
OnClickOpenButton connected to the Open button: action.
OnClickStartButton connected to the start button: action.
Connect to the UIActivityIndicatorView output port.
3.5 WebView
WebView can help us build Web iPhone applications. Many iPhone and iPad client programs on websites use WebView
Sent. WebView supports HTML5 and Flash.
Create a project MyWeb:
MyWebViewController. h
@interface MyWebViewController : UIViewController
{ IBOutlet UITextField * myTextField; IBOutlet UIWebView * myWebView;}@property(nonatomic, retain) UIWebView * myWebView;@property(nonatomic, retain) UITextField * myTextField;- (IBAction) changeLocation: (id) sender;@end
MyWebViewController needs to implement the Protocol UIWebViewDelegate, which is a delegate object. Delegation is a design mode that is mainly used for callback events on the iPhone. A method is defined in the delegate to implement the delegate object. The implementation of this method must be provided.
The method of UIWebViewDelegate is webViewDidFinishLoad: a URL in asynchronous mode. This method is called back when the response is returned.
MyWebViewController. m
@ Implementation MyWebViewController @ synthesize myWebView; @ synthesize myTextField;-(void) viewDidLoad {myWebView. delegate = self;}-(void) dealloc {myWebView. delegate = nil; [myTextField release]; [myWebView release]; [super dealloc];}-(IBAction) changeLocation: (id) sender {[myTextField resignFirstResponder]; NSURL * url = [NSURL URLWithString: myTextField. text]; NSURLRequest * request = [NSURLRequest requestWithURL: url]; [myWebView loadRequest: request] ;}# pragma mark WebView delegate # pragma mark --- (void) webViewDidFinishLoad: (UIWebView *) webView {NSLog (@ "% @", [webView stringByEvaluatingJavaScriptFromString: @ "document. body. innerHTML "]);} @ end
In the viewDidLoad method, myWebView. delegate = self is specified as itself.
The delegate callback function is implemented in the webViewDidFinishLoad method.
[MyWebView stringByEvaluatingJavaScriptFromString:
@ "Document.doc umentElement. textContent"]
Is to run a JavaScript script program, document. body. innerHTML to get the HTML code in the page.