#pragma mark UIAlertView 的使用方法
-(void)testAlertView
{
//可以输入用户名和密码的输入框
UIAlertView *alert=[[UIAlertViewalloc]init];
alert.title=@"输入用户名和密码";
//alertViewStyle这个属性比较重要
/*
UIAlertViewStyleDefault = 0, 表示没有格式
UIAlertViewStyleSecureTextInput, 安全输入格式,比如密码加密
UIAlertViewStylePlainTextInput, 简单格式
UIAlertViewStyleLoginAndPasswordInput 用户名和密码输入
*/
alert.alertViewStyle=UIAlertViewStyleLoginAndPasswordInput;
[alert addButtonWithTitle:@"取消"];
[alert addButtonWithTitle:@"完成"];
[alert show];
//要实现事件,必须遵守UIAlertViewDelegete代理
alert.delegate=self;
}
//alertView的事件处理
- (void)alertView:(UIAlertView *)alertViewclickedButtonAtIndex:(NSInteger)buttonIndex;
{
//textFieldAtIndex:0 根据索引下标获取textfield里面的值
UITextField *nametext=[alertView textFieldAtIndex:0];
UITextField *password=[alertView textFieldAtIndex:1];
NSLog(@"name=%@password=%@",nametext.text,password.text);
}
//附上效果图
#pragma mark 动作列表的创建
-(void)testActionSheet
{
//实现一个分享的实力,点击分享,弹出动作列表
UIButton *shareButton=[UIButtonsystemButtonWithFrame:CGRectMake(100, 100, 150, 40) title:@"分享"target:selfaction:@selector(dealshareClick)];
shareButton.backgroundColor=[UIColorgreenColor];
[self.view addSubview:shareButton];
}
#pragma mark 处理点击按钮
-(void)dealshareClick
{
UIActionSheet *actionsheet=[[UIActionSheetalloc]initWithTitle:@"分享" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"新浪微博",@"微信",@"邮件",@"短信", nil];
[actionsheet showInView:self.view];
}
//处理actionsheet的事件
// 必须遵守actionsheetdelegete协议
//处理这个事件 也必须实现UIActionSheet的代理
-(void)actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
//按照下表来处理事件
if (buttonIndex==0) {
}
elseif(buttonIndex==1)
{
}
NSLog(@"%d",buttonIndex);
}
//UIActionSheet的效果图
#pragma mark 创建文本视图
//这个文本视图类似于textfield但是不同的是textfield是单行文本,这是多行文本输入
-(void)testTextView
{
UITextView *textView=[[UITextViewalloc]initWithFrame:CGRectMake(10, 30, 300, 400)];
//点击return并不会回收键盘 是换行,要想回收,自己添加按钮
//1.设置文本内容
textView.text=@"尽人事,听天命,非也非也,我命由我不由天.尽人事,听天命,非也非也,我命由我不由天.尽人事,听天命,非也非也,我命由我不由天.尽人事";
//历史的尘埃(在历史的天空下,所有人只不过是一粒尘埃)
//2这里设置背景颜色
textView.backgroundColor=[UIColorgreenColor];
textView.delegate=self;
//回收textview 的键盘,
// 1在导航栏上添加完成回收
// 2.在虚拟键盘的附件区域添加一个完成按钮
// 3.添加一个根虚拟键盘联动的按钮
textView.tag=200;
[self.view addSubview:textView];
//z在键盘添加附件区域
UIToolbar *toolbar=[[UIToolbaralloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
// 完成按钮
UIBarButtonItem*finishItem=[[UIBarButtonItem alloc]initWithTitle:@"完成"style:UIBarButtonItemStylePlain target:self action:@selector(dealtextBtnClik:)];
finishItem.tag=100;
//清空按钮
UIBarButtonItem*clearItem=[[UIBarButtonItem alloc]initWithTitle:@"清空"style:UIBarButtonItemStylePlain target:self action:@selector(dealtextBtnClik:)];
clearItem.tag=101;
toolbar.items=@[finishItem,clearItem];
//把toolbar 加入到键盘的附件区域
//[self.view addSubview: toolbar];
textView.inputAccessoryView=toolbar;
}
#pragma mark这里在键盘附件区域添加事件
-(void)dealtextBtnClik:(UIBarButtonItem*)button
{
UITextView *textView=(UITextView*)[self.view viewWithTag:200];
//等于100的时候,完成
if (button.tag==100) {
[textView resignFirstResponder];
}
elseif(button.tag==101)
{
textView.text=@"";
}
}
#pragma mark 进度条
-(void)testProgressView
{
UIProgressView *progress=[[UIProgressViewalloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
progress.frame=CGRectMake(100, 260, 200,10);
[self.view addSubview:progress];
//属性的设置
progress.progress=0;
//模拟下载完成
[NSTimer scheduledTimerWithTimeInterval:0.1target:self selector:@selector(downloadprogress:)userInfo:progress repeats:YES];
}
-(void)downloadprogress:(NSTimer*)timer
{
UIProgressView *progress=(UIProgressView*)timer.userInfo;
progress.progress+=0.01;
if (progress.progress>=1.00 ) {
[timer invalidate];
UIAlertView *alert=[[UIAlertViewalloc]init];
alert.message=@"下载好啦";
[alert addButtonWithTitle:@"好的"];
[alert show];
}
}
//效果图
//计数器控件,一般用在购物app上面,商品的加减
-(void)testStepper
{
//宽度和高度都是固定的
UIStepper *step=[[UIStepperalloc]initWithFrame:CGRectMake(150, 200, 100, 20)];
//常用的属性的设置
//在购物界面上添加一个输入商品的计数器
step.minimumValue=1;
step.maximumValue=100;
step.stepValue=1;
[step addTarget:self action:@selector(dealstep:)forControlEvents:UIControlEventValueChanged];
textfield=[[UITextFieldalloc]initWithFrame:CGRectMake(10, 200, 100, 30)];
textfield.borderStyle=UITextBorderStyleRoundedRect;
textfield.text=@"1";
textfield.tag=100;
textfield.delegate=self;
[self.view addSubview:textfield];
[self.view addSubview:step];
}
-(void)dealstep:(UIStepper*)step
{
//step.value
UITextField *text=(UITextField*)[self.view viewWithTag:100];
text.text=[NSString stringWithFormat:@"%i",(int)step.value];
}
//键盘回收,一般所有的textfield控件都需要使用的方法,使用这个方法必须遵守UITextfieldDelegate这个协议
-(BOOL)textFieldShouldReturn:(UITextField*)textField
{
[textfield resignFirstResponder];
returnYES;
}
//效果图
#pragma mark 分段选择控件
-(void)testSegmentedControl
{
NSArray *array=@[@"脑残",@"菊花",@"武士",@"刀",@"小桥"];
UISegmentedControl *sc=[[UISegmentedControlalloc]initWithItems:array];
sc.frame=CGRectMake(10, 100,300, 30);
//设置当前的选择项
sc.selectedSegmentIndex=1;
[self.view addSubview:sc];
[sc insertSegmentWithTitle:@"瘦马" atIndex:2 animated:YES];
[sc addTarget:self action:@selector(dealsegmengt:)forControlEvents:UIControlEventValueChanged];
}
#pragma mark 处理分段控件的事件
-(void)dealsegmengt:(UISegmentedControl*)segmented
{
NSLog(@"segmented=%d",segmented.selectedSegmentIndex);
}
#pragma mark 滑块控件 一般用在音乐播放器进度控制
-(void)testSlider
{
//创建划块控件
UISlider *slider=[[UISlideralloc]initWithFrame:CGRectMake(100, 100, 200, 20)];
//修改长度,但是高度是固定不变的
NSLog(@"sliderwidth=%f and hight=%f",slider.frame.size.width,slider.frame.size.height);
slider.minimumValue=0;
slider.maximumValue=200;
slider.value=100;
//设置背景颜色
slider.tintColor=[UIColor redColor];
[slider addTarget:self action:@selector(dealslider:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview: slider];
}
#pragma mark 滑块控件的事件
-(void)dealslider:(UISlider*)slider
{
NSLog(@"slider.value=%f",slider.value);
}
#pragma mark 活动提示控件 菊花控件
-(void)activityIndicatorView
{
//UI ActivityIndicatorView 基本使用
UIActivityIndicatorView*activity=[[UIActivityIndicatorViewalloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activity.center=CGPointMake(160, 240);
[self.view addSubview:activity];
[activity startAnimating];
//模拟数据下载完成的操作
[ NSTimer scheduledTimerWithTimeInterval:3target:self selector:@selector(stopActivityIndicatorView:)userInfo:activity repeats:NO];
}
// 活动提示控件的处理
-(void)stopActivityIndicatorView:(NSTimer*)timer
{
UIActivityIndicatorView*aiv=timer.userInfo;
[aiv stopAnimating];
UIAlertView *alert=[[UIAlertViewalloc]init];
alert.message=@"加载完毕";
[alert addButtonWithTitle:@"确定"];
[alert show];
}
#pragma mark 开关控件的创建
-(void)testSwitch
{
UISwitch *myswicth=[[UISwitchalloc]initWithFrame:CGRectMake(100, 100, 100, 30)];
NSLog(@"%f %f",myswicth.frame.size.width,myswicth.frame.size.height);
[myswicth setOn:YES animated:YES];
//开关控件的简单设置
//设置状态
myswicth.on=NO;
myswicth.onTintColor=[UIColor redColor];
myswicth.tintColor=[UIColor greenColor];
myswicth.thumbTintColor=[UIColorblackColor];
//事件处理
[myswicth addTarget:self action:@selector(dealswitch:)forControlEvents:UIControlEventValueChanged];
[self.view addSubview: myswicth];
}
//switch的事件处理
-(void)dealswitch:(UISwitch*)myswitch
{
//关掉为0 开启为1
NSLog(@"%d",myswitch.on);
if (myswitch.on) {
// 处理开启的任务
}
}