iOS开发小白学习体验-8

UISlider

UISlider 就是我们看到的滑动条,让用户能够以可视化的方式设置指定范围内的值。和按钮一样,滑块也能响应事件,还可像文本框一样被读取。如果希望用户对滑块的调整立刻影响应用程序,则需要让他触发操作。

好了废话不说,上代码

self.slider = [[UISlider alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 20)];/**< 初始化 */
    self.slider.backgroundColor = [UIColor yellowColor];/**< 设置slider的背景颜色 */

    self.slider.minimumValue = 0;/**< 设置最小的值 */
    self.slider.maximumValue = 100;/**< 设置最大的值 */
    self.slider.value = 50;/**< 设置初始的值 */

    /**< Tips:下面说的当前值,是slider.value,在UI中显示为:在白色的圈左边的是小于当前值,在白色的圈右边的是大于当前值 */

    self.slider.minimumValueImage = [UIImage imageNamed:@" "];/**< 设置小于当前值的图片 */
    self.slider.maximumValueImage = [UIImage imageNamed:@" "];/**< 设置大于当前值的图片 */

    self.slider.minimumTrackTintColor = [UIColor redColor];/**< 设置小于当前值的颜色 */
    self.slider.maximumTrackTintColor = [UIColor greenColor];/**< 设置大于当前值的颜色 */
    self.slider.thumbTintColor = [UIColor grayColor];/**< 设置滑动圈的颜色 */
    [self.slider addTarget:self action:@selector(sliderByUser) forControlEvents:UIControlEventValueChanged];/**< 设置监听动作,当slider的值发生变化的时候 */

UISwitch

开关(UISwitch)提供了一个简单的开/关UI元素,类似于传统的物理开关,开关的可配置选项很少,应将其用于处理布尔值。我们使用其Value Changed事件来检测开关切换,并通过属性on或实例方法isOn来获取当前值

直接上代码:

/**< 开关的bounds固定为:宽=51,高=31 */
    UISwitch *swt = [[UISwitch alloc]initWithFrame:CGRectMake(0, 60, 0, 0)];
    [self.view addSubview:swt];
    swt.onTintColor = [UIColor redColor];/**< 开关打开时,的填充颜色,默认为绿色 */
    swt.tintColor = [UIColor greenColor];/**< 开关外圈的线的颜色 */
    swt.thumbTintColor = [UIColor blueColor];/**< 开关按钮的颜色 */
    swt.on = YES;/**< 开关是否是打开的(默认为NO) */
    swt.onImage = [UIImage imageNamed:@" "];/**< 开关打开时的图片 */
    swt.offImage = [UIImage imageNamed:@" "];/**< 开关关闭时的图片 */
    [swt addTarget:self action:@selector(switchByUser:) forControlEvents:UIControlEventValueChanged];/**< 给开关监听事件,当值发生变化的时候 */

UIAlertView

UIAlertView就是所谓的”提示框”或者说”警告框”

直接上代码说 :

  • alert的属性
    /**< UIAlertView不需要加载在视图上,显示提示款的方法是show */
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"警告" message:@"我想要个女朋友" delegate:nil cancelButtonTitle:@"不给就揍你" otherButtonTitles:@"送一个林巧雯", nil];/**< 快速创建一个alert,并给其设置标题,信息,取消按钮,其他按钮的文字,设置代理方法 */

    alert.title = @"你想要什么?";/**< 设置提示框的标题 */

    alert.message = @"我想要个女朋友";/**< 设置提示框的信息 */

#warning 为什么UIAlertView挂代理不用遵守UIAlertViewDelegate协议呢?

    alert.delegate = self; /**< 设置代理 */

    NSLog(@"%ld",alert.numberOfButtons);/**< 提示款按钮的数量,这里打印2,这个属性是只读的 */

