`
jsntghf
  • 浏览: 2511578 次
  • 性别: 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];

 

示例图:


分享到:
评论

相关推荐

    自定义警报框

    此外,对于复杂的交互,如多行输入、选择器等,我们可以添加子视图(如UITextField或UIPickerView)到自定义警报框中。同时,自定义警报框的关闭可以通过点击背景、滑动或按下特定按钮来触发,这需要监听相应的触摸...

    IOS应用源码之AlertPicker2.zip

    在这个源码中,我们可以深入理解如何在iOS应用中自定义警告视图,并添加自定义的UIPickerView。 首先,我们要明白UIAlertController是iOS 8及更高版本中用来替代UIAlertView和UIActionSheet的新API。它提供了更大的...

    UIDatePicker与UIPickerView

    UIDatePicker主要用于选取日期和时间,而UIPickerView则更为通用,允许开发者自定义显示的内容。 **UIDatePicker详解** 1. **创建与配置** - 在Xcode中,可以通过Interface Builder或代码创建一个UIDatePicker。...

    iOS自定义UIDatepicker日期选择器视图分享

    然后,我们使用`UIAlertView`显示一个提示框,展示所选日期,用户点击“确定”按钮后,`UIAlertView`会自动关闭。同时,这个方法还会清理`_picker`,防止内存泄漏。 `DatePicker`类中,我们定义了`...

    iOS App中UIPickerView选择栏控件的使用实例解析

    总的来说,UIPickerView是iOS应用中实现选择功能的重要工具,通过自定义数据源和委托,我们可以创建出各种定制化的选择界面。在实际开发中,开发者可以根据需求灵活地调整UIPickerView的样式、行为和数据源,以满足...

    iOS底部弹出日期选择器

    在iOS中,我们可以使用UIPickerView来创建一个自定义的日期选择器。为了实现底部弹出的效果,我们可以结合使用UIAlertController,它是iOS 8及更高版本中替代UIAlertView的新类,提供了更灵活的布局和内容展示方式。...

    IOS7编程指南

    通过这一节的学习,你可以了解如何创建和配置UIAlertView来显示各种类型的信息,包括简单的警告文本、带有确认按钮的对话框等。 **1.2 创建和使用UISwitch** UISwitch 是一个用于表示开/关状态的控件。本节将教你...

    ios7 programming cookbook

    - **显示警告框(UIAlertView)**:这部分内容介绍了如何在iOS应用中使用`UIAlertView`来显示警告信息或请求用户输入。这对于处理错误、确认操作或提供信息性提示非常重要。 - **创建和使用开关(UISwitch)**:`...

    IOS 7 CookBook

    书籍的目录涵盖了一系列iOS开发相关的主题,从实现控制器和视图的基础知识开始,逐步深入到更高级的技术实现,例如自定义UI控件。下面是一些书籍中提到的知识点: 1. 使用UIAlertView显示警告信息 在iOS开发中,...

    iOS 7 Programming Cookbook

    1. 创建和使用警告视图(UIAlertView):在iOS应用中,警告视图用于显示重要信息或需要用户确认的提示信息。第1.1节将指导如何显示警告视图。 2. 使用UISwitch控件:UISwitch是一个简单的开关组件,用于表示布尔型...

    iOS7 Programming Cookbook PDF版

    UIAlertView是iOS开发中常用的弹窗组件,用于向用户显示警告信息。 - 创建和使用UISwitch。UISwitch组件用于提供用户界面中的开关功能,实现布尔值的切换操作。 - 自定义UISwitch。书中还介绍了如何对UISwitch组件...

    iOS 7 programming cookbook

    使用UIAlertView可以在应用中向用户显示一个警告框,通常用于提醒用户某些信息或者请求用户进行决策。这个类在iOS 9之后被弃用,转而推荐使用UIAlertController。 - **1.2 创建和使用UISwitch** UISwitch是iOS中...

    ios7高级编程iOS7 Programming Cookbook

    在“实施控制器和视图”章节,作者详细讲解了如何使用UIAlertView来显示警告消息,UISwitch来创建和使用开关,UIPickerView来选择值,以及如何使用UIDatePicker来选择日期和时间。这些是构建iOS应用中不可或缺的基本...

    IOS7 Programming Cookbook

    类似于 `UISwitch`,`UISlider` 也可以通过自定义外观来增强用户的视觉体验。 **示例代码**: ```swift slider.minimumTrackTintColor = .green slider.maximumTrackTintColor = .gray ``` ##### 1.8 使用 `...

    [iOS 7 Programming Cookbook 2013 10 ] Vandad Nahavandipoor 文字版 pdf

    - 使用UIAlertView显示警告。 - 利用UISwitch创建并使用开关控件。 - 自定义UISwitch外观。 - 使用UIPickerView挑选值。 - 使用UIDatePicker挑选日期和时间。 - 实现范围选择器,如UISlider。 - 自定义...

    IOS应用源码——弹出框显示的Picker control.zip

    它提供了更多的自定义选项,包括添加自定义视图,如Picker View。通过设置UIAlertController的style为UIAlertControllerStyleActionSheet,我们可以创建一个底部弹出的界面,非常适合在这种情况下显示Picker View。 ...

    iOS开发,UI学习阶段,基本课程大纲

    - UIPickerView与UIDatePicker:了解选择器的使用,如日期选择、列表选择等。 5. **布局与约束** - Auto Layout:深入学习Auto Layout,理解约束的创建、修改和删除,以及约束优先级和更新布局的方法。 - Stack ...

Global site tag (gtag.js) - Google Analytics