I. Create
- Uiswitch * myswitch = [[uiswitch alloc] initwithframe: cgrectmake (200.0, 10.0, 0.0, 0.0)];
The size is 0.0 × 0.0, and the system will automatically help you determine the optimal size. The size you write will be ignored. You only need to define the position relative to the parent view. The default size is 79*27.
Ii. display controls
- [Parrentview addsubview: myswitch]; // Add to parent View
Or
- Self. navigationitem. titleview = myswitch; // Add to navigation bar
Iii. Switch Status
The switch status can be read through its on attribute. This attribute is a bool value, indicating whether the switch is Enabled:
- Bool switchstatus = myswitch. on;
You can use the Seton method in your code to turn on or off the switch:
- [Myswitch Seton: Yes animated: Yes];
4. To receive a notification when switching the status, you can use the addtarget method of the uicontrol class to add an action for the uicontroleventvaluechanged event.
- [Myswitch addtarget: Self action: @ selector (switchvaluechanged :) forcontrolevents: uicontroleventvaluechanged];
In this way, the switchvaluechanged method will be called as soon as the target class is switched (in the previous example, the target class is the current controller self,
-(Void) switchvaluechanged :( ID) sender
{
}
Code example: The uiswitch switch is used to display the ON and OFF status on the uilable.
1. Declare two instance variables first
@ Interface mhtviewcontroller ()
{
Uilabel * _ label;
Uiswitch * _ switch;
}
@ End
-(Void) viewdidload
{
[Superviewdidload];
// Do any additional setup after loading the view.
// Create a uilabel object: _ label;
_ Label = [[uilabelalloc] initwithframe: cgrectmake (,)];
// The text displayed in the initial _ label
_ Label. Text = @"";
// Set the _ label text alignment. The default value is left alignment.
_ Label. textalignment = nstextalignmentcenter;
// Set the text font and size
_ Label. font = [uifontfontwithname: @ "Arial" Size: 50];
// Set the text size.
_ Label. font = [uifontsystemfontofsize: 20];
// Set the text color
_ Label. textcolor = [uicolorbluecolor];
// Set the number of displayed rows. If it is 0, it is automatically expanded.
_ Label. numberoflines = 0;
// Add the object to the view
[Self. View addsubview: _ label];
// Remember to put the object release
[_ Label release];
// Create a uiswitch object: _ Switch
_ Switch = [[uiswitchalloc] init];
// Set its location. Its size is 79*27 and cannot be changed.
_ Switch. Frame = cgrectmake (120,100, 0, 0 );
// Set its initial status to off,
_ Switch. On = no;
// Add the object to the view
[Self. View addsubview: _ SWITCH];
// Remember to put the object release
[_ Switch release];
// Bind an object to _ switch. When uicontroeventvaluechanged, onchange: method is triggered.
[_ Switchaddtarget: selfaction: @ selector (onchange :) forcontrolevents: uicontroleventvaluechanged];
}
-(Void) onchange :( ID) sender
{
// Forcibly convert the sender type. Sender represents the sender.
Uiswitch * tmpswitch = (uiswitch *) sender;
If (tmpswitch. On ){
// If the status is on, the _ label text is "on"
_ Label. Text = @ "open ";
} Else {
// If the status is off, the text displayed in _ label is "off"
_ Label. Text = @ "off ";
}
}
Uiswitch Switch Control