//    alert.cancelButtonIndex = 3;
    NSLog(@"%ld",alert.cancelButtonIndex);/**< 取消按钮的index,默认情况下,如果没有取消按钮index=-1,如果有取消按钮index=0 */

    NSLog(@"%ld",alert.firstOtherButtonIndex);/**< 如果没有设置其他按钮,那么firstOtherButtonIndex默认为-1,这个属性是只读的 */

    NSLog(@"%d",alert.visible);/**< 提示框是否可见,只读 */

    /**
     *  alertViewStyle 有多种类型
     *  typedef NS_ENUM(NSInteger, UIAlertViewStyle) {
     *      UIAlertViewStyleDefault = 0,      默认情况
     *      UIAlertViewStyleSecureTextInput,  会出现password的输入文本
     *      UIAlertViewStylePlainTextInput,   会出现类似留言的输入文本
     *      UIAlertViewStyleLoginAndPasswordInput   会出现login、password的输入文本
     *  };
     */
    alert.alertViewStyle = UIAlertViewStyleDefault; 


    [alert show];/**< 将alert显示到视图上 */

alert代理方法

- (void)willPresentAlertView:(UIAlertView *)alertView/**< before animation and showing view */ /**< 在动画和显示视图之前调用,第一个被调用 */
{
    NSLog(@"动画还没有出来我就有了");
}

- (void)didPresentAlertView:(UIAlertView *)alertView /**< after animation */ /**< 在动画之后调用,第二个被调用 */
{
    NSLog(@"动画结束了啊?那我出来吧!");
}

// Called when a button is clicked. The view will be automatically dismissed after this call returns
/**< 点击按钮时触发,视图将自动调用之后被返回,第三个被调用 */
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"%ld",buttonIndex);
}

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex /**< before animation and hiding view */ /**< 在隐藏试图和动画之前调用,将要消失,第四个被调用 */
{
    NSLog(@"你们都消失了。。等等我");
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex /**< after animation */ /** 在动画之后调用,已经消失,第五个被调用 */
{
    NSLog(@"终于赶上你们了");
}

// Called after edits in any of the default fields added by the style
/**< 设置是否允许第一个按钮不是取消按钮 */
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
    return YES;
}

/**< 当用户按下HOME键的时候,回调此方法,用户点击Cancel按钮的时候不会回调此方法 */
- (void)alertViewCancel:(UIAlertView *)alertView
{
    NSLog(@"我在哪里?");
}

Tips:
1. iOS8 之后就不推荐使用了,现在推荐使用UIAlertController
2. 实现了就叫遵守协议,并不是在<加了才叫遵守>,动态语言特性,关键点在于delegate设置了

UIActionSheet

可以参考banyingli的博客
UIActionSheet的属性

