Ios IBAction&;按钮编程

Ios IBAction&;按钮编程,ios,objective-c,uibutton,uistoryboard,ibaction,Ios,Objective C,Uibutton,Uistoryboard,Ibaction,我以编程的方式创建按钮,然后我想添加一些功能,当它们被点击/按下时,除非再次点击,否则它们将保持高亮显示。我现在做的是创建按钮,然后尝试添加iAction。但是,问题是我在方法中创建了按钮,然后我不确定如何在IBAction中引用按钮。这是我的密码: UIButton* testButn = [UIButton buttonWithType:UIButtonTypeCustom]; [testButn setFrame:CGRectMake(0, 135, 40, 38)]; [test

我以编程的方式创建按钮,然后我想添加一些功能,当它们被点击/按下时,除非再次点击,否则它们将保持高亮显示。我现在做的是创建按钮,然后尝试添加iAction。但是,问题是我在方法中创建了按钮,然后我不确定如何在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

- (IBAction)staypressed:(id)sender
{
    UIButton *theButton = (UIButton*)sender;

    //do something to theButton
}

发送者是testButn。您应该将statypressed的参数类型从(id)更改为(UIButton*)

除非一个动作方法连接到几个不同的对象类,否则最好用您正在使用的任何对象类替换id。如果您在IB中连接东西,这会很有用,因为它不会让您将它连接到错误类型的对象

它不能工作的事实并不是因为它不能识别你的按钮。你的方法是错误的。我认为你需要有一个连接到触地的动作,也许可以将所选状态设置为“是”。在按钮定义中,需要为选定状态设置imageForState:。按照您现在的操作方式,该方法在润色之前不会被调用

大概是这样的:

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIButton* testButn = [UIButton buttonWithType:UIButtonTypeCustom];
    [testButn setFrame:CGRectMake(0, 135, 40, 38)];
    [testButn setImage:[UIImage imageNamed:@"New_PICT0019.jpg"] forState:UIControlStateNormal];
    [testButn setImage:[UIImage imageNamed:@"New_PICT0002.jpg"]   forState:UIControlStateSelected];
    [testButn addTarget:self action:@selector(stayPressed:) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:testButn];
}

-(void)stayPressed:(UIButton *) sender {
    if (sender.selected == YES) {
        sender.selected = NO;
    }else{
        sender.selected = YES;
    }
}

ui按钮按钮=(ui按钮)发送器;theButton.selected=true。选中可能会禁用该按钮,因此可能只是更改了样式。仍不工作。这就是我现在拥有的:-(iAction)statypressed:(id)发送方{UIButton*testButn=(UIButton*)发送方;[testButn setHighlighted:YES];}仍不工作。这就是我现在所拥有的:-(iAction)statypressed:(id)sender{UIButton*testButn=(UIButton*)sender;[testButn setHighlighted:YES];}好的,但是什么不起作用?该函数是否正在运行?还是你有错误?嗯,这就是我目前的情况,按钮仍然没有突出显示。(iAction)statypressed:(UIButton*)发送方{UIButton*testButn=(UIButton*)发送方;[testButn setHighlighted:YES];}如果您看到我的原始代码,我有一个UIControlStateHighlighted的图像。我的行动与触地得分有关。当我点击按钮时,它会高亮显示,但不会一直高亮显示。@DavidWest,您需要使用选定的状态,而不是高亮显示。请参阅我的编辑。通过使用触地,我仍然可以将按钮连接到内部触地的其他动作。谢谢,是的,我的问题是使用突出显示的状态而不是选中状态。
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];
  testButn.tag = 1;
  [self.contentview addSubview:testButn

-(IBAction)staypressed:(id)sender
 {
     if ([sender tag]==1)
     {
         somecodes...
     }
 }