---完成输入后关闭键盘
在ViewController.h文件中声明,放在属性的下方
- (IBAction)textFiledDoneEditing:(id)sender;
切换到ViewController.m,添加代码
- (IBAction)textFiledDoneEditing:(id)sender
{
[sender resignFirstResponder];
}
----通过触摸背景关闭键盘, 添加一个按钮,让他填充整个屏幕,
用户触到这个按钮就触发操作,将nameFiled和numberFiled设置成第一响应状态
ViewController.h中添加
— (IBAction)backgroundClick:(id)sender;
在实现文件中添加:
- (IBAction)backgroundClick:(id)sender
{
[nameFiled resignFirstResponder];
[numberFiled resignFirstResponder];
}
---实现滑块和标签,标签显示滑块的值
在.h 文件中声明一个输出口和一个操作
IBOutlet UILabel *sliderLable;
@property (nonatomic, retain) UILable *sliderLable
- (IBAction)sliderChanged:(id)sender;
切换到.m文件,添加属性同步 实现sliderChanged:方法
@synthesize sliderLable;
- (IBAction)sliderChanged:(id)sender
{
UISlider *slider = (UISlider *)sender;
int progressAsInt = (int)(slider.value + 0.5f);
NSString *newText = [[NSString alloc] initWithFormat:@"%d",
progressAsInt];
sliderLable.text = newText;
[newText release];
}
----隐藏和显示多个控件的最简单的方法是,
使用UIView作为需要隐藏或显示的项的公共父项.
除了为两个开关创建输出口外,我们还需要为父视图创建输出口
IBOutlet UISwitch *leftSwitch;
IBOutlet UISwitch *rightswitch;
IBOutlet UIView *switchView;
@property (nonatomic, retain) UISwitch *leftSwitch;
@property (nonatomic, retain) UISwitch *rightSwitch;
@property (nonatomic, retain) UIView *switchView;
-(IBAction)switchChanged:(id)sender;
-(IBAction)toggleShowHide:(id)sender;
在实现文件中添加如下代码
@synthesize leftSwitch;
@synthesize rightSwitch;
@synthesize switchView;
-(IBAction)switchChanged:(id)sender
{
UISwitch *whichSwich = (UISwitch)sender;
BOOL setting = whichSwitch.isOn;
[leftSwitch setOn:setting animated:YES];
[rightSwitch setOn:setting animated:YES];
}
-(IBAction)toggleShowHide:(id)sender
{
UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
NSInteger segment = segmentedControl.selectedSegmentIndex;
if(segment == 0) [switchView setHidden:ON];
else [switchView seHidden:YES];
}