I want to set the value of my uislider like this (4.8,4.9,4.10,4.11,5.0,5.1,5.2,5.3,5.4,5.5,5.6,5.7,5.8,5.9,5.10,5.11,6.0,6.1,6.2,6.3) but don't know how to do that. Can anyone help me with this? I even don't know how to code this. I only created the outlet of uislider.
you can set slidervalue like,
self.mySlider.minimumValue = 4.8;
selfmySlider.maximumValue = 6.8;
self.mySlider.value = 5.8; //cuerrent value
if you change above value (as per question) then you can shedule timer (NSTimer) and in selector method of this timer you can change slidervalue according to your value array that posted in question.
Update as per comment :
NSTimer * sliderTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(updateSlider) userInfo:nil repeats:YES];
int counter = 0; take counter to differentiate object.
NSArray *arr = []; //this is your value arr according to question
then in your update slider method update your slider smething like,
- (void)updateSlider{
mySlider.value = [[arr objectAtIndex:counter] floatValue];
counter ++;
}
Something lie this, manage according to your context. this is example. and please avoid syntax mistake if any.
Hope this will help :)
Related
I am using an NSTimer to call a method which is titled Lose. I had a timer which when it ran out, it called Lose, but I lost everything due to a hard drive error. After trying to code it all again, I can't seem to get the method to be called.
Timer = [NSTimer timerWithTimeInterval:timeMax target:self selector:#selector(Lose) userInfo:nil repeats:NO];
Lose is declared in my .h file, like this:
-(void)Lose;
Also, my method looks like this:
-(void)Lose{
Text.hidden = NO;
scoreLabel.hidden = NO;
Target.hidden = YES;
Targetx.hidden = YES;
if (Score > highScoreNumber) {
highScoreAchieved.hidden = NO;
highScoreNumber = Score;
}
}
The variable timeMax is an int declared in my .h file, like last time.
whenever a target is tapped in my game, timeMax becomes .03 seconds shorter. I do it like this:
timeMax = 5 - (Score * 0.03);
I don't remember it looking different before the massive hardware failure, but why isnt it working?
You have to schedule the timer on a run loop or just use this line instead which schedules it for you:
Timer = [NSTimer scheduledTimerWithTimeInterval:timeMax
target:self
selector:#selector(Lose)
userInfo:nil
repeats:NO];
It's also a good idea to always back up your code...which reminds me.
I have an NSTimer which I want to update a label every second. My code is:
- (IBAction)OnClickEmergencyButton:(id)sender
{
emergencyAlertTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(emergencyTimer) userInfo:nil repeats:YES];
[emergencyAlertTimer fire];
}
- (void)emergencyTimer
{
int i = 0;
_emergencyAlertTriggerTimerLabel.text = [NSString stringWithFormat:#"%d", ++i];
}
When I ran it, the label displayed "1" initially and then stopped.
I want the label to continuously count up every second, like "1", "2", "3", ...
There is no issue with your timer. The issue is with the variable declaration inside the emergencyTimer, you declared it as a local variable. So each time when the timer fires the variable will be initialized to 0 again. So declare the variable as static, so that it can preserve the value.
Change the method like:
-(void)emergencyTimer
{
static int timeValue = 0;
_emergencyAlertTriggerTimerLabel.text = [NSString stringWithFormat:#"%d",++timeValue];
}
Why static variable and Why not instance variable ?
I didn't used instance variable, for keeping the variable "Scope" safe. If I put it as an instance variable, it can be accessed by other methods of the same class, if there is no need of that functionality, I think using a static variable will be better.
Issue is with this code
int i=0;
Every time when timer method gets called, the integer i gets initialized and label will be displayed as "1" always.
Make this variable global or static to fix your issue.
Remove int i=0; from your timer action method because it will always have a value of zero. You should be using an instance variable (#property) to store the timerCounter and increment that and use it to populate the label.
At some point in time you need to invalidate the timer. This is particularly important to do before you create a new timer and replace the reference to the old timer. Currently, if you press the button twice, you will then have 2 timers running and your label will increment twice a second...
#property (nonatomic, assign) NSInteger timerCounter;
- (IBAction)OnClickEmergencyButton:(id)sender {
[emergencyAlertTimer invalidate];
emergencyAlertTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(emergencyTimer) userInfo:nil repeats:YES];
[emergencyAlertTimer fire];
}
- (void)emergencyTimer {
_emergencyAlertTriggerTimerLabel.text = [NSString stringWithFormat:#"%d", self.timerCounter];
self.timerCounter++;
}
You should also invalidate the timer when the view is removed from display / deallocated.
Always timer get called emergencyTimer but your value of i won't changed because it's an local variable, scope of i will remain at end of function call. Try this with static variable which remain globally...
-(void)emergencyTimer{
static int i=0; // initialize at first time only..
_emergencyAlertTriggerTimerLabel.text = [NSString stringWithFormat:#"%d",++i];
if ( i == 100)
[ emergencyAlertTimer invalidate] // stop at certain condition
}
Firstly everyone is right you will only display a 0 no matter what you do currently so use an instance variable.
With regards to the only firing once, instead of [NSTimer fire] try this:
emergencyAlertTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(emergencyTimer) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:emergencyAlertTimer forMode:NSRunLoopCommonModes];
here is my code:
-(void)flash_random_winning_number:(NSInteger)param_which_magic_number
{
NSInteger dd = 9;
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:.5
target:self
selector:#selector(ShowLabel22:dd:)
userInfo:Nil
repeats: YES
];
}//flash_random_winning_number
the problem is with selector:#selector(ShowLabel22:dd:) because I am sending a parameter to this method called ShowLabel22:
-(void)ShowLabel:(NSInteger)param_which_magic_number
{
random_magic_number1.hidden = NO;
}
however if I were to remove the parameters from all this code, then there is no error. Therefore it seems as if I have mistake in the way I am using parameters.
The selector you pass to scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: is akin to the name of a function. What you’re trying to do is to pass in the name of a function and the value of its parameters, but that won’t work. The method you name using #selector(...) will be passed exactly one argument: the NSTimer object calling the method.
If you have data you need to make available to ShowLabel it needs to be attached to the timer object or to self or something like that. To attach your number to the timer object you could do this:
- (void) flash_random_winning_number:(NSInteger) param_which_magic_number
{
NSInteger dd = 9;
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:.5
target:self
selector:#selector(ShowLabel:)
userInfo:#(dd)
repeats:YES];
}
- (void) ShowLabel:(NSTimer *)timer
{
NSInteger dd = [[timer userInfo] integerValue];
/* do stuff with dd */
}
(Since you’re new to Objective-C I want to mention the conventions that have arisen about method naming. The usual style, which you should always use, is for methods to be named with camelCasedNamesLikeThis. In particular, flash_random_winning_number and ShowLabel are both likely to confuse other Objective-C developers because they don’t “look like” method names. ShowLabel22 is also weird because of the numbers, but since you included that in one place in your question but not another I guess that’s just a typo.)
Look at this SO answer. When you have a selector with arguments you have to use this method:
[self performSelector:#selector(myTest:) withObject:myString];
Is there any method for slide show of an array of strings in UILabel in iphone app?(ie, strings will appear one by one). Anyone please help me.
Look at NSTimer.
This can get you a repeated callback every X seconds. You can use this to swap the strings in an array.
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(swapStrings:) userInfo:nil repeats:YES];
_myStrings = #[ #"a" , #"b", ..... ];
...
- (void) swapStirings:(id)sender
{
myInt ++;
if (myInt >= [_myStrings count]) myInt = 0;
myLabel.text = [_myStrings objectAtIndex:myInt];
}
No, there is not. But, the good news it should be trivial to implement. I suggest creating a subclass of uilabel that has an array of strings as an ivar. The subclass has an init method that sets the array and a time interval. Then it uses an nstimer to change its label and redisplay itself. Good luck!
I want a timer to stop at a user-defined input from a text field, then reset and repeat until it reaches a user-defined number of repetitions (another text field). This worked well when I was using sliders, but I was having some other problems with sliders so I changed to textfields. I think the problem is converting the text field into a numeric value, which I have tried many different ways without success. The app is a simple exercise repetition counter with a hold time. Any assistance is greatly appreciated. Thank you...
-(void)countup {
float floatHoldTime=[holdTimeText.text floatValue];
float floatReps=[repsText.text floatValue];
CountHold ++;
displayHoldTime.text = [NSString stringWithFormat:#"%i", CountHold];
if (CountHold == floatHoldTime) {
CountRep ++;
displayReps.text = [NSString stringWithFormat:#"%i", CountRep];
CountHold = 0;
}
if (CountRep == floatReps) {
[Timer invalidate];
Timer = nil;
}
}
-(IBAction)start:(id)sender {
Timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(countup) userInfo:nil repeats:YES];
}
I suspect that the problem is that your float and what I presume is an int (CountHold) are never quite equal due to the way that floats are represented inside a computer. Either make floatHoldTime and floatReps integers, or make you comparison something like CountHold - floatHoldTime < 0.01 to allow for a little slop.