封装顶部选择按钮(用block实现在当前控制器下面添加button点击方法)

本文详细介绍了如何使用Objective-C进行iOS应用开发,包括基本语法、开发工具、设计模式和实践经验分享。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

******************************************************************************
baseView.h
#import <UIKit/UIKit.h>

typedef void(^ClickBlock)(UIButton *);

@interface baseView : UIView
{
   
BOOL isSelected;
}
@property(nonatomic, copy)ClickBlock block;
@property (nonatomic, retain)UIButton *btn;
@property (nonatomic, retain)UIView *smallLineV;
@property (nonatomic, retain)NSArray *titleArr;

-(
instancetype)initWithFrame:(CGRect)frame titleArr : (NSArray *)titleArr;
- (
void)addBlock:(ClickBlock)block;
@end
******************************************************************************
baseView.m
#import "baseView.h"
#import
"UIViewExt.h"
// 获取设备屏幕的物理尺寸
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define kScreenWidth [UIScreen mainScreen].bounds.size.width

@implementation baseView
-(
instancetype)initWithFrame:(CGRect)frame titleArr : (NSArray *)titleArr
{
self = [super initWithFrame:frame];
   
if (self) {
       
self.titleArr = titleArr;
        [
self _initViews];
    }
   
return self;
}

-(
void)_initViews
{
   
for (int i = 0; i < _titleArr.count ; i ++) {
       
self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
       
_btn.frame = CGRectMake(kScreenWidth/_titleArr.count * i, 0, kScreenWidth/_titleArr.count, self.height - 1);
       
_btn.tag = i;
        [
_btn setTitle:_titleArr[i] forState:UIControlStateNormal];
        [
_btn setTitleColor:[UIColor colorWithRed:0.66f green:0.66f blue:0.66f alpha:1.00f] forState:UIControlStateNormal];
        [
_btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [
self addSubview:_btn];
       
    }
   
_smallLineV = [[UIView alloc]initWithFrame:CGRectMake(0, self.height , kScreenWidth/_titleArr.count, 1)];
   
_smallLineV.backgroundColor = [UIColor colorWithRed:0.24f green:0.25f blue:0.23f alpha:1.00f];
    [
self addSubview:_smallLineV];
   
UIView *lineV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 1)];
    lineV.
top = self.height - 1;
    lineV.
backgroundColor = [UIColor colorWithRed:0.95f green:0.95f blue:0.95f alpha:1.00f];
    [
self addSubview:lineV];

   
}
- (
void)addBlock:(ClickBlock)block
{
   [
_btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
  
self.block = block;
}
#pragma mark 按钮点击事件
-(void)btnAction : (UIButton *)btn
{
   
if (self.block != nil) {
       
self.block(btn);
    }
   
_smallLineV.frame = CGRectMake(kScreenWidth/_titleArr.count *btn.tag, self.height , kScreenWidth/_titleArr.count, 1);
   
for(id cc in [self subviews])
    {
       
if([cc isKindOfClass:[UIButton class]])
        {
           
UIButton *btn = (UIButton *)cc;
            [btn
setTitleColor:[UIColor colorWithRed:0.66f green:0.66f blue:0.66f alpha:1.00f] forState:UIControlStateNormal];
        }
    }
   
isSelected = !isSelected;
   
if (!isSelected) {
        [btn
setTitleColor:[UIColor colorWithRed:0.66f green:0.66f blue:0.66f alpha:1.00f] forState:UIControlStateNormal];
    }
else
    {
        [btn
setTitleColor:[UIColor colorWithRed:0.00f green:0.00f blue:0.00f alpha:1.00fforState:UIControlStateNormal];
       
isSelected = NO  ;
    }
}
@end
******************************************************************************

ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
******************************************************************************
ViewController.m
#import "ViewController.h"
#import
"baseView.h"
// 获取设备屏幕的物理尺寸
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define kScreenWidth [UIScreen mainScreen].bounds.size.width

@interface ViewController ()

@end

@implementation ViewController

- (
void)viewDidLoad {
    [
super viewDidLoad];
   
NSArray *arr = @[@"你好",@"是的",@"不是的"];
   
__block ViewController *this = self;
   
baseView *baseV = [[baseView alloc]initWithFrame:CGRectMake(0, 44, kScreenWidth, 30) titleArr:arr];
    [baseV
addBlock:^(UIButton *btn) {
        [this
text:btn];
    }];
    [
self.view addSubview:baseV];
   
// Do any additional setup after loading the view, typically from a nib.
}

-(
void)text : (UIButton*)btn
{
   
//点击按钮执行的方法
   
switch (btn.tag) {
       
case 0:
        {
           
NSLog(@"1111");
        }
           
break;
       
case 1:
        {
           
NSLog(@"2222");
 
        }
           
break;
       
case 2:
        {
           
NSLog(@"333");

        }
            break;          
        default:
           
break;
    }
}
@end
******************************************************************************
效果图如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值