Objective-c ComboBox定制组件

时间:2013-09-09 19:28:56

标签: ios objective-c uiview combobox uikit

嗨,我正在创建自己的自定义ComboBox,我在这里复制我的代码,以便任何人都可以使用它。 (我不知道还能把它放在哪里大声笑)

我还想知道你是否有更好的想法来改进它。

我得到一个小错误,当我用initWithBlurredImage初始化实例时,一切正常,但模糊的图像并没有覆盖整个屏幕,它错过了一个约5px的小帧。你明白为什么吗?

combobox enter image description here

//
//  UIComboBox.h
//  testComboBox
//
//  Created by StabiloCode on 09/09/13.
//  Copyright (c) 2013 SimplyXcode. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface UIComboBox : UIViewController <UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource> {

    UITextField *myTextField;
    UIImageView *imageView;
    UIPickerView *pickerView;
    NSMutableArray *array;
    UIToolbar *toolBar;
    CGRect oldFrame;
    UIImage *blurImage;
    UIImageView *backgroundView;
}


@property (nonatomic, retain) UITextField *myTextField;
@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) UIPickerView *pickerView;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) UIImage *blurImage;

-(void)comboBox:(UIComboBox *)comboBox dataSource:(NSMutableArray *)anArray;
-(UIImage *)createBlur;
-(id)initwithBlurredImage:(UIImage *)anImage;

@end


//
//  UIComboBox.m
//  testComboBox
//
//  Created by StabiloCode on 09/09/13.
//  Copyright (c) 2013 SimplyXcode. All rights reserved.
//

#import "UIComboBox.h"

@interface UIComboBox ()

@end

@implementation UIComboBox

@synthesize myTextField, imageView, array, pickerView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

-(id)init {
    self = [super init];
    if ( self ){
               }
    return self;
}

-(id)initwithBlurredImage:(UIImage *)anImage{
    blurImage = anImage;
      NSLog(@"blur = %@",blurImage);
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

      myTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 10, 300, 30)];
    [myTextField setBorderStyle:UITextBorderStyleRoundedRect];
    [self.view addSubview:myTextField];

    imageView = [[UIImageView alloc] initWithFrame:CGRectMake(myTextField.frame.size.width-20, myTextField.frame.size.height-17, 25, 25)];
    UIImage *arrow = [UIImage imageNamed:@"arrow.png"];  
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.clipsToBounds = YES;
    [imageView setImage:arrow];
    [self.view addSubview:imageView];

    [myTextField setDelegate:self];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)textFieldDidBeginEditing:(UITextField *)textField{

   [self showPickerView];
}


-(void)showPickerView{
    //saving old frame to fix the position after the selection
    oldFrame = self.view.frame;

    //changing the frame to have the whole screen for the comboBox
    [self.view setFrame:CGRectMake(0, 0, 320, 460)];
      if(blurImage){
        backgroundView = [[UIImageView alloc] initWithImage:blurImage];
        backgroundView.backgroundColor = [UIColor colorWithPatternImage:blurImage];
        [backgroundView setFrame:CGRectMake(0,0, 320, 460)];
        [backgroundView setContentMode:UIViewContentModeScaleToFill];
        [self.view addSubview:backgroundView];
        [self.view sendSubviewToBack:backgroundView];
    }

    if ([myTextField.text isEqual: @""]){
        myTextField.text = [array objectAtIndex:0];
    }
    [myTextField setEnabled:NO];

    //creating the pickerview
    pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 244, 320, 216)];
    [pickerView setDataSource:self];
    [pickerView setDelegate:self];
    [pickerView showsSelectionIndicator];
    [pickerView setShowsSelectionIndicator:YES];
    [self.view addSubview:pickerView];

    toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 200, 320, 244)];
    toolBar.barStyle = UIBarStyleBlackTranslucent;
    [toolBar sizeToFit];

    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done)];
    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel)];
    UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

    [toolBar setItems:@[space,cancelButton, doneButton]];

    [self.view addSubview:toolBar];
}


-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [array objectAtIndex:row];
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 1; }

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [array count];
}


-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
     myTextField.text = [array objectAtIndex:row];
}

-(void)done{
    [toolBar removeFromSuperview];
    [pickerView removeFromSuperview];
    [myTextField setEnabled:YES];
    [self.view setFrame:oldFrame];
    if(blurImage){ [backgroundView removeFromSuperview]; }
}

-(void)cancel{
    myTextField.text = @"";
    [toolBar removeFromSuperview];
    [pickerView removeFromSuperview];
    [myTextField setEnabled:YES];
     [self.view setFrame:oldFrame];
    if(blurImage){ [backgroundView removeFromSuperview]; }
}

// setting the pickerView content
-(void)comboBox:(UIComboBox *)comboBox dataSource:(NSMutableArray *)anArray{
    self.array = anArray;
}


@end

如果您想使用该代码,您可以自由地做到这一点,请告诉我它是否正常工作:) stabilocode@gmail.com

0 个答案:

没有答案