zoukankan      html  css  js  c++  java
  • 重写AlertView(用block)

    @interface AlertView : UIView

    @property (nonatomic,copy) void(^block)(UIColor *color);

    - (id)initWithAlertView;

    - (void)showTwo;

    @end

     

     

     

     

    自定义View的.m文件

    - (id)initWithAlertView

    {

        self = [super init];//自定义init方法 就是重写了系统的init方法;

        if (self)

        {

            

            UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];

            btn1.backgroundColor = [UIColor redColor];

            btn1.frame = CGRectMake(5, 100, 40, 25);

            [btn1 addTarget:self action:@selector(btn1Click:) forControlEvents:UIControlEventTouchUpInside];

            [self addSubview:btn1];

            UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];

            btn2.backgroundColor = [UIColor greenColor];

            btn2.frame = CGRectMake(150, 100, 40, 25);

            [btn2 addTarget:self action:@selector(btn2Click:) forControlEvents:UIControlEventTouchUpInside];

            [self addSubview:btn2];

     

        }

        return self;

     

    }

    - (void)btn1Click:(UIButton *)btn

    {

        self.block(btn.backgroundColor);

        [self removeFromSuperview];

    }

    - (void)btn2Click:(UIButton *)btn

    {

        self.block(btn.backgroundColor);

        [self removeFromSuperview];

    }

     

     

     

    主界面的.m

    - (IBAction)buttonClick:(id)sender

    {

        AlertView *alertView = [[AlertView alloc] initWithAlertView];

        alertView.backgroundColor = [UIColor grayColor];

        alertView.frame = CGRectMake(100, 100, 200, 130);

    //    [alertView initWithAlertView];

        [self.view addSubview:alertView];

        __block ViewController *vc = self;

        alertView.block = ^(UIColor *color){

            vc.view.backgroundColor = color;

        };

    //    [alertView showTwo];要不要都行 看情况而定

    }

  • 相关阅读:
    HashTable、HashSet和Dictionary的区别
    CCF_ 201312-3_最大的矩形
    CCF_ 201312-2_ISBN号码
    CCF_201312-1_出现次数最多的数
    CCF_ 201509-2_日期计算
    CCF_ 201512-3_画图
    CCF_ 201512-2_消除类游戏
    CCF_ 201409-2_画图
    CCF_201409-1_相邻数对
    CCF_ 201412-1_门禁系统
  • 原文地址:https://www.cnblogs.com/dlwj/p/4875872.html
Copyright © 2011-2022 走看看