Ios development from scratch (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
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
3) add Outlet and Action
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) identifier :( 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
-(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 ).
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.
5) Add a Button
Add a button at the same position of the two switches.
6) add Outlet and Action for the button
7) Hide the button
8) Implement the segmented Action
-(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.
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.

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.