A Set of Control–iPhone App

ControlSetViewController.h:

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#define kShowSegmentIndex 0
@interface ControlSetViewController : UIViewController {
    IBOutlet UITextField *nameField;         //名字域
    IBOutlet UITextField *numberField;     //数字域
    IBOutlet UILabel *sliderLabel;              //显示slider数值的Label
    IBOutlet UISwitch *leftSwitch;            //左侧的switch
    IBOutlet UISwitch *rightSwitch;          //右侧的switch
    IBOutlet UIView *switchView;            //含有switch的视图区
}
– (IBAction)backgroundClick:(id)sender;            //点背景,隐藏输入键盘
– (IBAction)sliderChanged:(id)sender;               //slider数值改动事件
– (IBAction)textFieldDoneEditing:(id)sender;   //文本域输入完成
– (IBAction)switchChanged:(id)sender;             //左侧switch数值改动
– (IBAction)toggleShowHide:(id)sender;           //显示、隐藏switch视图区
– (IBAction)switchChangedR:(id)sender;          //右侧switch数值改动

@property (nonatomic, retain) UITextField *nameField;
@property (nonatomic, retain) UITextField *numberField;
@property (nonatomic, retain) UILabel *sliderLabel;
@property (nonatomic, retain) UISwitch *leftSwitch;
@property (nonatomic, retain) UISwitch *rightSwitch;
@property (nonatomic, retain) UIView *switchView;
@end

 

ControlSetViewController.m:

#import "ControlSetViewController.h"

@implementation ControlSetViewController

@synthesize nameField;
@synthesize numberField;
@synthesize sliderLabel;
@synthesize leftSwitch;
@synthesize rightSwitch;
@synthesize switchView;

– (IBAction)switchChanged:(id)sender {                                                    //左侧switch值改变
    UISwitch *whichSwitch = (UISwitch *)sender;                                       //把弱类型sender转成强类型Switch型
    if(whichSwitch.isOn == YES) [rightSwitch setOn:NO animated:YES];    //左ON右OFF
    else [rightSwitch setOn:YES animated:YES];                                         //左OFF右ON
}

– (IBAction)toggleShowHide:(id)sender {
    UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;  //sender转成SegmentControl型
    NSInteger segment = segmentedControl.selectedSegmentIndex;                 //显示Show时SegmentIndex=0,Hide时为1
    if (segment == kShowSegmentIndex) [switchView setHidden:NO];              //Index=0, 不隐藏switchView
    else [switchView setHidden:YES];                                                                //Index=1, 隐藏switchView
}

– (IBAction)sliderChanged:(id)sender {
    UISlider *slider = (UISlider *)sender;                                                                    //sender转成Slider型
    int progressAsInt = (int)(slider.value + 0.5f);                                                      //四舍五入
    NSString *newText = [[NSString alloc] initWithFormat: @"%d", progressAsInt];  //创建字符串
    sliderLabel.text = newText;                                                                               //show slider value of integer
    [newText release];
}

– (IBAction)backgroundClick:(id)sender {
    [nameField resignFirstResponder];                      //unfocus nameField
    [numberField resignFirstResponder];                  //unfocus numberField
}

– (IBAction)textFieldDoneEditing:(id)sender {
    [sender resignFirstResponder];                          //clear the previous field
}

– (IBAction)switchChangedR:(id)sender {
    UISwitch *whichSwitch = (UISwitch *)sender;
    if(whichSwitch.isOn == YES) [leftSwitch setOn:NO animated:YES];    //右ON左OFF
    else [leftSwitch setOn:YES animated:YES];                                         //右OFF左ON
}

@end

Controls

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s