其实只要想想IOS 里面的Setting and Clock apps , 就能一次review all the UI element, event ,customizer,delegate,etc. 其实他们的使用方式基本是相同的,只是具体到不同的UI Element 时有不同的属性罢了,
当然我们还是要逐个总结,以后方便reference.
2.1 Displaying Alerts with UIAlertView
The best way to initialize an alert view is, of course, by using its designated initializer:
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Title"
message:@"Message"
delegate:nil
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Ok", nil]; //注意nil 不能省啊
Alert views can take various styles. The UIAlertView class has a property called alert
ViewStyle of type UIAlertViewStyle:
typedef enum {
UIAlertViewStyleDefault = 0,
UIAlertViewStyleSecureTextInput,
UIAlertViewStylePlainTextInput,
UIAlertViewStyleLoginAndPasswordInput
} UIAlertViewStyle
If you need to get notified when the user interacts with the alert view, specify a delegate
object to your alert view. This delegate must conform to the UIAlertViewDelegate protocol.
The most important method defined in this protocol is the alertView:clickedButtonAtIndex: method, which gets called as soon as the user taps on one of the buttons in the alert view. The button index is passed to you through the clickedButtonAtIndex parameter.
- (void) alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex{
NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:[self yesButtonTitle]]){
NSLog(@"User pressed the Yes button.");
}
else if ([buttonTitle isEqualToString:[self noButtonTitle]]){
NSLog(@"User pressed the No button.");
}
}
{
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Credit Card Number"
message:@"Please enter your credit card number:"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Ok", nil];
[alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
/* Display a numerical keypad for this text field */
UITextField *textField = [alertView textFieldAtIndex:0]; // 根据index get the UITextField
textField.keyboardType = UIKeyboardTypeNumberPad;
}
2.2 Creating and Using Switches with UISwitch
@property (nonatomic, strong) UISwitch *mySwitch;
//
- (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];
}
[self.mySwitch setOn:YES];
{
[self.mySwitch addTarget:self
action:@selector(switchIsChanged:)
forControlEvents:UIControlEventValueChanged];
}
(void) switchIsChanged:(UISwitch *)paramSender{
NSLog(@"Sender is = %@", paramSender);
if ([paramSender isOn]){
NSLog(@"The switch is turned on.");
} else {
NSLog(@"The switch is turned off.");
}
}
2.3 Customizing the UISwitch
Simply use one of the tint/image customization properties of the UISwitch such as the
tintColor or the onTintColor.
In both iOS 5 and iOS 6 SDKs, Apple has done a fantastic job of bringing customization
to UI components such as the UISwitch. In previous SDKs, developers were going as
far as subclassing UISwitch just to change its appearance and color. Now, iOS 6 SDK
makes this much simpler.
There are two main ways of customizing a switch:
Tint Colors
Tint colors are colors that you can apply to a UI component such as a UISwitch.
The tint color will be applied on top of the current color of the component.For
instance, in a normal UISwitch, you will be able to see different colors. When you
apply the tint color on top, the normal color of the control will be mixed with the
tint color, giving a flavor of the tint color on the UI control.
Images
A switch has two images:
On Image
The image that represents the on state of the switch. The width of this image
is 77 points and its height is 22.
Off Image
The image that represents the switch in its off state. This image, like the on
state of the switch, is 77 points in width and 22 points in height.
let’s get started by learning how we can change the tint color of the switch UI component. This can be achieved
by the use of three important properties of the UISwitch class:
tintColor
This is the tint color that will be applied to the off state of the switch. Unfortunately,
Apple has not taken the time to name this property offTintColor instead of tint
Color to make it more explicit. This property is of type UIColor.
thumbTintColor
This is the tint color that will be applied to the little knob on the switch. This
property is of type UIColor.
onTintColor
This tint color will be applied to the switch in its on state. This property is of type
UIColor as well.
/* Adjust the off-mode tint color */
self.mainSwitch.tintColor = [UIColor redColor];
/* Adjust the on-mode tint color */
self.mainSwitch.onTintColor = [UIColor brownColor];
/* Also change the knob's tint color */
self.mainSwitch.thumbTintColor = [UIColor greenColor];
/* Customize the switch */
self.mainSwitch.onImage = [UIImage imageNamed:@"On"];
self.mainSwitch.offImage = [UIImage imageNamed:@"Off"];
2.4 Picking Values with UIPickerView