0

我编写了以下代码来在标识符播放和暂停之间切换按钮。按钮在暂停时必须是播放类型,在播放时必须是暂停类型。

- (IBAction)playSound:(id)sender {

    if (isPaused) {
        playOrPauseButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemPause target:self action:@selector(pausePlaying)];
//        playOrPauseButton.style = UIBarButtonSystemItemPause;
//        [playOrPauseButton setStyle:UIBarButtonSystemItemPause];
        isPaused = NO;
        NSLog(@"Playing");
    }

    else {

        playOrPauseButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(resumePlaying)];
//        playOrPauseButton.style = UIBarButtonSystemItemPlay;
//        [playOrPauseButton setStyle:UIBarButtonSystemItemPlay];
        isPaused = YES;
        NSLog(@"Paused");

    }

}

注释语句是我从网上得到的不同选项,我一一尝试。这三个选项都不是在播放和暂停状态之间切换。我已将按钮标识符设置为情节提要中的播放。无论我做什么,按钮仍然是播放按钮。如何将按钮切换为播放/暂停类型?

4

2 回答 2

2

只需为您的方法编写如下代码:-

-(IBAction)pausePlaying
{
    NSLog(@"push tap");
    if(playing == YES)
    {
        playOrPauseButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemPause target:self action:@selector(resumePlaying)];
        self.navigationItem.rightBarButtonItem = playOrPauseButton;
        playing= NO;
    }
}

-(IBAction)resumePlaying
{
    if(playing == NO)
    {  
        playOrPauseButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(pausePlaying)];
        self.navigationItem.rightBarButtonItem = playOrPauseButton;
        playing= YES;  
    }

    NSLog(@"resume tap");    
}


- (void)viewDidLoad
{
    playing=YES; 
    playOrPauseButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(pausePlaying)];
    self.navigationItem.rightBarButtonItem = playOrPauseButton;
    [super ViewDidLoad:animated];
}

工作截图:-

在此处输入图像描述

于 2013-07-29T05:49:45.010 回答
0

设置工具栏项的工作方式如下

UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[self.navigationController.toolbar setBarStyle:UIBarStyleBlackOpaque];
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithTitle:@"toolbar title" style:UIBarButtonItemStylePlain    target:self     action:@selector(onToolbarTapped:)];
NSArray *toolbarItems = [NSArray arrayWithObjects:spaceItem, customItem, spaceItem, nil];

[self setToolbarItems:toolbarItems animated:NO];

使一组 barbuttons 将其设置为工具栏项

所以在你的情况下,不仅不修改还在工具栏中设置它以使其正常工作

于 2013-07-26T07:30:50.300 回答