今天要介绍一下更多的控键使用, 当然也会对上一篇说的控件做一个巩固, 所以这一篇涉及到的控键会包括 UIImage、UITextField、UIButton、UILabel、UISwitch、以及 UIActionSheetDelegate的使用。
BIDViewController.h
#import@interface BIDViewController : UIViewController // 声明使用到UIActionSheetDelegate@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;@property (weak, nonatomic) IBOutlet UIButton *doSomethingButton;- (IBAction)textFieldDoneEditing:(id)sender;- (IBAction)backgroundTap:(id)sender;- (IBAction)sliderChanged:(UISlider *)sender;- (IBAction)switchChanged:(UISwitch *)sender;- (IBAction)toggleControls:(UISegmentedControl *)sender;- (IBAction)buttonPressed:(id)sender;@end
BIDViewController.m
#import "BIDViewController.h"@interface BIDViewController ()@end@implementation BIDViewController- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.sliderLabel.text = @"50"; // 为UILabel缚值 UIImage *buttonImageNormal = [UIImage imageNamed:@"whiteButton.png"]; // 根据图片的名字获取UIImage对象 UIEdgeInsets insets = UIEdgeInsetsMake(0, 12, 0, 12); // 定义一个参数分别是上左下右的矩行框 UIImage *stretchableButtonImageNormal = [buttonImageNormal resizableImageWithCapInsets:insets]; // 定义图片的拉伸区域, 有点儿像 .9图片哦 [self.doSomethingButton setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal]; // 设置按钮的背景图 UIImage *buttonImagePressed = [UIImage imageNamed:@"blueButton.png"]; UIImage *stretchableButtonImagePressed = [buttonImagePressed resizableImageWithCapInsets:insets]; [self.doSomethingButton setBackgroundImage:stretchableButtonImagePressed forState:UIControlStateHighlighted]; // 设置按钮的背景图, 不同的是这个背景图在按钮触发的显示}- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}- (IBAction)textFieldDoneEditing:(id)sender { [sender resignFirstResponder]; // 放弃第一响应者, 让键盘隐藏}- (IBAction)backgroundTap:(id)sender { [self.nameField resignFirstResponder]; // 放弃第一响应者, 让键盘隐藏 [self.numberField resignFirstResponder];}- (IBAction)sliderChanged:(UISlider *)sender { int progress = lroundf(sender.value); // 获取slider的值并且进行四舍五入的折算 self.sliderLabel.text = [NSString stringWithFormat:@"%d", progress]; // 傅值label}- (IBAction)switchChanged:(UISwitch *)sender { BOOL setting = sender.isOn; // 获取当前switch的值 [self.leftSwitch setOn:setting animated:YES]; 为switch设值 [self.rightSwitch setOn:setting animated:YES];}- (IBAction)toggleControls:(UISegmentedControl *)sender { // 0 == switches index if (sender.selectedSegmentIndex == 0) { self.leftSwitch.hidden = NO; // 隐藏 self.rightSwitch.hidden = NO; // 隐藏 self.doSomethingButton.hidden = YES; // 显示 } else { self.leftSwitch.hidden = YES; self.rightSwitch.hidden = YES; self.doSomethingButton.hidden = NO; }}- (IBAction)buttonPressed:(id)sender { UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Are you sure?" delegate:self cancelButtonTitle:@"No Way!" destructiveButtonTitle:@"Yes, I’m Sure!" otherButtonTitles:nil]; [actionSheet showInView:self.view]; // UIActionSheet是在iOS弹出的选择按钮项}// UIActionSheet 消失的时候调用- (void)actionSheet:(UIActionSheet *)actionSheetdidDismissWithButtonIndex:(NSInteger)buttonIndex{ // UIActionSheet 消失不是由于点击cancelButton触发 if (buttonIndex != [actionSheet cancelButtonIndex]) { NSString *msg = nil; if (self.nameField.text.length > 0) msg = [NSString stringWithFormat: @"You can breathe easy, %@, everything went OK.", self.nameField.text]; else msg = @"You can breathe easy, everything went OK."; // 创建UIAlertView对象 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Something was done" message:msg delegate:self cancelButtonTitle:@"Phew!" otherButtonTitles:nil]; [alert show]; }}@end