Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (3.67 MB, 89 trang )
Figure 2-7. UISwitch used in the Settings app on an iPhone
In order to create a switch, you can either use Interface Builder or you can simply create
your instance in code. Let's do this through code. So, now the challenge is: finding the
class where we have to place our code in. We need to place our code in a View Controller's class. We don't yet know what a View Controller really is but depending on
the name of your project, the view controller's .h (header) file name ends with ...ViewController.h. For instance, I have named my project Creating and Using Switches with
UISwitch and the .h (header) file of my view controller is called Creating_and_Using_Switches_with_UISwitchViewController.h. Open that file now.
122 | Chapter 2: Implementing Controllers and Views
Let's create a property of type UISwitch and call it mySwitch:
#import
@interface Creating_and_Using_Switches_with_UISwitchViewController
: UIViewController
@property (nonatomic, strong) UISwitch *mySwitch;
@end
Now let's go to the view controller's implementation file (.m file) and synthesize our
mySwitch property:
#import "Creating_and_Using_Switches_with_UISwitchViewController.h"
@implementation Creating_and_Using_Switches_with_UISwitchViewController
@synthesize mySwitch;
...
We are now in a place where we can go ahead and create our switch. Find the viewDid
Load method in your view controller's implementation file:
- (void)viewDidLoad{
[super viewDidLoad];
}
Now let's create our switch and place it on our view controller's view:
- (void)viewDidLoad{
[super viewDidLoad];
/* Make sure our view is white */
self.view.backgroundColor = [UIColor whiteColor];
/* Create the switch */
self.mySwitch = [[UISwitch alloc] initWithFrame:
CGRectMake(100, 100, 0, 0)];
[self.view addSubview:self.mySwitch];
}
So we are allocating an object of type UISwitch and using the initWithFrame: initializer
to initialize our switch. Note that the parameter that we have to pass to this method is
of type CGRect. A CGRect denotes the boundaries of a rectangle using (x, y) position of
the top left corner of the rectangle and the width and height of it. We can construct a
CGRect using the CGRectMake inline method where the first two parameters passed to
this method are the (x, y) positions and the next two are width and height of the rectangle.
After we've created the switch, we are simply adding it to our view controller's view.
2.2 Creating and Using Switches with UISwitch | 123
In this example, we are changing the background color of our view controller's view to white (as opposed to the default Single View Application's gray background color), just to make our app look nicer.
Now let's run our app on iPhone Simulator and see what happens:
Figure 2-8. A switch placed on a view
124 | Chapter 2: Implementing Controllers and Views
As you can see, the switch's default state is off. We can change this by changing the
value of the on property of the instance of UISwitch. Alternatively, you can call the
setOn: method on the switch, as shown here:
[self.mySwitch setOn:YES];
We can also use the setOn:animated: method of the switch. The animated parameter
accepts a boolean value. If this boolean value is set to YES, the change in the switch's
state (from on to off or off to on) will be animated, just as if the user was interacting
with it.
Obviously, you can read from the on propert of the switch to find out if the switch is
on or off at the moment. Alternatively, you can use the isOn method of the switch, as
shown here:
if ([self.mySwitch isOn]){
NSLog(@"The switch is on.");
} else {
NSLog(@"The switch is off.");
}
If you want to get notified when the switch gets turned on or turned off, then you will
need to add your class as the target for the switch, using the addTarget:action:forCon
trolEvents: method of UISwitch, as shown here:
[self.mySwitch addTarget:self
action:@selector(switchIsChanged:)
forControlEvents:UIControlEventValueChanged];
And then we will go ahead and implement the switchIsChanged: method. Note that
when the runtime calls this method for the UIControlEventValueChanged event of the
switch, it will pass the switch as the parameter to this method so we can find out which
switch has fired this event:
- (void) switchIsChanged:(UISwitch *)paramSender{
NSLog(@"Sender is = %@", paramSender);
if ([paramSender isOn]){
NSLog(@"The switch is turned on.");
} else {
NSLog(@"The switch is turned off.");
}
}
Now go ahead and run the app on iOS Simulator. You will see messages similar to this
in the console window:
Sender is =
frame = (100 100; 79 27);
layer =
The switch is turned off.
Sender is =
2.2 Creating and Using Switches with UISwitch | 125
frame = (100 100; 79 27);
layer =
The switch is turned on.
See Also
XXX
2.3 Picking Values with UIPickerView
Problem
You want to allow the users of your app to select from a list of values.
Solution
Use the UIPickerView class.
Discussion
A picker view is something that allows you to display series of values to your users and
allow them to pick one. The Timer section of the Clock app on the iPhone is a great
example of this:
126 | Chapter 2: Implementing Controllers and Views
Figure 2-9. A picker view on top of the screen
As you can see, this specific picker view has 2 components. One is on the left and one
is on the right. The left component is displaying hours (such as 0 hours, 1, 2, etc) and
the component on the right is displaying minutes such as (18, 19, 20 mins, 21, 22, etc).
So these two items (one on the left and the other on the right) are called components.
Each component has rows. Any item in any of the components is in fact repreented by
a row, as we will soon see. For instance, 0 hours, 1 and 2 are, each and all, rows in the
left component.
Let's go ahead and create a picker view on our view controller's view. If you don't know
where your view controller's source code is, please have a look at the Recipe 2.2 recipe
where this subject is discussed.
First let's go to the .h (header) fileof our view controller and define our picker view:
#import
@interface Picking_Values_with_UIPickerViewViewController
: UIViewController
2.3 Picking Values with UIPickerView | 127