过去两天我一直在寻找并尝试一些自己的解决方案。我通过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"];
}
答案 0 :(得分:5)
我终于想通了......只花了几天但是我太忙了,不能发布一个解决方案。我终于有时间,很乐意发布我的解决方案。我有一种预感,除非以编程方式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];
}
好消息是我终于完成了我的应用并将其提交到应用商店。希望一切顺利,我期待着它!
答案 1 :(得分:0)
夫特,
我设置了一个实例var,并使用它来切换我的开关。我的导航栏中还有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)
}
}