#pragma mark - 属性
    UIActionSheet *action = [[UIActionSheet alloc]initWithTitle:@"你叫我干嘛" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定?" otherButtonTitles:@"你说谁是小个子", nil];/**< 快速创建一个actionsheet */

    action.title = @" 我是谁 "; /**< 设置标题 */

    action.delegate = self; /**< 挂代理 */

    action.numberOfButtons; /**< 按钮的个数,只读,NSInteger */

    action.cancelButtonIndex; /**< 如果没有实现 "-actionSheetCancel:" 代理方法,我们假装这个按钮已经被点击。默认值为-1 */

    action.destructiveButtonIndex; /**< 设置destructive(红色)按钮。默认的值是-1,-1的意思是没有设置,如果只有一个按钮,这个按钮会被忽略 */

    action.firstOtherButtonIndex; /**< 如果没有设置其他按钮的title,或者没有使用initWithTitle: 方法,那么值为-1 */

    action.visible; /**< 是否可见 */

    /**
     *  actionSheetStyle     
     *  default is UIActionSheetStyleAutomatic. ignored if alert is visible
     *  typedef NS_ENUM(NSInteger, UIActionSheetStyle) {
     *      UIActionSheetStyleAutomatic        = -1,       // take appearance from toolbar style otherwise uses 'default'
     *      UIActionSheetStyleDefault          = UIBarStyleDefault,
     *      UIActionSheetStyleBlackTranslucent = UIBarStyleBlackTranslucent,
     *      UIActionSheetStyleBlackOpaque      = UIBarStyleBlackOpaque,
     *  };
     */
    action.actionSheetStyle = UIActionSheetStyleBlackTranslucent;

#pragma mark - 加载到视图上的方法

    [action showInView:self.view];/**< 将actionsheet显示到视图上 */
    /**
     - (void)showFromToolbar:(UIToolbar *)view;
     - (void)showFromTabBar:(UITabBar *)view;
     - (void)showFromBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated NS_AVAILABLE_IOS(3_2);
     - (void)showFromRect:(CGRect)rect inView:(UIView *)view animated:(BOOL)animated NS_AVAILABLE_IOS(3_2);
     - (void)showInView:(UIView *)view;
     */

UIActionSheet的代理方法

#pragma mark -- UIActionSheetDelegate --
// Called when a button is clicked. The view will be automatically dismissed after this call returns

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex/**< 点击按钮时被调用,第三个被调用 */
{
    NSLog(@"1");
}

// Called when we cancel a view (eg. the user clicks the Home button). This is not called when the user clicks the cancel button.
// If not defined in the delegate, we simulate a click in the cancel button
/**< 当用户按下home键时调用 */
- (void)actionSheetCancel:(UIActionSheet *)actionSheet
{
    NSLog(@"2");
}

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet// before animation and showing view /**< 在动画和视图显示之前调用,第一个被调用 */
{
    NSLog(@"3");
}
- (void)didPresentActionSheet:(UIActionSheet *)actionSheet// after animation /**< 在动画完成之后调用,第二个被调用 */
{
    NSLog(@"4");
}

- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex// before animation and hiding view /**< 点击按钮后,在动画和视图被隐藏之前调用,第四个被调用 */
{
    NSLog(@"5");
}
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex// after animation /**< 点击按钮后,在动画结束之后调用,第五个被调用 */
{
    NSLog(@"6");
}

UIAlertController

在iOS8之后推荐使用UIAlertController,而不是使用UIAlertView和UIActionSheet

A UIAlertController object displays an alert message to the user. This class replaces the UIActionSheet and UIAlertView classes for displaying alerts. After configuring the alert controller with the actions and style you want, present it using the presentViewController:animated:completion: method.

In addition to displaying a message to a user, you can associate actions with your alert controller to give the user a way to respond. For each action you add using the addAction: method, the alert controller configures a button with the action details. When the user taps that action, the alert controller executes the block you provided when creating the action object. Listing 1 shows how to configure an alert with a single action.


        /**< UIAlertControllerStyleAlert            alert类型
             UIAlertControllerStyleActionSheet      actionSheet类型
         */
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleAlert];

        /**<
            handler : 是一个block类型的,在block里面可以写点击按钮时的操作
         */
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}];

        /**< addAction:(UIAlertAction)添加一个alertAction,一个alertAction就是一个按钮 */
        [alert addAction:defaultAction];

        /**< UIAlertActionStyleDefault        默认的类型
             UIAlertActionStyleCancel         取消类型
             UIAlertActionStyleDestructive    警告类型(红色的字体加粗)
         */
        UIAlertAction *notDefaultAction = [UIAlertAction actionWithTitle:@"取消取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
            NSLog(@"取消,我就测试一下");
        }];
        [alert addAction:notDefaultAction];

        /**< 你在设置完AlertAction和Style之后,目前用这个方法加载
            类似于UIAlertView里的show方法
            类似于UIActionSheet里的showInView方法
            completion : block类型  block里面的内容与与弹框同时显示
         */
        [self presentViewController:alert animated:YES completion:^{
            NSLog(@"我没有点击吧?");
        }];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!
提供的源码资源涵盖了python应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值