我编写了以下代码来切换标识符播放和暂停之间的按钮。按钮在播放时必须是播放类型,并在播放时暂停。
- (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");
}
}
评论的陈述是我从网上得到的不同选项,我逐一尝试。这三个选项都没有在播放和暂停状态之间切换。我在故事板中将按钮标识符设置为播放。无论我做什么,按钮仍然是播放按钮。如何将按钮切换为播放/暂停类型?
答案 0 :(得分: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];
}
工作屏幕截图: -
答案 1 :(得分: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];
制作一组条形按钮,将其设置为工具栏项
因此,在您的情况下,不仅不修改也在工具栏中设置它以使其正常工作