UI_06 UIControl及其子类

本文详细介绍了iOS开发中常用的UI控件,包括UISegmentedControl、UISlider和UIAlertController的使用方法及示例代码。同时涵盖了如何利用这些控件进行交互设计,并探讨了UIControl的基础作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

⼀、UISegmentedControl的使⽤

     分段控件。每个segment都能被点击,相当于集成了若干个button。 通常我们会点击不同的segment来切换不同的view。

1、常⽤⽅法和属性

  • initWithItems:                                    //UISegmentedControl独有的初始化⽅法,⽤来创建多个分段

  • setTitle: forSegmentAtIndex:            //为指定下标的分段设置title

  • selectedSegmentAtIndex                 //(property)被选中的segment

  • tintColor                                            //(property)segmentedControl条的颜⾊(含每个segment的颜⾊)

  • addTarget: action: forControlEvents://给UISegmentedControl添加事件,

  • controlEvent                                      //为UIControlEventValueChanged。

2、使用

RootViewController.m

#import "RootViewController.h"
#import
"MessageViewController.h"
#import
"PhoneViewController.h"

@interface RootViewController ()

@property (nonatomic, retain) UISegmentedControl *segmentedControl;

@property (nonatomic, retain) MessageViewController * messageVC;

@property (nonatomic, retain) PhoneViewController * phoneVC;

@end

@implementation RootViewController
- (
void)dealloc
{
    [
_segmentedControl release];
    [
_messageVC release];
    [
_phoneVC release];
    [
super dealloc];
}
- (
void)viewDidLoad {
    [
super viewDidLoad];
   
self.view.backgroundColor = [UIColor whiteColor];
   
#pragma mark -- 添加子视图控制器 --
    [self createChildViewControllers];
   
#pragma mark -- UISegmentedControl --
    [self createSegmentedControl];


}


- (
void)createChildViewControllers
{
   
MessageViewController * messageVC = [[MessageViewController alloc] init];
   
PhoneViewController * phoneVC = [[PhoneViewController alloc] init];
   
   
self.phoneVC = phoneVC;
   
self.messageVC = messageVC;
   
    [
self addChildViewController:messageVC];
    [
self addChildViewController:phoneVC];
   
    [messageVC
release];
    [phoneVC
release];
   
    [
self.view addSubview:messageVC.view];
   
}

- (
void)createSegmentedControl
{
   
UIImage *image

    = [UIImage imageNamed:@"bank_icon_ccb.png"];

    

    self.segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"消息",@"电话",image]];
   
self.segmentedControl.frame = CGRectMake(100, 100, 150, 30);
   
self.segmentedControl.selectedSegmentIndex = 0;
   
//改变颜色
   
_segmentedControl.tintColor = [UIColor redColor];
    [
self.view addSubview:self.segmentedControl];
    [
self.segmentedControl release];
   
//添加事件

    [_segmentedControl addTarget:self action:@selector(exchangeView:) forControlEvents:UIControlEventValueChanged];

    

    [self.segmentedControl release];
}

- (
void)exchangeView:(UISegmentedControl *)SC
{
   
NSLog(@"%ld", (long)SC.selectedSegmentIndex);
   
switch (SC.selectedSegmentIndex) {
       
case 0: {
           
if (!self.messageVC.view.superview) {
                [
_phoneVC.view removeFromSuperview];
                [
self.view addSubview:_messageVC.view];
            }
           
break;
        }
       
case 1:
           
if (!_phoneVC.view.superview) {
                [
_messageVC.view removeFromSuperview];
                [
self.view addSubview:_phoneVC.view];
            }
           
break;
       
default:
           
break;
    }
    [
self.view bringSubviewToFront:SC];
}

- (
void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

}


@end



⼆、UISlider的使⽤

     滑块控件。 通常⽤于控制视频播放进度,控制⾳量等。 它也是继承于UIControl,滑块提供了⼀系列连续的值,滑块停在不 同的位置,获取到滑块上的值也不同。

1、UISlider常⽤属性

  • minimumValue                                   //设置滑块的最⼩值

  • maximumValue                                  //设置滑块的最⼤值

  • value                                                  //设置滑块的当前值

  • minimumTrackTinkColor                    //定义划过区域的颜⾊

  • addTarget: action: forControlEvents: //给UISlider添加事件,

  • controlEvent                                       //为UIControlEventValueChanged。

2、UISlider使用

     下面是UISlider与UIImageView的综合使用。用UISlider控制动图的播放速度(关于UIImageView的介绍请看前一篇)

RootViewController.m

#import "RootViewController.h"

@interface RootViewController ()
@property (nonatomic, retain) UIImageView *imgView;
@property (nonatomic, retain) UISlider *slider;

@end

@implementation RootViewController

- (
void)viewDidLoad {
    [
super viewDidLoad];
   
self.view.backgroundColor = [UIColor whiteColor];
   
#pragma mark -- 创建UIImageView --
    [self createImageView];
#pragma mark -- 创建UISlider --
    [self createSlider];
}

- (
void)createSlider
{
   
self.slider = [[UISlider alloc] initWithFrame:CGRectMake(50, 400, 267, 30)];
    [
self.view addSubview:_slider];
   
    [
_slider release];
   
   
_slider.minimumValue = 1;
   
_slider.maximumValue = 5; //最大值必须大于最小值
   
_slider.minimumTrackTintColor = [UIColor redColor];
   
_slider.maximumTrackTintColor = [UIColor blueColor];
   
//设置滑块
    [
_slider setThumbImage:[UIImage imageNamed:@"greenbird.png"] forState:UIControlStateNormal];
    [
_slider setThumbImage:[UIImage imageNamed:@"bluebird.png"] forState:UIControlStateHighlighted];
    [
_slider addTarget:self action:@selector(changeSpeed:) forControlEvents:UIControlEventValueChanged];
   
}

- (
void)changeSpeed:(UISlider *)slider
{
   
if (slider.value==slider.minimumValue) {
        [
self.imgView stopAnimating];
    }
else if(slider.value != slider.maximumValue) {
       
self.imgView.animationDuration = slider.maximumValue-slider.value;
        [
self.imgView startAnimating];
    }
else {
       
self.imgView.animationDuration = 0.0001;
        [
self.imgView startAnimating];
    }
}

- (
void)createImageView
{
   
self.imgView = [[UIImageView alloc] init];
   
_imgView.frame = CGRectMake(50, 100, 250, 250);
    [
self.view addSubview:_imgView];
    [
_imgView release];
   
   
NSMutableArray * imagesArray = [NSMutableArray array];
   
for (int i = 1; i<28; i++) {
        [imagesArray
addObject:[UIImage imageNamed:[NSString stringWithFormat:@"Z6CLNZ]T)N8I}L9A))7TRD9%d(被拖移).tiff", i]]];
    }
   
_imgView.animationImages = imagesArray;
   
_imgView.image = _imgView.animationImages[0];
}


- (
void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

}

@end



三、UIAlertController

RootViewController.m

#import "RootViewController.h"

@interface RootViewController ()
@property (nonatomic, retain) UITextField * textField;
@property (nonatomic, retain) UIButton *button;
@end

@implementation RootViewController

- (
void)dealloc
{
    [
_textField release];
    [
_button release];
    [
super dealloc];
}

- (
void)viewDidLoad {
    [
super viewDidLoad];
   
   
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 150, 30)];
   
   
_textField.borderStyle = UITextBorderStyleRoundedRect;
   
_textField.placeholder = @"请输入密码";
    [
self.view addSubview:_textField];
    [
_textField release];
   
   
self.button = [UIButton buttonWithType:UIButtonTypeCustom];
   
_button.frame = CGRectMake(150, 300, 60, 30);
    [
_button setTitle:@"登陆" forState:UIControlStateNormal];
    [
_button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [
_button addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
    [
self.view addSubview:_button];
   
   
   
NSLog(@"%lu", self.retainCount);
}

- (
void)login:(UIButton *)button
{
   
NSString * infoStr;
   
if ([_textField.text isEqualToString:@"123"]) {

        infoStr = @"登录成功";

#pragma mark -- UIAlertView --     //UIAlertView

        UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"登录成功" message:infoStr delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alertView
show];

        [alertView release];

    } else {

        infoStr = @"登录失败";

#pragma mark -- UIAlertController --      //UIAlertController

        UIAlertController * alertC = [UIAlertController alertControllerWithTitle:@"登录信息" message:infoStr preferredStyle:UIAlertControllerStyleAlert];

        //避免循环引用

        //block里用到self,将self赋值给__block修饰的变量再使用

        __block RootViewController *rootVC = self;
       
//block里用到实例变量(非属性),用如下方式
       
//    __block UITextField *textField = _textField;
       
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            [rootVC.
textField resignFirstResponder];
        }];
       

        UIAlertAction *reloginAction = [UIAlertAction actionWithTitle:@"重新登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

            rootVC.textField.text = @"";
        }];

       

        [alertC addAction:cancelAction];
        [alertC
addAction:reloginAction];
       
        [
self presentViewController:alertC animated:YES completion:nil];
    }

}


- (
void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
   
NSLog(@"%@", _textField.text);
}

- (
void)alertViewCancel:(UIAlertView *)alertView
{
   
NSLog(@"canceled");
}

- (
void)didReceiveMemoryWarning {
    [
super didReceiveMemoryWarning];

}


@end



四、UIControl的作⽤

     UIControl是所有控制控件(⽐如UIButton、UISlider、 UISegmentedControl等)的基类。 只要跟控制有关的控件都是继承于该类。

1、UIControl的核⼼功能

  • 为控制控件通过addTarget: action: forControlEvents: ⽅法来添加事件。

  • 通过removeTarget: action: forControlEvents: 来移除事件。

2、UIControl的继承关系



iOS UIKit_Framework:

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIKit_Framework/index.html#other


转载于:https://my.oschina.net/zooyf/blog/495666

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值