简体   繁体   中英

Xcode button linking error?

Hey I was wondering is their any possible way I can link two actions to the same button in Xcode? I've already tried but keep getting this error: "terminating with uncaught exception of type NSException". So i'm guessing I am not able to do that? See what i'm trying to do is make a button play a sound but that same button is also linked to starting a new round in the game. How would I go about doing this? I've currently got this going in my .m file.

#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

And here's my .h file.

 //
 //  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

Some help would be greatly appreciated :)

Add an action like this to the button

-(IBAction)myButtonPressed:(id)sender
{
 [self playSound:sender];
 [self startNewRound:Sender];
}

Use only one action, just set a BOOL to check if you need to play the sound or not.

Example code would be:

-(IBAiction)btnPressed:(id)sender
{
  if(playsound)
  {
    [self playSound];
    playsound = NO;
  }
  [self startOver];
}

And then whenever you want the saund to be played again just set playsound to YES and next time user presses the button it will play the sound again

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM