setImage:forState: 在 iOS 3.2 中不起作用

发布于 2024-10-29 00:21:13 字数 706 浏览 0 评论 0原文

预期的行为是在单击播放操作时更改图像(从播放到暂停以及从暂停到播放)。这在 4.x 版本中按预期工作。在版本 3.2 中,setImage 不起作用,这意味着 setImage 不设置图像。在 3.2 版本中,调用 imageForState:UIControlStateNormal 返回 null。有什么想法吗?

@interface TestViewController : UIViewController {
  IBOutlet UIButton *playButton;
  BOOL play;
}

- (IBAction) play:(id)sender;

@end


@implementation TestViewController

- (IBAction) play:(id)sender {  
  if (play == YES) {
    [playButton setImage:[UIImage imageNamed:@"pause"] forState:UIControlStateNormal];    
   play = NO;
  } else {
    [playButton setImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];    
    play = YES;
  }
  //[playButton imageForState:UIControlStateNormal]

}

@end

The expected behavior is to change the image (from play to pause and from pause to play) when the play action is clicked. This works as expected in version 4.x. In version 3.2 the setImage does not work meaning the setImage does not set the image. Call to imageForState:UIControlStateNormal returns null in 3.2 version. Any ideas?

@interface TestViewController : UIViewController {
  IBOutlet UIButton *playButton;
  BOOL play;
}

- (IBAction) play:(id)sender;

@end


@implementation TestViewController

- (IBAction) play:(id)sender {  
  if (play == YES) {
    [playButton setImage:[UIImage imageNamed:@"pause"] forState:UIControlStateNormal];    
   play = NO;
  } else {
    [playButton setImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];    
    play = YES;
  }
  //[playButton imageForState:UIControlStateNormal]

}

@end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮生未歇 2024-11-05 00:21:13

您的代码看起来正确,您是否检查过是否在 XIB 文件中为按钮连接了 IBOutlet?

Your code looks correct, have you checked that you connected up the IBOutlet in your XIB file for your button?

微暖i 2024-11-05 00:21:13

UIImageimageNamed 接受带有扩展名的图像名称,即如果您将代码更改

[UIImage imageNamed:@"pause"]

[UIImage imageNamed:@"pause.png"] or [UIImage imageNamed:@"pause.jpg"]

您的代码将按照您的期望工作。

UIImage's imageNamed takes in a image name with extension i.e if you change your code

[UIImage imageNamed:@"pause"]

to

[UIImage imageNamed:@"pause.png"] or [UIImage imageNamed:@"pause.jpg"]

your code will work as per your expectations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文