简体   繁体   English

UIButton的自定义属性(iphone sdk)

[英]custom properties for UIButton (iphone sdk)

I have created a subclass for a UIButton and was hoping to pass a custom property to it. 我为UIButton创建了一个子类,并希望将自定义属性传递给它。 However, it's not working and I've since read that subclassing a UIButton isn't a great idea. 但是,它无法正常工作,从那以后我就知道对UIButton进行子类化不是一个好主意。

MY question is how do I assign a custom property to a button? 我的问题是如何为按钮分配自定义属性? I am creating a button which is being placed in the headerview of a grouped table. 我正在创建一个按钮,该按钮被放置在分组表的标题视图中。 I want to pass to that button the section number. 我想将部分编号传递给该按钮。 I've done this successfully with a UISwitch in each row, but can't use the same method for the UIButton. 我已经成功完成了每行中的UISwitch的操作,但是不能对UIButton使用相同的方法。

So I create a button 所以我创建一个按钮

MattsButtonClass *button = [MattsButtonClass buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(240, 15, 45, 30);
button.MFsectionNo = section; //this would be 1, 2 etc
//etc

So how do I actually do this? 那我该怎么做呢? you'll see that my UIbutton has a subclass, which looks like this 您会看到我的UIbutton有一个子类,看起来像这样

//.h file
@interface MattsButtonClass : UIButton {
    int MFsectionNo;
}

@property (nonatomic) int MFsectionNo;

@end

//.m file
@implementation MattsButtonClass

@synthesize MFsectionNo;


@end

The error on the above is 上面的错误是

-[UIRoundedRectButton MFsectionNo:]: unrecognized selector sent to instance 0x5673650
2010-09-07 22:41:39.751 demo4[67445:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIRoundedRectButton MFsectionNo:]: unrecognized selector sent to instance 0x5673650'

Is what I am trying not possible? 我尝试的是不可能的吗? Thanks for any help... 谢谢你的帮助...

您可能只需要使用UIButtontag属性来存储您的部分编号(可能带有偏移量,因为默认情况下tag为0)

This is not possible because you are using the function call: 这是不可能的,因为您正在使用函数调用:

[MattsButtonClass buttonWithType:UIButtonTypeCustom]

That message will return a UIButton not a MattsButtonClass. 该消息将返回一个UIButton 而不是 MattsButtonClass。 That is why the property does not exist on the returned object. 这就是为什么该属性在返回的对象上不存在的原因。 This makes it difficult to subclass UIButton. 这使得很难将UIButton子类化。 However you may be able to achieve your desired function by the following: 但是,您可以通过以下方法实现所需的功能:

MattsButtonClass *button = [[MattsButtonClass alloc] initWithFrame:CGRectMake(240, 15, 45, 30)];
button.MFsectionNo = section;

Maybe keep an array that has references to the buttons in each section header? 也许保留一个在每个节标题中都有对按钮的引用的数组?

-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIButton *myButton = [[UIButton alloc] init];
    [myButton addTarget:self action:@selector(myButton_touchUpInside:) forControlEvents:UIControlEventTouchUpInside];

    // previously defined as an instance variable of your viewcontroller
    // NSMutableArray *sectionHeaderButtons;
    [sectionHeaderButtons insertObject:myButton atIndex:section];
}

Then in the handler for the TouchUpInside event of the button: 然后在按钮的TouchUpInside事件的处理程序中:

- (void)myButton_touchUpInside:(id)sender
{
    int i;
    for (i = 0; i < [sectionHeaderButtons count]; i++)
    {
        if ([sectionHeaderButtons objectAtIndex:i] == sender)
            break; // now you know what section index was touched
    }
}

If the section index of the touched button is what you're after, this should do the trick. 如果要触摸的按钮的部分索引是您要查找的,这应该可以解决问题。 However, it's not optimal if: 但是,如果满足以下条件,则不是最佳选择

  1. You're rearranging the TableView sections, or 您正在重新排列TableView部分,或者
  2. You've got A LOT of sections 您有很多部分

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM