【发布时间】:2013-01-31 16:20:21
【问题描述】:
我正在以编程方式创建按钮,然后我想添加一些功能,当它们被点击/按下时,它们会保持突出显示,除非再次点击。我现在正在做的是创建按钮,然后尝试添加 IBAction。但是,问题是我在一个方法中创建了按钮,然后我不确定如何在我的 IBAction 中引用该按钮。这是我的代码:
UIButton* testButn = [UIButton buttonWithType:UIButtonTypeCustom];
[testButn setFrame:CGRectMake(0, 135, 40, 38)];
[testButn setImage:[UIImage imageNamed:@"test_butn_un.png"] forState:UIControlStateNormal];
[testButn setImage:[UIImage imageNamed:@"test_butn_pressed.png"] forState:UIControlStateHighlighted];
[testButn addTarget:self action:@selector(staypressed:) forControlEvents:UIControlEventTouchUpInside];
[self.contentview addSubview:testButn
-(IBAction)staypressed:(id)sender{
//Not sure what to do here, since this method doesn't recognize testButn, How do I reference testButn
【问题讨论】:
-
UIButton theButton = (UIButton)sender; theButton.selected = true; .selected 可能会禁用按钮,因此可能只是更改了样式。
-
还是不行。这就是我现在所拥有的:-(IBAction)stayPressed:(id)sender{ UIButton *testButn =(UIButton *)sender; [testButn setHighlighted:YES]; }
标签: ios objective-c uibutton uistoryboard ibaction