Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mandude

macrumors member
Original poster
Nov 19, 2009
64
0
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
 

mandude

macrumors member
Original poster
Nov 19, 2009
64
0
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.
 

KoolStar

macrumors demi-god
Oct 16, 2006
825
9
Kentucky
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.