`
jsntghf
  • 浏览: 2481625 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

自定义UIAlertView(UIPickerView)

    博客分类:
  • iOS
阅读更多

头文件:

 

#import <UIKit/UIKit.h>

@interface CustomPickerView : UIAlertView <UIPickerViewDataSource, UIPickerViewDelegate> {
    NSArray *majorNames;
    NSArray *grades;
    UIPickerView *selectPicker;
    int selectedMajor;
    int selectedGrade;
}

@end

 

实现文件:

 

#import "CustomPickerView.h"

#define componentCount 2
#define majorComponent 0
#define gradeComponent 1
#define majorComponentWidth 165
#define gradeComponentWidth 70

@implementation CustomPickerView

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        majorNames = [[NSArray alloc]initWithObjects:@"111", @"222", @"333", @"444", @"555", nil];
        grades = [[NSArray alloc]initWithObjects:@"aaa", @"bbb", @"ccc", @"ddd", @"eee", nil];
        selectPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
        selectPicker.showsSelectionIndicator = YES;
        selectPicker.delegate = self;
        selectPicker.dataSource = self;        
        selectPicker.opaque = YES;
		
        [self addSubview:selectPicker]; 
    }
    return self;
}        

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == majorComponent) {
        return [majorNames count];
    } else {
        return [grades count];
    }
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *printString;
    if (component == majorComponent) {
        printString = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, majorComponentWidth, 45)];
        printString.text = [majorNames objectAtIndex:row];
        [printString setFont:[UIFont fontWithName:@"Georgia" size:12.0f]];
    } else {
        printString = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, gradeComponentWidth, 45)];
        printString.text = [grades objectAtIndex:row];
    }
    [printString autorelease];
    printString.backgroundColor = [UIColor clearColor];
    printString.textAlignment = UITextAlignmentCenter;
    
    return printString;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 45.0;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    if (component == majorComponent) {
        return majorComponentWidth;
    } else {
        return gradeComponentWidth;
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {   
    selectedMajor = [pickerView selectedRowInComponent:majorComponent];
    selectedGrade = [pickerView selectedRowInComponent:gradeComponent];
}    

- (void)setFrame:(CGRect)rect { 
    [super setFrame:CGRectMake(0, 0, rect.size.width, 330)];         
    self.center = CGPointMake(320/2, 480/2); 
}

- (void)layoutSubviews {   
    selectPicker.frame = CGRectMake(10, 45, self.frame.size.width - 52, self.frame.size.height - 50);     
    for (UIView *view in self.subviews) {
        if ([[[view class] description] isEqualToString:@"UIThreePartButton"]) {
            view.frame = CGRectMake(view.frame.origin.x, self.bounds.size.height - view.frame.size.height - 15, view.frame.size.width, view.frame.size.height);
        }
    }
}

- (void)dealloc {
    [super dealloc];
}

@end

 

示例:

 

CustomPickerView *alert = [[CustomPickerView alloc] 
						   initWithTitle:@"Orange" 
						   message:nil 
						   delegate:self 
						   cancelButtonTitle:@"Cancel" 
						   otherButtonTitles:@"Submit", nil];
[alert show];
[alert release];

 

示例图:


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics