博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ios成长之每日一遍(day 3)
阅读量:5754 次
发布时间:2019-06-18

本文共 4682 字,大约阅读时间需要 15 分钟。

今天要介绍一下更多的控键使用, 当然也会对上一篇说的控件做一个巩固, 所以这一篇涉及到的控键会包括 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

 

转载地址:http://ywckx.baihongyu.com/

你可能感兴趣的文章
Java 重载、重写、构造函数详解
查看>>
【Best Practice】基于阿里云数加·StreamCompute快速构建网站日志实时分析大屏
查看>>
【云栖大会】探索商业升级之路
查看>>
HybridDB实例新购指南
查看>>
C语言及程序设计提高例程-35 使用指针操作二维数组
查看>>
华大基因BGI Online的云计算实践
查看>>
排序高级之交换排序_冒泡排序
查看>>
Cocos2d-x3.2 Ease加速度
查看>>
[EntLib]关于SR.Strings的使用办法[加了下载地址]
查看>>
中小型网站架构分析及优化
查看>>
写shell的事情
查看>>
负载均衡之Haproxy配置详解(及httpd配置)
查看>>
标准与扩展ACL 、 命名ACL 、 总结和答疑
查看>>
查找恶意的TOR中继节点
查看>>
MAVEN 属性定义与使用
查看>>
shell高级视频答学生while循环问题
查看>>
使用@media实现IE hack的方法
查看>>
《11招玩转网络安全》之第一招:Docker For Docker
查看>>
hive_0.11中文用户手册
查看>>
hiveserver2修改线程数
查看>>