【问题标题】:Setting a UIButton from a different class selected从选定的不同类设置 UIButton
【发布时间】:2014-07-10 11:00:02
【问题描述】:

我知道这不是一个非常高级的问题,但我感到迷茫。我有一个定义自定义表格单元格样式的类,其中包含我希望编辑的所需按钮。然后在另一个类中,我使用这种单元格类型并构造一个表格视图。然后我在不同类的按钮上写一个IBAction。当按钮被点击时,它应该保持选中状态。我已经尝试了几件事,但我无法正确获得按钮的引用以提供所需的结果。

我还在努力适应 Objective-C。

customcellstyle.m

   #import "imageCellCell.h"

    @implementation imageCellCell

    @synthesize view;
    @synthesize starbtn;


    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {

            view = [[UIView alloc] initWithFrame:self.frame];
            [self addSubview:view];


    // initiate favorites button       
            starbtn = [[UIButton alloc]initWithFrame:CGRectMake(243,0, 30, 30)];

            [starbtn setTintColor:[UIColor clearColor]]; 

            [starbtn setBackgroundImage:[UIImage imageNamed:@"startbefore.jpg"]
                               forState:UIControlStateNormal];
            [starbtn setBackgroundImage:[UIImage imageNamed:@"startafter.jpg"]
                               forState:UIControlStateSelected];
            [starbtn setBackgroundImage:[UIImage imageNamed:@"startafter.jpg"]
                               forState:UIControlStateHighlighted];
            [starbtn setBackgroundImage:[UIImage imageNamed:@"startafter.jpg"]


    [view addSubview:starbtn];
                               forState:UIControlStateHighlighted|UIControlStateSelected|UIControlStateDisabled];

tableclass.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

NSString *CellIdentifier;
NSString *CellIdentifierimg;

    UITableViewCell *cell;
    if (cell == nil) {
        if (indexPath.row == 0) {
            cell = [[imageCellCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierimg];
        } else if (indexPath.row == 1) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:CellIdentifier];

    }
    }




    switch ([indexPath row])
    {
        case 0:
        {

            imageCellCell *firstRowCell = (imageCellCell *)cell;  

            // reference of the favorites button to the buttonclick method

            [firstRowCell.starbtn addTarget:self action:@selector(clickFavButton:) forControlEvents:UIControlEventTouchUpInside];
            firstRowCell.accessoryType = UITableViewCellAccessoryNone;

            cell.selectionStyle = UITableViewCellSelectionStyleNone;


            break;

        }




-(IBAction)clickFavButton:(id)sender{


    customcellstyle *class = [[customcellstyle alloc] init];;


[class.starbtn setSelected:YES];
[class.starbtn setHighlighted:YES];

    [starbtn setSelected:YES];
    [starbtn setHighlighted:YES];

}

【问题讨论】:

    标签: ios xcode uitableview uibutton


    【解决方案1】:

    您应该创建一个自定义按钮类,您需要使用它的选定值设置和处理按钮图像,我对复选框按钮做了同样的事情,例如

    在.h文件中

    @interface MXCheckButton : UIButton {
        BOOL    _checked;
    }
    
    @property (nonatomic, setter = setChecked:) BOOL checked;
    
    -(void) setChecked:(BOOL) check;
    
    @end
    

    在你的 .m 文件中

    @implementation MXCheckButton
    @synthesize checked = _checked;
    
    -(id) init
    {
        if( self=[super init] )
        {
            self.checked = NO;
            [self addTarget:self action:@selector(OnCheck:) forControlEvents:UIControlEventTouchUpInside];
        }
        return self;
    }
    
    -(void) awakeFromNib
    {
        self.checked = NO;
        [self addTarget:self action:@selector(OnCheck:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    -(void) setChecked:(BOOL) check
    {
        _checked = check;
        if( _checked )
        {
            UIImage* img = [UIImage imageNamed:@"checked.png"];
            [self setImage:img forState:UIControlStateNormal];
        }
        else
        {
            UIImage* img = [UIImage imageNamed:@"unchecked.png"];
            [self setImage:img forState:UIControlStateNormal];
        }
    }
    
    -(void) OnCheck:(id) sender
    {
        self.checked = !_checked;
    }
    
    @end
    

    在这里您只需更改图像文件名即可开始使用!

    【讨论】:

    • 我可以在没有自定义按钮类的情况下更改图像。我的问题是单击按钮后我无法使图像保持不变/选中状态。
    • 是的,为此你可以为你的 tableRow 设置一个 BOOL 数组大小,并根据索引使它们为 YES 或 NO 以根据它们设置图像。
    • 我认为您没有理解我的问题。它是一个表格单元格上的一个 uibutton,单击时我需要它来保持 stateselected 的背景图像,而不是恢复到正常状态
    • 我想我已经给了你我认为应该可行的解决方案!
    • 但是如何在我的其他班级中使用这个自定义按钮?我也读过子类化 UIButton 不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-15
    • 1970-01-01
    • 1970-01-01
    • 2012-05-03
    • 1970-01-01
    相关资源
    最近更新 更多