Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何在代码中更改uibarbuttonite的样式_Iphone_Objective C_Ios - Fatal编程技术网

Iphone 如何在代码中更改uibarbuttonite的样式

Iphone 如何在代码中更改uibarbuttonite的样式,iphone,objective-c,ios,Iphone,Objective C,Ios,我正在使用UIBarButtonSystemItemPlay播放我的音频文件,我想在单击它时动态更改它的样式。有可能吗?如果有,请帮助我。这是我的代码,其中_playPause是uibarbuttonsystemplay的IBOutlet。提前谢谢 - (IBAction)playPause:(UIBarButtonItem *) sender { if (_playPause.style == UIBarButtonSystemItemPlay) { [_pla

我正在使用UIBarButtonSystemItemPlay播放我的音频文件,我想在单击它时动态更改它的样式。有可能吗?如果有,请帮助我。这是我的代码,其中_playPause是uibarbuttonsystemplay的IBOutlet。提前谢谢

- (IBAction)playPause:(UIBarButtonItem *) sender
{
    if (_playPause.style == UIBarButtonSystemItemPlay)
    {
        [_playPause setStyle:UIBarButtonSystemItemPause];
        [audio play];
    }
    else
    {
        [_playPause setStyle:UIBarButtonSystemItemPlay];
        [audio pause];
    }
}

我相信,一旦最初设置了样式(即在interface builder中或在代码中创建按钮时),就无法更改它

您唯一的其他选择是删除代码中的按钮,并使用不同的样式在其位置创建一个新按钮。您可以调用此方法:

- (id)initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action
- (void) setAsPlaying:(BOOL)isPlaying
{
    self.rootViewController.playing = isPlaying;

    // we need to change which of play/pause buttons are showing, if the one to
    // reverse current action isn't showing
    if ((isPlaying && !self.pauseButton) || (!isPlaying && !self.playButton))
    {
        UIBarButtonItem *buttonToRemove = nil;
        UIBarButtonItem *buttonToAdd = nil;
        if (isPlaying)
        {
            buttonToRemove = self.playButton;
            self.playButton = nil;
            self.pauseButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPause
                                                                             target:self
                                                                             action:@selector(pauseAudio:)];
            buttonToAdd = self.pauseButton;
        }
        else
        {
            buttonToRemove = self.pauseButton;
            self.pauseButton = nil;
            self.playButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay
                                                                            target:self
                                                                            action:@selector(playAudio:)];
            buttonToAdd = self.playButton;
        }

        // Get the reference to the current toolbar buttons
        NSMutableArray *toolbarButtons = [[self.toolbar items] mutableCopy];

        // Remove a button from the toolbar and add the other one
        if (buttonToRemove)
            [toolbarButtons removeObject:buttonToRemove];
        if (![toolbarButtons containsObject:buttonToAdd])
            [toolbarButtons insertObject:buttonToAdd atIndex:2];    // vary this index to put in diff spots in toolbar

        [self.toolbar setItems:toolbarButtons];
    }
}

有关
UIBarButtonItem
的详细信息,请查看。

uiBarButtonSystemImplay
uiBarButtonSystemImpause
无法按样式属性检索。样式的类型为
uibarbuttonimstyle

检查文档


我建议创建两个不同的
UIBarButtonItem
,然后交替启用或禁用(例如)。还可以删除当前的可见按钮,并添加一个新按钮,该按钮具有不同的
UIBarButtonSystemItem

我刚刚实现了一个非常完整的解决方案:

首先,定义变量以保持两个按钮:

@property (weak, nonatomic) IBOutlet UIToolbar *toolbar;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *playButton;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *pauseButton;
(我在这里的理解是,工具栏是一个弱引用,因为它是通过IB定义的,并且它有一个指向它的强指针,但是播放/暂停按钮是强指针,因为我们在下面创建它们。但是我的fu在这方面有点弱,作为一个相当新手,所以非常感谢。)

其次,在IB中,只创建一个播放按钮(没有暂停按钮),并将其绑定到上面的playButton变量

第三,建立这种方法:

- (id)initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action
- (void) setAsPlaying:(BOOL)isPlaying
{
    self.rootViewController.playing = isPlaying;

    // we need to change which of play/pause buttons are showing, if the one to
    // reverse current action isn't showing
    if ((isPlaying && !self.pauseButton) || (!isPlaying && !self.playButton))
    {
        UIBarButtonItem *buttonToRemove = nil;
        UIBarButtonItem *buttonToAdd = nil;
        if (isPlaying)
        {
            buttonToRemove = self.playButton;
            self.playButton = nil;
            self.pauseButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPause
                                                                             target:self
                                                                             action:@selector(pauseAudio:)];
            buttonToAdd = self.pauseButton;
        }
        else
        {
            buttonToRemove = self.pauseButton;
            self.pauseButton = nil;
            self.playButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay
                                                                            target:self
                                                                            action:@selector(playAudio:)];
            buttonToAdd = self.playButton;
        }

        // Get the reference to the current toolbar buttons
        NSMutableArray *toolbarButtons = [[self.toolbar items] mutableCopy];

        // Remove a button from the toolbar and add the other one
        if (buttonToRemove)
            [toolbarButtons removeObject:buttonToRemove];
        if (![toolbarButtons containsObject:buttonToAdd])
            [toolbarButtons insertObject:buttonToAdd atIndex:2];    // vary this index to put in diff spots in toolbar

        [self.toolbar setItems:toolbarButtons];
    }
}
第四,无论您要在何处播放或暂停模式,都可以调用setAsPlaying。例如:

-(IBAction) playAudio:(id)sender
{
    [self.audioPlayer play];
    [self setAsPlaying:YES];
}

-(IBAction) pauseAudio:(id)sender
{
    [self.audioPlayer pause];
    [self setAsPlaying:NO];
}
是的,一旦设置了“样式”(实际上不称为样式),就不能更改它

是的,你可以制作另一个按钮来修复它

以下是可重复使用的代码:

UIBarButtonItem *newButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
    target:controller.navigationItem.rightBarButtonItem.target
    action:controller.navigationItem.rightBarButtonItem.action];
controller.navigationItem.rightBarButtonItem = newButton;

这里,控制器通常是
self

您是否尝试使用
=
而不是
=
?…无论如何,我会使用ivar bool标志来检查音频是否正在播放。哦,对不起!非常感谢,我会检查它,稍后再来找您。我已经检查了它,但它不起作用。
uibarbuttonsystem
无法通过
style
属性检索<代码>样式属于
uibarbuttonimstyle
类型。必须使用[本答案中描述的方法][1]替换
uibarbuttonim
。[1] :另外,您不需要使用
strong
属性,因为您正在实例化
UIBarButtonItem
。您可以在方法中使用本地
UIBarButtonItem
变量和
weak
属性。您可以通过执行
NSInteger index=[toolbarButtons IndexOfoObject:buttonToRemove]
然后执行
[ToBarButtons replaceObjectAtIndex:index withObject:buttonToAdd]