UI常用控制項,ui控制項
//建立警告框對象
UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"警告!" message:@"您確定退出嗎?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定",@"繼續",@"查看",nil];
//顯示警告框
[alertView show];
//需要在.h裡遵守UIAlertViewDelegate協議並且實現協議裡面方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//通過buttonIndex參數,可以區分點擊的為哪個按鈕
NSLog(@"%i",buttonIndex);
}
(2)
//建立ActionSheet對象
UIActionSheet *actionSheet=[[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"刪除圖片" otherButtonTitle:nil,nil];
//添加到self.view上
[actionSheet showInView:self.view];
//也需要在.h檔案中遵守UIActionSheetDelegate協議實現協議裡面的方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"%i",buttonIndex);
}
(3)
//建立步進器對象
UIStepper *stp=[[UIStepper alloc]initWithFrame:CGRectMake(100,100,100,0)
[self.view addSubview:stp];
//設定步進器的最大值和最小值
stp.maximumValue=10;
stp.minimumValue=0;
//設定步進器的幅度 預設為一
stp.stepValue=2;
[stp addTarget:self action:@selector(stpClick:) forControlEvents:UIControlEventValueChanged];
//建立進度條對象
//進度不能設定最大和最小值
//進度條的取值範圍0~1
UIProgressView *p=[[UIProgressView alloc]initWithFrame:CGRectMake(10,150,300,10)];
p.tag=200;
[self.view addSubview:p];
//p.progress設定進度條進度
(4)
NSArray *arr=@[@"菜單",@"購物車",@"結賬",@"單價"];
UISegmentedCotrol *seg=[[UISegmentControl alloc]initWithItems:arr];
[self.view addSubview:seg];
seg.frame=CGRectMake(100,100,200,30);
//設定預設選中的位置
seg.selectedSegmentIndex=1;
//向segment控制項中添加按鈕
[seg insertSegmentWithTitle:@"數量" atIndex=1 animated:YES];
//給segment控制項添加事件
[seg addTarget:self action:@selector(segChange:) forControlEvents:UIControlEventValueChanged];
-(void)segChange:(UISegmentedControl*)sg
{
NSLog(@"%i",sg.selectedSegmentIndex);;
}
(5)
UISlider *slider=[[UISlider alloc]initWithFrame:CGRectMake(10,200,300,30)];
[self.view addSubview:slider];
//設定劃條最大值
slider.maximumValue=50;
//設定劃條最小值
slider.minimunValue=0;
//給 劃條添加滑動事件
[slider addTarget:self action:@selector(sliderChange:) forControlEvents:UIControlEventValueChanged];
//滑動結束後調用方法
slider.continuous=NO;
//設定左右兩側的滑條顏色
slider.minimumTrackTintColor=[UIColor redColor];
slider.maxmumTrackTintColor=[UIColor blueColor];
//設定滑條左右圖片
slider.maximumValueImage=[UIImage imageNamed:@"b.png"];
slider.minimumValueImage=[UIImage imageNamed:@"a.png"];
-(void)sliderChange:(UISlider *)s
{
NSLog(@"%f",s.value);
}
(6)
//建立開關控制項對象
UISwitch *sw=[[UISwitch alloc]initWithFrame:CGRectMake(100,100,0,0);
[self.view addSubview:sw];
//設定開關處於開的狀態
[sw setOn:YES];
[sw addTarget:self action:@selector(swChange:) forControlEvents:UIControlEventValueChanged];
//建立UIActivityIndicatorView對象,用於網路載入
UIActivityIndicatorView *act=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
act.frame=CGRectMake(100,150,100,100);
[self.view addSubview:act];
act.tag=100;
//開始載入
[act startAnimating];
-(void)swChange:(UISwitch*)sw
{
UIActivityIndicatorView *act=(UIActivityIndicatorView *)[self.view viewWithTag:100];
if (sw.isOn) {
[act startAnimating];
NSLog(@"on");
}
else
{
//停止載入
[act stopAnimating];
NSLog(@"off");
}
}
(7)
//建立網頁載入控制項對象
UIWebView *webView=[[UIWebView alloc]initWithFrame:CGRectMake(20,90,220,220)];
webView.backgroundColor=[UIColor redColor];
[self.view addSubview:webView];
webView.tag=200;
-(void)btnClick:(UIButton *)bt
{
//獲得文字框對象
UITextField *textField=(UITextField *)[self.view viewWithTag:100];
//獲得文字框中的網址
NSString *strURL=textField.text;
//獲得webView對象
UIWebView *webView=(UIWebView *)[self.view viewWithTag:200];
NSURL *url=[NSURL URLWithString:strURL];
//封裝網路請求類
NSURLRequest *request=[[NSURLRequest alloc]initWithURL:url];
//載入網路請求
[webView loadRequest:request];
}