为 UIButton 设置多个自定义参数


.... Call to webservice
NSArray *someValues = [results valueForKey:@"someKey"];
[button setTitle:[NSString stringWithFormat:@"%@", [someValues objectAtIndex:i]] forState:UIControlStateNormal];
} // end
-(void)btnPressed:(id)sender
{
UIButton *button = sender;
button.currentTitle; // Title comes back just fine
}

I want to be able to add more parameters so I can get them back on the click method. So example
button.someParam = @"My name"
...
// once clicked
UIButton *button = sender;
button.someParam // gets back "My Name"

目前,我可以设置自定义标题并将其恢复正常。我想添加更多值以与我的按钮一起传递。这可能吗?这些值是动态返回的。我知道我可以设置标签#,但这不会帮助我从我尝试过的中获取自定义值。

您希望向UIButton添加属性。

执行此操作的一种方法是创建一个UIButton子类并在那里添加属性。然后使用该子类而不是常规UIButton

我现在通过某种黑客修复解决了这个问题。我会继续寻找实现这一点的最佳方法。但是现在我只是使用可访问性值作为参数,然后我以后就可以检索了。

button.accessibilityValue = [NSString stringWithFormat:@"%@", [someArray objectAtIndex:i]];

最新更新