views:

45

answers:

2

Heya again, I have been having problems creating a UISlider that can be used to modify a NSTimer I created. Essentially the slider is ment to modify the interger that the NSTimer is counting down from, but when I try to move the UISlider, the application crashes, I'm assuming this is because it is interfering with the countdown that is occuring, but I don't know what to do to fix this.

Here is the relevant code

   - (void)viewDidLoad {

    [label setFont:[UIFont fontWithName:@"DBLCDTempBlack" size:36.0]];
    label.text = @"I0A0IN6";
    mainInt = mySlider.value;
    timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/1.0) target:self selector:@selector (timerVoid) userInfo:nil repeats:YES];
    [super viewDidLoad];
}

- (void)timerVoid {

    mainInt += -1;
    if (mainInt == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Break Time!" 
                                                        message:@"Time to take a break, please go to the exorcises page during your break inorder to maximise it"
                                                       delegate:nil 
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles: nil];
        [alert show];

        [mainInt invalidate]; 
    }
    [label setFont:[UIFont fontWithName:@"DBLCDTempBlack" size:36.0]];
    label.text=[NSString stringWithFormat:@"%d" , mainInt];

}

The slider is called mySlider, and it is modifying the interger "mainInt" (line 5).

thanks in advance

+1  A: 

Few things:

  • [super viewDidLoad]; line is better be first in viewDidLoad.
  • There is no need to set the font each time timerVoid is executed.
  • As I have mentioned in the comment, you should call invalidate on timer and not on mainInt.
  • The slider does not modify mainInt - you have set the value of mainInt to hold the initial value of your slider and it is not changed by itself when you change the value of the slider. In order to do that you should create an IBAction and connect it to slider's valueChanged event. Inside that IBAction you may do what ever you want with the new value of the slider - for example set the value of mainInt or reschedule your timer.
  • You may also avoid using the IBAction by using the mySlider.value directly everywhere you need.
  • I don't see any reason for crashing the app though...

Possible code:

- (void)viewDidLoad {
    // This line should be first
    [super viewDidLoad];

    [label setFont:[UIFont fontWithName:@"DBLCDTempBlack" size:36.0]];
    label.text = @"I0A0IN6";

    // There is no need in mainInt
    //mainInt = mySlider.value;

    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerVoid) userInfo:nil repeats:YES];
}

- (void)timerVoid {

    // This line is much more readable like this ("-=" instead of "+= -")
    mySlider.value -= 1;

    // Much better to use "<=" in this case to avoid bugs
    if (mySlider.value <= 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Break Time!" 
                                                        message:@"Time to take a break, please go to the exorcises page during your break inorder to maximise it"
                                                       delegate:nil 
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles: nil];
        [alert show];
        [alert release]; // Haven't noticed the lack of release here earlier...

        // timer and not mainInt
        [timer invalidate]; 
    }

    // The next line is unnecessary 
    //[label setFont:[UIFont fontWithName:@"DBLCDTempBlack" size:36.0]];
    label.text = [NSString stringWithFormat:@"%d", mySlider.value];
}

EDIT:
Added the [alert release]; line

Michael Kessler
So I subbed in your code you sent, but its still having the same issue, but now instead of the timer displaying the sliders initial value as set in the IB (2700), it is displaying the value as 0, and again if I try to move the slider, the app crashes.
Callum Clarke
Also interestingly, if I hold onto the slider and move it up/down, hold it in a postion but not actually release they key, the slider is displaying numbers far off the mix/max I set in the IB (0-3600), with numbers such as 1616025731 or negatives of similar values...
Callum Clarke
If you had crashes with your initial code then I believe that you have some additional code that involves `mySlider`. Or `mySlider` is connected to some IBAction. There is no reason to crash with your original code - it should be just a slider that is not connected to anything.
Michael Kessler
so I decided to change mySlider to MySlider just incase I am missing something, I also redid the connections in mainView.xib, so now the only connection to the slider is "outlet - MySlider". the crashes have stopped, but the timer is still sticking on 0, and when the slider is moved the timer changes to a weird 10 digit number or 0 seemingly randomly. Any ideas?
Callum Clarke
I suggest you to create an IBAction method and connect it to valueChanged of the slider. Return the int value that I have remarked in my answer and update it inside the IBAction. This is the classic way and it always works for me. Just initiate the int as you have done it in the `viewDidLoad` and update it inside the IBAction. Inside the `timerVoid` use the int and not the value of the slider...
Michael Kessler
A: 

Your app is crashing because you haven't linked the valueChanged event from your slider to any proper IBAction method.

hotpaw2