updateLabels方法的错误访问错误

时间:2013-11-19 23:52:13

标签: objective-c uiviewcontroller

对于updateLabels方法,我遇到了错误的访问错误,但无法弄明白。这是代码:

BullsEyeViewController.m

#import "BullsEyeViewController.h"

@interface BullsEyeViewController ()

@end

@implementation BullsEyeViewController
{
    int _currentValue;
    int _targetValue;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self startNewRound];
    [self updateLabels];
}

-(void)startNewRound
{
    _targetValue = 1 + arc4random_uniform(100);
    _currentValue = 50;
    self.slider.value = _currentValue;
}

-(void)updateLabels
{
    self.targetLabel.text = [NSString stringWithFormat:@"%d", _targetValue]; //EXC_BAD_ACCESS (code = 2)
    // Convert the int into a string so that it will fit in the label as an outlet
    [self updateLabels];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)showAlert
{
    NSString *message = [NSString stringWithFormat:@"The value of the slider is: %d\nThe target value is %d", _currentValue, _targetValue];

    UIAlertView *alertView = [[UIAlertView alloc]
    initWithTitle:@"Hello World"
          message:message
         delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];

    [alertView show];

    [self startNewRound];
}

-(IBAction)sliderMoved:(UISlider *)slider
{
    _currentValue = lroundf(slider.value);
}

@end

3 个答案:

答案 0 :(得分:0)

它无限地呼唤着自己。不要在updateLabels内无条件地执行此操作...

[self updateLabels];

答案 1 :(得分:0)

为什么从updateLabels内拨打updateLabels?崩溃是由于无限递归造成的。

答案 2 :(得分:0)

您的updateLabels方法调用自身并创建无限循环。您的意思是[super updateLabels]吗?