你好,歡迎來到IOS教程網

 Ios教程網 >> IOS訊息 >> 關於IOS >> iOS—各種控件的創建方法和部分調用方法

iOS—各種控件的創建方法和部分調用方法

編輯:關於IOS

1.Image View

//找到圖片
UIImage *pImage = [UIImage imageNamed:@"sea.png"];
//根據內容圖片創建ImageView
self.pImageView = [[UIImageView alloc]initWithImage:pImage];
//將ImageView放到父視圖中
[self.view insertSubview:self.pImageView atIndex:0];
2.Label
//創建一個label
UILabel *pLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 120, 50)];
//添加內容
pLabel.text = @"HelloWorld/nSecondLine";
//設置字體和大小
pLabel.font = [UIFont fontWithName:@"Verdana" size:18];
//字體對齊方式
pLabel.textAlignment = NSTextAlignmentCenter;
//字體顏色
pLabel.textColor = [UIColor redColor];
//顯示行數
pLabel.numberOfLines = 2;
//陰影顏色
pLabel.shadowColor = [UIColor blackColor];
//陰影尺寸
pLabel.shadowOffset = CGSizeMake(2.0,1.0);
//設置label的背景色為透明色
pLabel.backgroundColor = [UIColor clearColor];
[self.view addSubview:pLabel];
[pLabel release];
3.TextField
//創建TextField
UITextField *pTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 116, 200, 31)];
//設置邊框樣式
pTextField.borderStyle = UITextBorderStyleRoundedRect;
//設置字體
pTextField.font = [UIFont systemFontOfSize:18.0];
//根據寬度改變字體
pTextField.adjustsFontSizeToFitWidth = YES;
//最小字體
pTextField.minimumFontSize = 2.0;
//清除按鈕的樣式
pTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
//彈出的鍵盤的樣式
pTextField.keyboardType = UIKeyboardTypeDefault;
//設置使用自動更正功能
pTextField.autocorrectionType = UITextAutocorrectionTypeNo;
//設置鍵盤自動大小寫的屬性
pTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
//設置返回按鈕類型
pTextField.returnKeyType = UIReturnKeyDone;
//設置是否支持密碼文本顯示
pTextField.secureTextEntry = YES;
//設置委托
pTextField.delegate = self;
[self.view addSubview:pTextField];
[pTextField release];
編寫方法時需用到協議UITextFieldDelegate如下:
//彈回鍵盤方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
//放當前的textField放棄第一響應者
[textField resignFirstResponder];
return YES;
}
//觸摸背景彈回鍵盤如下:
- (IBAction)backgroundTap:(id)sender{
[_numberField resignFirstResponder];
[_codeField resignFirstResponder];
}
//一個簡單的彈回鍵盤方法(觸摸背景)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.textField endEditing:YES];
}
//限制輸入字符串長度
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
int MAX_LENGTH = 10;
NSMutableString *pNewString = [NSMutableString stringWithString:textField.text];
//完成range內字符串的替換
[pNewString replaceCharactersInRange:range withString:string];
//根據兩個的長度判斷,返回YES or NO.
return ([pNewString length] <= MAX_LENGTH);
}
4.Button
//創建一個按鈕
UIButton *pBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//設置區域
[pBtn setFrame:CGRectMake(10, 70, 100, 40)];
//設置名字
[pBtn setTitle:@"Normal" forState:UIControlStateNormal];
[pBtn setTitle:@"HighLight" forState:UIControlStateHighlighted];
//允許顯示高亮
pBtn.showsTouchWhenHighlighted = YES;
//按鈕調用方法
[pBtn addTarget:self action:@selector(buttonDown:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:pBtn];
// 按鈕的響應方法
-(void)buttonDown:(id)sender
{ }
5.CheckboxButton (選中框)
//創建一個自定義按鈕
UIButton *pCheckboxButton = [UIButton buttonWithType:UIButtonTypeCustom];
//設置區域
CGRect checkboxRect = CGRectMake(10,155,36,36);
[pCheckboxButton setFrame:checkboxRect];
//分別為兩種不同狀態設置對應圖片
[pCheckboxButton setImage:[UIImage imageNamed:@"checkbox_off.png"] forState:UIControlStateNormal];
[pCheckboxButton setImage:[UIImage imageNamed:@"checkbox_on.png"] forState:UIControlStateSelected];
//添加關聯動作
[pCheckboxButton addTarget:self action:@selector(checkboxClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:pCheckboxButton];
//關聯方法
- (void)checkboxClick:(UIButton *)btn
{
btn.selected = !btn.selected;
}
6.Slideer――滑塊
//創建UISlider對象
UISlider *pSlider = [[UISlider alloc]initWithFrame:CGRectMake(10, 195, 300, 20)];
//設置最小值
pSlider.minimumValue = 0;
//最大值
pSlider.maximumValue = 100;
//是否可以持續滑動
pSlider.continuous = YES;
//設置初始值
pSlider.value = 50;
//透明度
self.pImageView.alpha = pSlider.value/100;
//最小值處的圖片
pSlider.minimumValueImage = [UIImage imageNamed:@"apple_min"];
//最大值處的圖片
pSlider.maximumValueImage = [UIImage imageNamed:@"apple_max.png"];
//設置當前的圖片
[pSlider setThumbImage:[UIImage imageNamed:@"apple_thumb.png"] forState:UIControlStateNormal];
//添加對應動作,注意:CotrolEvents:valueChanged
[pSlider addTarget:self action:@selector(sliderValueChange:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:pSlider];
//創建顯示當前值的label
self.pShowValueLabel = [[UILabel alloc]initWithFrame:CGRectMake(180, 150, 86, 25)];
//設置背景色
self.pShowValueLabel.backgroundColor = [UIColor lightGrayColor];
//設置當前文本內容
self.pShowValueLabel.text = [NSString stringWithFormat:@"亮度:%%%d",(int)pSlider.value];
[self.view addSubview:self.pShowValueLabel];
//關聯方法
- (void)sliderValueChange:(UISlider *)slider
{
//顯示當前亮度
self.pShowValueLabel.text = [NSString stringWithFormat:@"亮度:%%%d",(int)slider.value];
//透明度
self.pImageView.alpha = slider.value/100;
}
7.Switch――開關
UISwitch *pSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(220, 10, 10, 50)];
//給開關添加關聯動作
[pSwitch addTarget:self action:@selector(switchMethod:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:pSwitch];
//關聯方法
- (void)switchMethod:(id)sender
{
//找到開關
UISwitch *pSwitch = (UISwitch *)sender;
if (pSwitch.isOn)//根據開關的狀態設置背景圖hidden
{
[self.pImageView setHidden:NO];
}
else
{
[self.pImageView setHidden:YES];
}
}
8.SegmentControl――分段控制
//創建數組
NSArray *pArr = [NSArray arrayWithObjects:@"1",@"2", nil];
//根據數組對象初始化SegmentControl對象
UISegmentedControl *pSegment = [[UISegmentedControl alloc]initWithItems:pArr];
[pSegment setFrame:CGRectMake(10, 240, 300, 40)];
//設置該分段控件的Style
[pSegment setSegmentedControlStyle:UISegmentedControlStyleBordered];
//設置分段控件選擇的段數
[pSegment setSelectedSegmentIndex:0];
//添加關聯動作
[pSegment addTarget:self action:@selector(segmentMethod:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:pSegment];
9.Progress――進度條
//創建進度條並初始化
self.pProgress = [[UIProgressView alloc]initWithFrame:CGRectMake(10, 360, 300, 30)];
//設置Style
self.pProgress.progressViewStyle = UIProgressViewStyleBar;
//分別設置進度條完成未完成的顏色
self.pProgress.trackTintColor = [UIColor redColor];
self.pProgress.progressTintColor = [UIColor greenColor];
[self.view addSubview:self.pProgress];
//創建用來顯示進度的label
self.pProgressValueLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 370, 110, 30)];
self.pProgressValueLabel.backgroundColor = [UIColor clearColor];
self.pProgressValueLabel.text = [NSString stringWithFormat:@"加載:%%%.1f",self.progressValue*100];
[self.view addSubview:self.pProgressValueLabel];
//通過計時器完成循環調用
pTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(progressMethod:) userInfo:nil repeats:YES];
//關聯的方法
- (void)progressMethod:(id)sender
{
self.progressValue += 0.1;
self.pProgress.progress = self.progressValue;
self.pProgressValueLabel.text = [NSString stringWithFormat:@"加載:%%%.1f",self.progressValue*100];

//[self.pActivity startAnimating];
if (self.progressValue >= 1)
{
self.progressValue = 0;
self.pProgressValueLabel.text = @"加載完成";
//[self.pActivity stopAnimating];
//self.pActivity.hidden = NO;
//停止計時器
[pTimer invalidate];
}
}
10.ActivityIndicatorView
//創建初始化ActivityIndicatorView(Style)
self.pActivity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[self.pActivity setFrame:CGRectMake(10, 280, 45, 45)];
//設置是否隱藏
self.pActivity.hidden = NO;
[self.view addSubview:self.pActivity];
//創建按鈕,關聯對應方法
UIButton *pBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[pBtn setFrame:CGRectMake(10, 330, 45,30 )];
[pBtn setTitle:@"轉" forState:UIControlStateNormal];
[pBtn addTarget:self action:@selector(doRotate:) forControlEvents:UIControlEventTouchUpInside];
pBtn.tag = 110;
[self.view addSubview:pBtn];
//關聯的方法
- (void)doRotate:(id)sender
{
UIButton *pBtn = (UIButton *)sender;
[self.pActivity isAnimating]?[self.pActivity stopAnimating]:[self.pActivity startAnimating];
//根據Activity是否運行來設置按鈕的title
[pBtn setTitle:(self.pActivity.isAnimating?@"停":@"轉")forState:UIControlStateNormal];
self.pActivity.hidden = NO;
}

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved