你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS中各種控件的使用

iOS中各種控件的使用

編輯:IOS開發綜合
#import 
@interface RootViewController : UIViewController  {
    UILabel *_label;
    UIProgressView *progress;
    UIActionSheet *sheet;
}
@end

#import "RootViewController.h"
@implementation RootViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor yellowColor];
    
    /******************************switch*****************************/
    //寬高固定
    UISwitch *sw = [[UISwitch alloc] initWithFrame:CGRectMake(10, 30, 0, 0)];
    [self.view addSubview:sw];
    sw.on = NO;//設置默認開關
    NSLog(@"%d", sw.on);
    sw.onTintColor = [UIColor purpleColor];//開的顏色
//    sw.thumbTintColor = [UIColor yellowColor];//圈的顏色
//    sw.tintColor = [UIColor greenColor];//關的顏色
//    [sw setOnImage:[UIImage imageNamed:@"main_left_nav.png"]];//設置開始的圖片
//    [sw setOffImage:[UIImage imageNamed:@"main_left_nav.png"]];
    [sw addTarget:self action:@selector(switchBackGround:) forControlEvents:UIControlEventValueChanged];
    
    
    /******************************slider*****************************/
    UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(10, 70, 250, 0)];
    [self.view addSubview:slider];
    
    [slider setMinimumValue:0];//最小值
    [slider setMaximumValue:100];//最大值
    [slider setValue:50 animated:YES];//當前值
    
    [slider setMinimumValueImage:[UIImage imageNamed:@"main_left_nav.png"]];//最小值圖片
    [slider setMaximumValueImage:[UIImage imageNamed:@"main_left_nav.png"]];//最大值圖片
    
//    [slider setMinimumTrackImage:[UIImage imageNamed:@"main_left_nav.png"] forState:UIControlStateNormal];
//    [slider setMaximumTrackImage:[UIImage imageNamed:@"main_left_nav.png"] forState:UIControlStateNormal];
    
    [slider setMaximumTrackTintColor:[UIColor redColor]];
    [slider setMinimumTrackTintColor:[UIColor greenColor]];
    
    [slider setThumbTintColor:[UIColor clearColor]];
    
    [slider setThumbImage:[UIImage imageNamed:@"main_left_nav.png"] forState:UIControlStateNormal];
    [slider setThumbImage:[UIImage imageNamed:@"main_left_nav.png"] forState:UIControlStateHighlighted];
    
    [slider addTarget:self action:@selector(sliderChange:) forControlEvents:UIControlEventValueChanged];
    
    
    /******************************segment*****************************/
    UIImage *image = [UIImage imageNamed:@"main_left_nav.png"];
    UISegmentedControl *sc = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"aaa", image, @"ccc", @"ddd", nil]];
    sc.frame = CGRectMake(10, 120, 300, 40);
    [self.view addSubview:sc];
    
    [sc setSegmentedControlStyle:UISegmentedControlStyleBar];
//    [sc setMomentary:YES];//自動彈起
    NSLog(@"%d",sc.numberOfSegments);//一共有多少個segment
//    [sc setApportionsSegmentWidthsByContent:YES];//自適應寬度
    
//    [sc insertSegmentWithImage:nil atIndex:0 animated:YES];
//    [sc insertSegmentWithTitle:nil atIndex:0 animated:YES];
    
//    [sc titleForSegmentAtIndex:0];//取title
//    [sc imageForSegmentAtIndex:0];//取圖
    
    [sc insertSegmentWithTitle:@"haha" atIndex:sc.numberOfSegments animated:YES];//添加一個
