IOS-常见控件用法例子(一)

来源:互联网 发布:逗斗车 知乎 编辑:程序博客网 时间:2024/06/09 14:42

这部分主要展示了这些控件的用法:UISwitch, UISlider, UIStepper, UIToolbar, UISegmentedControl

因为偷懒,全部的控件共用一个事件处理函数。

h文件

////  ViewController.h//  UICtrl////  Created by God Lin on 14/11/30.//  Copyright (c) 2014年 arbboter. All rights reserved.//#import <UIKit/UIKit.h>@interface ViewController : UIViewController{    IBOutlet UITextField* _textInfo;    IBOutlet UISwitch* _switchText;    IBOutlet UISlider* _sliderText;    IBOutlet UIStepper* _stepperText;    IBOutlet UIToolbar* _toolbarView;    IBOutlet UISegmentedControl* _segmentedBKColor;}@property (nonatomic, retain) UITextField* _textInfo;@property (nonatomic, retain) UISwitch* _switchText;@property (nonatomic, retain) UISlider* _sliderText;@property (nonatomic, retain) UIStepper* _stepperText;@property (nonatomic, retain) UIToolbar* _toolbarView;@property (nonatomic, retain) UISegmentedControl* _segmentedBKColor;-(IBAction)onCtrl:(id)sender;@end


m文件

////  ViewController.m//  UICtrl////  Created by God Lin on 14/11/30.//  Copyright (c) 2014年 arbboter. All rights reserved.//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController@synthesize _textInfo;@synthesize _switchText;@synthesize _sliderText;@synthesize _stepperText;@synthesize _toolbarView;@synthesize _segmentedBKColor;-(IBAction)onCtrl:(id)sender{    if (sender == _switchText)    {        if(_switchText.isOn)        {            _textInfo.text = @"Switch is On";        }        else        {            _textInfo.text = @"Switch is Off";        }    }    else if(sender == _sliderText)    {        NSString* strSlider = [[NSString alloc] initWithFormat:@"%.2f", _sliderText.value];        _textInfo.text = strSlider;        [strSlider release];    }    else if(sender == _stepperText)    {        NSString* strText = [[NSString alloc] initWithFormat:@"%.2f", [_stepperText value]];        _textInfo.text = strText;        [strText release];    }    else if(sender == _segmentedBKColor)    {        UIColor* bkColor[3] = {[UIColor whiteColor], [UIColor greenColor], [UIColor blueColor]};        [self.view setBackgroundColor:bkColor[[_segmentedBKColor selectedSegmentIndex]]];    }}- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}-(void)dealloc{    [_textInfo release];    [_switchText release];    [_sliderText release];    [_stepperText release];    [_toolbarView release];    [_segmentedBKColor release];    [super dealloc];}@end


0 0