关闭

block使用的一个例子

标签: 界面编辑器
223人阅读 评论(0) 收藏 举报
分类:

.h
// Copyright © 2015年 1. All rights reserved.
//

import

import “BlockDemoViewController.h”

import “BlockCustomItem.h”

import “BlockCustomButton.h”

@interface BlockDemoViewController ()

@end

@implementation BlockDemoViewController

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

    BlockCustomItem *customItem = [[BlockCustomItem alloc] init];
    customItem.block = ^{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@”提示” delegate:self cancelButtonTitle:@”取消” otherButtonTitles:@”确定”, nil];
    [alert show];
    };

    BlockCustomButton *blockButton = [BlockCustomButton buttonWithType:UIButtonTypeCustom];
    blockButton.frame = CGRectMake(50, 100, 100, 30);
    blockButton.backgroundColor = [UIColor colorWithRed:59.0/255.0 green:249.0/255.0 blue:199.0/255.0 alpha:1.0];
    blockButton.item = customItem;
    [blockButton addTarget:self action:@selector(blockButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:blockButton];
    }

  • (void)blockButtonAction:(BlockCustomButton *)button {
    if (button.item.block) {
    button.item.block();
    }
    }

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

@end

.h

import

import “BlockCustomItem.h”

@interface BlockCustomButton : UIButton

@property (nonatomic, strong) BlockCustomItem *item;

@end

.m

import “BlockCustomButton.h”

@implementation BlockCustomButton

@end

.h

import

import “BlockCustomItem.h”

@implementation BlockCustomItem

  • (id)init {
    self = [super init];
    if (self) {
    self.title = @”“;
    }
    return self;
    }

@end

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:142741次
    • 积分:2134
    • 等级:
    • 排名:第18446名
    • 原创:59篇
    • 转载:91篇
    • 译文:0篇
    • 评论:11条
    最新评论
    精彩推荐