嘿,我想知道他们可以将两个动作链接到Xcode中的同一个按钮吗?我已经尝试但是仍然遇到这个错误:"以NSException"类型的未捕获异常终止。所以我猜我无法做到这一点?看看我想做的是什么 按下按钮播放声音但同样的按钮也与在游戏中开始新一轮相关联。我该怎么做呢?我目前在我的.m文件中得到了这个。
#import "BullsEyeViewController.h"
@interface BullsEyeViewController ()
@end
@implementation BullsEyeViewController
{
int _currentValue;
int _targetValue;
int _score;
int _round;
}
- (IBAction)playSound:(id)sender {
SystemSoundID soundID;
NSString *buttonName=[sender currentTitle];
NSString *soundFile=[[NSBundle mainBundle]
pathForResource:buttonName ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)
[NSURL fileURLWithPath:soundFile], &
soundID);
AudioServicesPlaySystemSound(soundID);
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self startNewGame];
[self updateLabels];
UIImage *thumbImageNormal = [UIImage
imageNamed:@"SliderThumb-Normal"];
[self.slider setThumbImage:thumbImageNormal
forState:UIControlStateNormal];
UIImage *thumbImageHighlighted = [UIImage
imageNamed:@"SliderThumb-Highlighted"];
[self.slider setThumbImage:thumbImageHighlighted
forState:UIControlStateHighlighted];
UIImage *trackLeftImage =
[[UIImage imageNamed:@"SliderTrackLeft"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 14, 0, 14)];
[self.slider setMinimumTrackImage:trackLeftImage
forState:UIControlStateNormal];
UIImage *trackRightImage =
[[UIImage imageNamed:@"SliderTrackRight"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 14, 0, 14)];
[self.slider setMaximumTrackImage:trackRightImage
forState:UIControlStateNormal];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)startNewRound
{
_round += 1;
_targetValue = 1 + arc4random_uniform(100);
_currentValue = 50;
self.slider.value = _currentValue;
}
- (void)startNewGame
{
_score = 0;
_round = 0;
[self startNewRound];
}
- (void)updateLabels
{
self.targetLabel.text = [NSString stringWithFormat:@"%d",
_targetValue];
self.scoreLabel.text = [NSString stringWithFormat:@"%d",
_score];
self.roundLabel.text = [NSString stringWithFormat:@"%d",
_round];
}
- (BOOL)prefersStatusBarHidden
{
return YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)showAlert
{
int difference = abs(_targetValue - _currentValue);
int points = 100 - difference;
NSString *title;
if (difference == 0) {
title = @"Perfect!";
points += 100;
} else if (difference < 5) {
title = @"You almost had it!";
if (difference == 1) {
points += 50;
}
} else if (difference < 10 ) {
title = @"Pretty good!";
} else {
title = @"Not even close...";
}
_score+=points;
NSString *message = [NSString stringWithFormat:@"You scored %d points", points];
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle: title
message:message
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alertView show];
}
-(IBAction)sliderMoved:(UISlider *)slider
{
_currentValue = lroundf(slider.value);
}
- (void)alertView:(UIAlertView *)alertView
didDismissWithButtonIndex:(NSInteger)buttonIndex
{
[self startNewRound];
[self updateLabels];
}
-(IBAction)startOver
{
CATransition *transition = [CATransition animation];
transition.type = kCATransitionFade;
transition.duration = 1;
transition.timingFunction = [CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseOut];
[self startNewGame];
[self updateLabels];
[self.view.layer addAnimation:transition forKey:nil];
}
@end
这是我的.h文件。
//
// BullsEyeViewController.h
// BullsEye
//
// Created by Sebastian Shelley on 28/04/2014.
// Copyright (c) 2014 Sebastian Shelley. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
@interface BullsEyeViewController : UIViewController
<UIAlertViewDelegate>
@property (nonatomic, weak) IBOutlet UISlider *slider;
@property (nonatomic, weak) IBOutlet UILabel *targetLabel;
@property (nonatomic, weak) IBOutlet UILabel *scoreLabel;
@property (nonatomic, weak) IBOutlet UILabel *roundLabel;
-(IBAction)showAlert;
-(IBAction)sliderMoved:(UISlider *)slider;
-(IBAction)startOver;
- (IBAction)playSound:(id)sender;
@end
非常感谢一些帮助:)
答案 0 :(得分:0)
将这样的动作添加到按钮
-(IBAction)myButtonPressed:(id)sender
{
[self playSound:sender];
[self startNewRound:Sender];
}
答案 1 :(得分:0)
只使用一个动作,只需设置一个BOOL来检查是否需要播放声音。
示例代码为:
-(IBAiction)btnPressed:(id)sender
{
if(playsound)
{
[self playSound];
playsound = NO;
}
[self startOver];
}
然后,只要您想要再次播放saund,只需将playsound
设置为YES
,然后用户按下该按钮即可再次播放声音