【问题标题】:IBAction & Buttons programmaticallyIBAction & Buttons 以编程方式
【发布时间】: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


【解决方案1】:

发件人是testButn。您应该将stayPressed 的参数类型从 (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;
    }
}

【讨论】:

  • 嗯,这就是我目前所拥有的,但按钮仍未突出显示。 (IBAction)stayPressed:(UIButton *)sender{ UIButton *testButn =(UIButton *)sender; [testButn setHighlighted:YES]; }
  • 如果你看到我的原始代码,我有一个 UIControlStateHighlighted 的图像。我的操作与 touchupinside 有关。当我点击按钮时,它会突出显示,但不会保持突出显示。
  • @DavidWest,您需要使用选中状态,而不是突出显示。请参阅我的编辑。通过使用 touchDown,我仍然可以将按钮连接到内部触摸的其他操作。
  • 谢谢,是的,我的问题是使用突出显示状态而不是选中状态。
【解决方案2】:

您需要将 sender 强制转换为 UIButton。

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

    //do something to theButton
}

【讨论】:

  • 还是不行。这就是我现在所拥有的:-(IBAction)stayPressed:(id)sender{ UIButton *testButn =(UIButton *)sender; [testButn setHighlighted:YES]; }
  • 好的,但是什么不起作用?该功能是否正在运行?还是您遇到错误?
【解决方案3】:
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...
     }
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    相关资源
    最近更新 更多