iOS 简单实现下拉列表框

来源:互联网 发布:mysql数据库基础 编辑:程序博客网 时间:2024/05/12 09:12

iOS 简单实现下拉列表框

import “LianXIViewController.h”

@interface LianXIViewController ()

// 定义视图作为下拉列表框内容
@property (nonatomic, retain)UIView * myView;

@end

@implementation LianXIViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    self.view.backgroundColor = [UIColor blueColor];

    // 首先创建一个 Button 按钮 点击弹出下拉列表框
    UIButton * myButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    // 给 按钮设置位置
    myButton.frame = CGRectMake(100, 100, 100, 30);
    // 设置 按钮名称
    [myButton setTitle:@”下拉列表框” forState:(UIControlStateNormal)];
    // 给按钮添加背景颜色
    myButton.backgroundColor = [UIColor orangeColor];
    // 添加按钮点击方法
    [myButton addTarget:self action:@selector(myActionButton) forControlEvents:(UIControlEventTouchUpInside)];
    // 将按钮加在主视图
    [self.view addSubview:myButton];

    // 思路:
    // 初始化下拉列表框 位置 在Button 下方 这个位置可以随便设在想要它出现的地方
    // 现将 alpha 设置为0 此时这个View 不显示 点击Button 的时候再判断透明度 alpha
    self.myView = [[UIView alloc] initWithFrame:(CGRectMake(100, 140, 100, 100))];
    // 设置背景颜色
    self.myView.backgroundColor = [UIColor redColor];
    // 设置两个 UILabel 添加到 下拉列表视图中 这个视图可以看做平常视图 随便使用
    UILabel * myLabel1 = [[UILabel alloc] initWithFrame:(CGRectMake(0, 0, 100, 40))];
    myLabel1.text = @”大师兄”;
    UILabel * myLabel2 = [[UILabel alloc] initWithFrame:(CGRectMake(0, 60, 100, 40))];
    myLabel2.text = @”二师兄”;
    // 将两个UILabel 添加到 myView 上
    [self.myView addSubview:myLabel1];
    [self.myView addSubview:myLabel2];
    // 设置 myView 的透明度 当为 0 的时候彻底透明 也可以理解为隐藏
    self.myView.alpha = 0;
    // 将myView 添加到 主视图上
    [self.view addSubview:self.myView];
    }

// 实现点击方法
- (void)myActionButton
{
NSLog(@”sldjflskjdf”);
// 思路: 判断myView 的 alpha 属性是否为 0
// 当为 0 的时候说明掩藏 则更改为1
// 当为 1 的时候说明显示 则更改为0
if (self.myView.alpha == 0) {
self.myView.alpha = 1;
} else {
self.myView.alpha = 0;
}
}

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }
    // 介绍到此结束 多谢各位看官观赏
0 0