Please Help With Slider

Discussion in 'iOS Programming' started by mandude, Dec 20, 2009.

  1. mandude macrumors member

    Joined:
    Nov 19, 2009
    #1
    hi i've done this before but for some reason its not working now all help is appreciated. im trying to get the value of the slider to go down by one everytime the user taps the screen. and id also like a label to represent the current value of the slider as a number here's my code so far:

    h. file:

    Code:
     @interface........ {
    UISlider *slider;
    UILabel *sliderLabel;
    }
    @property (nonatomic, retain) IBOutlet UISlider *slider;
    @property (nonatomic, retain) IBOutlet UILabel *sliderLabel;
    -(IBAction)sliderChanged:(id)sender;
    @end
    

    m. file:

    Code:
    @implementation......
    @synthesize slider;
    @synthesize sliderLabel;
    
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    if (touch) {
    slider.value = slider.value + (-1);
    }
    
        }
    
    -(IBAction)sliderChanged:(id) sender {
    slider = (UISlider *)sender;
    int progressAsInt = (int) (slider.value);
    NSString *string = [[NSString alloc] initWithFormat:@"%d", progressAsInt];
    sliderLabel.text = string;
    [string release];
    }
    
    -(void)dealloc {
    [slider release];
    [sliderLabel release];
    }
    
    @end
    
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    What exactly is not working?
     
  3. mandude thread starter macrumors member

    Joined:
    Nov 19, 2009
    #3
    the label that is supposed to show the sliders value does not shot the value. Yes i did make sure to make the proper connections in Interface Builder, yet the label doesnt say the current value of the slider. So lets the the slider value was 10, the label should read "10" when i tap the screen the slider should decrease and the label as well by one, making it "9" yep.
     
  4. KoolStar macrumors demi-god

    KoolStar

    Joined:
    Oct 16, 2006
    Location:
    Kentucky
    #4
    The issue was that you were trying to set the value in the action for when the slider moves. However, you are not actually interacting with the UISlider. By moving the setter for the label to the touch even it works. Also you can condense your code into two lines that occur in the touch method. You no longer need IBAction event for the slider. All you have to do is set the text label to the maximum value on view load or when ever you want using textLabel.text = ["Slider" maximumValue];

    HEADER
    Code:
    #import <UIKit/UIKit.h>
    
    @interface TouchCountDownViewController : UIViewController {
    
    	UISlider *slide;
    	UILabel *showValue;
    	
    }
    
    @property(nonatomic, retain) IBOutlet UISlider *slide;
    @property(nonatomic, retain) IBOutlet UILabel *showValue;
    
    @end
    
    MAIN
    Code:
    #import "TouchCountDownViewController.h"
    
    @implementation TouchCountDownViewController
    
    @synthesize slide;
    @synthesize showValue;
    
    -(void)viewDidLoad{
    	showValue.text = @"10";
    }
    
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    	UITouch *touch = [touches anyObject];
    	if (touch) {
    		slide.value -= 1;
    		showValue.text = [NSString stringWithFormat:@"%d", (int)[slide value]];
    	}
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
    }
    
    - (void)viewDidUnload {
    	self.slide = nil;
            showValue = nil;
    }
    
    
    - (void)dealloc {
    	[slide release];
    	[showValue release];
            [super dealloc];
    }
    
    @end
     
  5. mandude thread starter macrumors member

    Joined:
    Nov 19, 2009

Share This Page