//    [sc removeSegmentAtIndex:sc.numberOfSegments animated:YES];//刪除
//    [sc setEnabled:NO forSegmentAtIndex:2];//關閉某一個segment
    [sc setBackgroundImage:[UIImage imageNamed:@"header_bg.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];//最後一個參數是橫屏還是豎屏
//    [sc setDividerImage:[UIImage imageNamed:@""] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];//間隔圖片
    
    [sc addTarget:self action:@selector(segmentChange:) forControlEvents:UIControlEventValueChanged];
    
    /******************************stepper*****************************/
    UIStepper *stepper = [[UIStepper alloc] initWithFrame:CGRectMake(30, 180, 50, 20)];
    [self.view addSubview:stepper];
    _label = [[UILabel alloc] initWithFrame:CGRectMake(30, 230, 200, 40)];
    _label.font = [UIFont boldSystemFontOfSize:20];
    [self.view addSubview:_label];
    _label.text = [NSString stringWithFormat:@"%lf", stepper.value];//默認從0到100
    [stepper addTarget:self action:@selector(stepperChange:) forControlEvents:UIControlEventValueChanged];
    
    [stepper setMinimumValue:10];
    [stepper setMaximumValue:50];
    
    [stepper setStepValue:0.01];
    
    [stepper setWraps:YES];
    [stepper setAutorepeat:NO];//長按是否自動
    [stepper setContinuous:YES];//長按是否自動加(過程是否顯示)
    
    
    
    /******************************progress*****************************/
    progress = [[UIProgressView alloc] initWithFrame:CGRectMake(10, 280, 300, 30)];
    [self.view addSubview:progress];
    [progress setProgressViewStyle:UIProgressViewStyleBar];
//    [progress setProgress:0.2 animated:YES];
    
    [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(refresh) userInfo:nil repeats:YES];
    [progress setProgressTintColor:[UIColor greenColor]];
    [progress setBackgroundColor:[UIColor yellowColor]];
    
    
    /******************************UIActivityIndicatorView*****************************/
    UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(10, 330, 300, 20)];
    [self.view addSubview:activity];
    [activity startAnimating];
    [activity stopAnimating];
    
//    [activity setHidesWhenStopped:YES];//設置停止的時候是否顯示
    [activity isAnimating];
    [activity setColor:[UIColor yellowColor]];
    
    
    /******************************TextView*****************************/
    //設置代理之後,在TextView中不能重寫return鍵,TextField中可以重寫return
    UITextView *text = [[UITextView alloc] initWithFrame:CGRectMake(10, 350, 300, 80)];
    [self.view addSubview:text];
    text.backgroundColor = [UIColor grayColor];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
    view.backgroundColor = [UIColor blueColor];
//    text.inputView = view;//取代鍵盤
    
    text.inputAccessoryView = view;//在鍵盤上附加一個view
    
    [text release];
    
    
    /******************************AlertView*****************************/
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(0, 0, 100, 100);
    [btn addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    
    /******************************ActionSheet*****************************/
//    UIActionSheet *sheet = [[UIActionSheet alloc] initWithFrame:CGRectMake(10, 200, 300, 40)];
//    [sheet showInView:self.view];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(10, 200, 300, 30);
    [button addTarget:self action:@selector(butttClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    sheet = [[UIActionSheet alloc] initWithTitle:@"請選擇" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"從照相機獲取" otherButtonTitles: @"從相冊獲取", nil];
    
}

- (void)butttClick {
    [sheet showInView:self.view];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSLog(@"%d",buttonIndex);
}

- (void)buttonClick {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"標題" message:@"你喜歡我嗎" delegate:self cancelButtonTitle:@"喜歡" otherButtonTitles:@"很喜歡",@"超級喜歡", nil];
    [alert show];
    [alert release];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSLog(@"%d",buttonIndex);
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UIView *view  in self.view.subviews) {
        if ([view isKindOfClass:[UITextView class]]) {
            UITextView *tv = (UITextView *)view;
            [tv resignFirstResponder];
        }
    }
}

- (void)refresh {
    if (progress.progress <= 1.0) {
//        progress.progress += 0.01;
        [progress setProgress:(progress.progress+0.001) animated:YES];
    }
}

- (void)stepperChange:(UIStepper *)stepper {
    _label.text = [NSString stringWithFormat:@"%lf", stepper.value];
}

- (void)segmentChange:(UISegmentedControl *)sc {
    NSLog(@"%d",sc.selectedSegmentIndex);
}

- (void)switchBackGround:(UISwitch *)sw
{
    if (sw.on) {
        self.view.backgroundColor = [UIColor whiteColor];
    } else {
        self.view.backgroundColor = [UIColor blackColor];
    }
}

- (void)sliderChange:(UISlider *)slider
{
    NSLog(@"%f",slider.value);
}

@end

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