单击Objective-C中按钮图像时为什么不调用发送方?

时间:2014-04-30 02:21:51

标签: ios objective-c uibutton

我是IOS和Objective-C的新手,我尝试为Image设置UIButton。 我点击按钮,按钮将改变image1。如果我再次点击button,则会更改为image2

首先,我在image中设置了Viewdidload,如下面的代码。

    @interface AITPreviewViewController ()
    {
        VLCMediaPlayer *mediaPlayer ;
        BOOL recording ;
        BOOL recordMode;
    }
    - (void)viewDidLoad
    {
        recordMode = YES;
        UIButton *modeChangeButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 450, 60, 60)];
        [modeChangeButton setBackgroundImage:[UIImage imageNamed:@"recordmode.PNG"] forState:UIControlStateNormal];

        [self.view addSubview:modeChangeButton];
    }

单击按钮时,按钮将更改图像。代码如下:

- (IBAction)modeButtonClick:(id)sender {
    NSLog(@"modeButtonClick..!!!!!!");

    if (recordMode == YES) {
        NSLog(@"modeButtonClick..%hhd = " , recordMode);
        [self.modeChangeButton setBackgroundImage:[UIImage imageNamed:@"photomode.png"] forState:UIControlStateNormal];
        recordMode = NO;

    }else if (recordMode == NO){
        NSLog(@"modeButtonClick..%hhd = " , recordMode);
        [self.modeChangeButton setBackgroundImage:[UIImage imageNamed:@"recordmode.png"] forState:UIControlStateNormal];
        recordMode = YES;
    }

}

单击按钮上的图像时,按钮的图像会变暗。似乎按钮已被按下。但是- (IBAction)modeButtonClick:(id)sender didn't called

单击空白区域(图像侧面)时,按下按钮。 - (IBAction)modeButtonClick:(id)sender has been calledheader file。而且图像也会变成其他图像。

我确信我已在.m file和{{1}}设置了操作。

以上描述如下图所示。 enter image description here

按钮似乎是按图像覆盖,图像和按钮不一样。 为什么会这样? 如何解决这个问题?

提前致谢。

4 个答案:

答案 0 :(得分:1)

您可以实施的一些变更:

第一: 将setBackgroundImage更改为setImage。 例如:[self.modeChangeButton setImage: [UIImage imageNamed: @"recordmode.png" forControlState: UIControlStateNormal];

这样,如果您错误地将图像设置为按钮,则会覆盖它。

第二: 确保已在“接口”构建器中连接了参考插座(IBOutlet)和操作(IBActions) - 无论是XIB文件还是故事板。如果您没有将接口对象链接到代码中的接口对象,它们将无法工作。

第三: 如果仍然无效,请尝试将此行添加到viewDidLoad

[self.modeChangeButton addTarget:self @selector(modeButtonClick:) forControlEvents:UIControlEventTouchUpInside];

第四: 在viewDidLoad

中更改此行
UIButton *modeChangeButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 450, 60, 60)];

为:

self.modeChangeButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 450, 60, 60)];

这可以解决您的问题。

答案 1 :(得分:0)

尝试按如下方式修改viewDidLoad方法。

- (void)viewDidLoad
{
   [super viewDidLoad];

   UIButton *modeChangeButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 300, 60, 60)];
   [modeChangeButton setBackgroundImage:[UIImage imageNamed:@"recordmode.PNG"] forState:UIControlStateNormal];

   // You might need to add following two lines into your code.
   [modeChangeButton addTarget:self action:@selector(modeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
   self.modeChangeButton = modeChangeButton;

   [self.view addSubview:modeChangeButton];
}

以上解决方案应该有效。正如我所看到的,你在modeButtonClick:方法中引用了名为modeChangeButton的属性。但是您没有将创建的按钮分配到您的属性中。您还必须将操作选择器设置为按钮。

作为改进,您可以这样做以提高应用程序的性能。如果不在单击事件上设置背景图像,则可以选择/取消选择按钮。有了这个,您不需要维护布尔标志,这可能会给您的应用程序带来新的错误。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIButton *modeChangeButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 300, 60, 60)];
    [modeChangeButton setBackgroundImage:[UIImage imageNamed:@"recordmode.png"] forState:UIControlStateNormal];
    [modeChangeButton setBackgroundImage:[UIImage imageNamed:@"photomode.png"] forState:UIControlStateSelected];

    [modeChangeButton addTarget:self action:@selector(modeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    self.modeChangeButton = modeChangeButton;

    [self.view addSubview:modeChangeButton];
}

- (IBAction)modeButtonClick:(id)sender {
    NSLog(@"modeButtonClick..!!!!!!");
    [self.modeChangeButton setSelected:![self.modeChangeButton isSelected]];
}

希望这有帮助。

答案 2 :(得分:0)

此代码在self.modeChangeButton之上添加了一个新按钮,我假设它来自xib或storyboard。

 - (void)viewDidLoad
    {
        recordMode = YES;
        UIButton *modeChangeButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 450, 60, 60)];
        [modeChangeButton setBackgroundImage:[UIImage imageNamed:@"recordmode.PNG"] forState:UIControlStateNormal];

        [self.view addSubview:modeChangeButton];
    }

这样做......

 - (void)viewDidLoad {
        self.recordMode = YES;
    }

- (void)setRecordMode:(BOOL)recordMode {
    _recordMode = recordMode;
    UIImage *imageForMode = recordMode ? [UIImage imageNamed:@"photomode.png"] : [UIImage imageNamed:@"recordmode.png"];
    [self.modeChangeButton setBackgroundImage:imageForMode forState:UIControlStateNormal];
}

- (IBAction)modeButtonClick:(id)sender {
    self.recordMode = !self.recordMode;
}

答案 3 :(得分:0)

检查按钮尺寸并使框架尺寸更接近图像尺寸。并尝试维护两个图像,如下所示。

- (void)viewDidLoad
{
      UIButton *modeChangeButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 450, 60, 60)];

      [modeChangeButton setImage:[UIImage imageNamed:@"recordmode.PNG"] forState:UIControlStateNormal];

      [modeChangeButton setImage:[UIImage imageNamed:@"photomode.png"] forState:UIControlStateSelected];

      [modeChangeButton addTarget:self action:@selector(modeButtonClick:) forControlEvents:UIControlEventTouchUpInside];

      [self.view addSubview:modeChangeButton];
}

- (IBAction)modeButtonClick:(id)sender 
{
    sender.selected = !sender.selected;
}