简体   繁体   English

Xcode按钮链接错误?

[英]Xcode button linking error?

Hey I was wondering is their any possible way I can link two actions to the same button in Xcode? 嘿,我想知道他们是否可以将两个动作链接到Xcode中的同一按钮? I've already tried but keep getting this error: "terminating with uncaught exception of type NSException". 我已经尝试过,但一直收到此错误:“以类型为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. 我目前在.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

And here's my .h file. 这是我的.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 @结束

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. 仅需执行一项操作,只需将BOOL设置为一个即可检查是否需要播放声音。

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 然后,每当你想要的saund再次发挥刚刚成立playsoundYES ,下次用户按下按钮,它会再次播放声音

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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