Starting from scratch, I learned iOS development (6): IOS control (3), segmented control, switch

Source: Internet
Author: User

This study continues based on the previous project (you can also create a new project) to learn segmented control and switch.

Segmented Control

Switch

The main difference between segmented control and switch is that segmented control can have multiple values for selection, while switch has only two values.

1) Add segmented Control
Drag a segmented control from the object library to the iPhone interface
Then adjust the position of the segmented Control and Its width, as shown in figure

One attribute in the segbutes inspector of segmented control is segments. This value is used to set the number of segments. The default value is 2. You can set this value to 100, see how it works.

Like changing the text content of the label control, double-click the text on Segmented control to change its content, change "first" to "switches", and change "second" to "button", as shown below:

1) Add two switches
Drag two switches from the object library to the iPhone interface, as shown below:

The attribute of the switch does not need to be changed. Next, create the outlet and action

3) add outlet and action
Add outlet for the two switches respectively, then the two switches share an action, and the segmented control uses an action
Add an outlet for the switch on the left and name it leftswitch. Add an outlet for the switch on the right and name it rightswitch.
 
Add an action for the switch on the left and name it switchchanged. After adding the action, associate the switch on the right with switchchanged.

Add an action for segmented control and name it togglecontrols

The added bidviewcontroller. H is as follows:

#import <UIKit/UIKit.h>@interface BIDViewController : UIViewController@property (weak, nonatomic) IBOutlet UITextField *nameField;@property (weak, nonatomic) IBOutlet UITextField *numberField;@property (weak, nonatomic) IBOutlet UILabel *sliderLabel;@property (weak, nonatomic) IBOutlet UISwitch *leftSwitch;@property (weak, nonatomic) IBOutlet UISwitch *rightSwitch;- (IBAction)textFieldDoneEditing:(id)sender;- (IBAction)backgroundTap:(id)sender;- (IBAction)sliderChanged:(id)sender;- (IBAction)switchChanged:(id)sender;- (IBAction)toggleControls:(id)sender;@end

(There are many other outlets and actions on them. This is why the previous project was used)

Bidviewcontroller. m is as follows:

#import "BIDViewController.h"@implementation BIDViewController@synthesize nameField;@synthesize numberField;@synthesize sliderLabel;@synthesize leftSwitch;@synthesize rightSwitch;...- (IBAction)switchChanged:(id)sender {}- (IBAction)toggleControls:(id)sender {}

4) Implement switch action
Add the following code to the switchchanged method in bidviewcontroller. M:

- (IBAction)switchChanged:(id)sender {    UISwitch *whichSwitch = (UISwitch *)sender;    BOOL setting = whichSwitch.isOn;    [leftSwitch setOn:setting animated:YES];    [rightSwitch setOn:setting animated:YES];}

Because both leftswitch and rightswitch are associated with switchchanged, this method is called no matter which switch is operated. In this method, the sender is forcibly converted to the uiswitch type, in this way, you can use the properties defined by uiswitch. ISON is used to determine whether the switch is enabled or disabled (the switch has only two statuses). It is best to use the Switch Status of the trigger event, the other switch is also set to the same State. For convenience, the program does not determine which switch triggers the action, but simply sets the two switches to the same State.

[rightSwitch setOn:setting animated:YES];

The Seton method sets whether to enable or disable the switch based on the values of the following bool parameters ).
Animated indicates whether the slider of a switch is active after switching from one status to another. If yes, the slider slides slowly, the slider moves slowly. If it is set to no, the slider quickly changes its position and slides fast.

Compile and run the program, click the switch control in the simulator with the mouse to see the effect. The values of the two switches should always be the same, and no matter which switch is clicked, the value of another switch also changes.
Switch is on

Switch is off

5) Add a button
The role of segmented control in this application is to switch the switch and button, and the switch and button can only have one display at the same time, and the other must be hidden. Therefore, we add a button in the same position of the switch, set the property of the button to "hidden", and the program will not be displayed at the beginning. to display the button, you only need to switch the segmented value. The button will be displayed and the two switches will be hidden.

Add a button at the same position of the two switches.

Stretch the button to completely hide the two switches, double-click the button, and enter "do something"

6) add outlet and action for the button
The reason for adding outlet is that we need to change segmented to control the release of the show or hide button. When a button is clicked, the action will be triggered.
Add outlet: dosomethingbutton

Add action: buttonpressed

7) Hide the button
The reason for setting the hidden attribute of the button is that it makes it easier to add the outlet and action of the button and is easy to select.
Select the button, find the "hidden" checkbox in the View bar of attributes inspector, and select

After hidden is selected, the button becomes invisible, but in the layout interface of the iPhone, the button only becomes transparent, and its appearance is still visible. In the real running environment, the button is invisible.

8) Implement the segmented action
The segmented action tooglecontrols has been added. Add the following code in it:

- (IBAction)toggleControls:(id)sender {    if ([sender selectedSegmentIndex] == 0) {        leftSwitch.hidden = NO;        rightSwitch.hidden = NO;        doSomethingButton.hidden = YES;    }    else {        leftSwitch.hidden = YES;        rightSwitch.hidden = YES;        doSomethingButton.hidden = NO;    }}

The first if statement uses the selectedsegmentindex method of segmented control to return the value of segment (that segment) selected (the index of the block in segmented control starts from 0 from left to right ), if there are 0th switches, the switch segment is selected. Therefore, the left and right switches need to be displayed and the buttons need to be hidden. Therefore, set the hidden attribute value of the two switches to no (false, do not hide). The button must be hidden. set its hidden value to Yes (true, hidden ). If not, it indicates that 1st segments are selected (only 2 segments are included in this program, but 0th segments are selected or 1st segments are selected). 2 switches need to be hidden and buttons need to be displayed.

Compile and run the program. In the initial stage, segment 0th of segmented is selected, and two switches are displayed.

Select the segmented button and hide the two switches. The button is displayed.

9) The next article will implement the action of the button
So far, all newly added controls, except the button action, have implemented their functions. The button action will be completed in the next article. Here, the button action involves two new controls: Action sheet and alert, which are somewhat special, instead of dragging the Object Library to the iPhone interface, they will use delegate (in fact, delegate has not been clearly understood so far, but is still a bit confused. I hope you can give me some advice ), it is still a bit complicated for me, so I will talk about it in the next article. I hope you will understand it.

 

Control fun 3

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.