在 UIBarButtonItem 中切换自定义图像

发布于 2024-09-07 04:43:58 字数 548 浏览 7 评论 0原文

在过去的两天里,我一直在绞尽脑汁地寻找并尝试一些自己的解决方案。我通过 IB 放置了一个 UIBarButtonItem,并在顶部栏中放置了一个图像,作为静音/取消静音按钮。除了图像不变之外,一切正常。我使用了以下代码,它可以编译但没有改变

if( mute == YES ) {
    UIImage *unmuteImage = [UIImage imageNamed:@"audio-on.png"];
    [self.muteButton setImage:unmuteImage];
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"muteKey"];
}
else {
    UIImage *muteImage = [UIImage imageNamed:@"audio-off.png"];
    [self.muteButton setImage:muteImage];
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"muteKey"];
}

I've been breaking my head for the past two days searching and trying some of my own solutions. I placed a UIBarButtonItem through IB with an image in the top bar to act as a mute/unmute button . Everything works except the image doesn't change. I used the following code and it compiles but no change

if( mute == YES ) {
    UIImage *unmuteImage = [UIImage imageNamed:@"audio-on.png"];
    [self.muteButton setImage:unmuteImage];
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"muteKey"];
}
else {
    UIImage *muteImage = [UIImage imageNamed:@"audio-off.png"];
    [self.muteButton setImage:muteImage];
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"muteKey"];
}

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

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

发布评论

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

评论(2

我要还你自由 2024-09-14 04:43:58

我终于弄清楚了......只花了几天时间,但我太忙了,无法发布解决方案。我终于有时间了,很高兴发布我的解决方案。我有预感,除非 100% 以编程方式完成,否则这是行不通的,我是对的。这是我的问题的最终解决方案:

if(mute == YES)
{
    UIImage *image = [UIImage imageNamed:@"audio-off.png"];
    UIButton *myMuteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    myMuteButton.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );    
    [myMuteButton setImage:image forState:UIControlStateNormal];
    [myMuteButton addTarget:self action:@selector(mute) forControlEvents:UIControlEventTouchUpInside];    
    UIBarButtonItem *myMuteBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myMuteButton];   
    navBar.leftBarButtonItem = myMuteBarButtonItem;
    [myMuteBarButtonItem release];
}
else
{
    UIImage *image = [UIImage imageNamed:@"audio-on.png"];
    UIButton *myUnmuteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    myUnmuteButton.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );    
    [myUnmuteButton setImage:image forState:UIControlStateNormal];
    [myUnmuteButton addTarget:self action:@selector(mute) forControlEvents:UIControlEventTouchUpInside];    
    UIBarButtonItem *myUnmuteBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myUnmuteButton];
    navBar.leftBarButtonItem = myUnmuteBarButtonItem;
    [myUnmuteBarButtonItem release];
}

好消息是我终于完成了我的应用程序并将其提交到应用程序商店。希望一切顺利,我很期待!

I finally figured it out...only took a couple of days but I've been too busy to post up a solution. We'll I finally got time and am happy to post my solution. I had a hunch that this would'nt work unless it was done 100% programmatically, and I was right. Here's the final solution to my problem:

if(mute == YES)
{
    UIImage *image = [UIImage imageNamed:@"audio-off.png"];
    UIButton *myMuteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    myMuteButton.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );    
    [myMuteButton setImage:image forState:UIControlStateNormal];
    [myMuteButton addTarget:self action:@selector(mute) forControlEvents:UIControlEventTouchUpInside];    
    UIBarButtonItem *myMuteBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myMuteButton];   
    navBar.leftBarButtonItem = myMuteBarButtonItem;
    [myMuteBarButtonItem release];
}
else
{
    UIImage *image = [UIImage imageNamed:@"audio-on.png"];
    UIButton *myUnmuteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    myUnmuteButton.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );    
    [myUnmuteButton setImage:image forState:UIControlStateNormal];
    [myUnmuteButton addTarget:self action:@selector(mute) forControlEvents:UIControlEventTouchUpInside];    
    UIBarButtonItem *myUnmuteBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myUnmuteButton];
    navBar.leftBarButtonItem = myUnmuteBarButtonItem;
    [myUnmuteBarButtonItem release];
}

the good news is I finally finished my app and submitted it to the app store. Hopefully everything will go smooth and am looking forward to it!

内心激荡 2024-09-14 04:43:58

Swift,

我围绕实例变量设置了我的变量,并用它来切换我的开关。我的导航栏中也有 3 个按钮。

private var activeStaff:Staff? {
    didSet {
        let image = (activeStaff == nil) ? UIImage(named: "active")! : UIImage(named: "notActive")!
        let button = UIBarButtonItem(image: image, style: .Plain, target: self, action: "activePressed:")

        if navigationItem.rightBarButtonItems?.count == 3 {
            navigationItem.rightBarButtonItems?.removeAtIndex(0)
        }

        navigationItem.rightBarButtonItems?.insert(button, atIndex: 0)
    }
}

Swift,

I set mine around an instance var, and used that to toggle my switch. I also had 3 buttons in my nav bar.

private var activeStaff:Staff? {
    didSet {
        let image = (activeStaff == nil) ? UIImage(named: "active")! : UIImage(named: "notActive")!
        let button = UIBarButtonItem(image: image, style: .Plain, target: self, action: "activePressed:")

        if navigationItem.rightBarButtonItems?.count == 3 {
            navigationItem.rightBarButtonItems?.removeAtIndex(0)
        }

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