UISlider value changed update result - ios

I have three labels receiving output from three UISliders. A result is calculated and placed in a fourth label when a segmented control is touched.
How do I make the result update if one of the slider values is changed without touching the segmented control again?

First, add an event for your slider changing values:
[mySlider addTarget:self
action:#selector(sliderValueChanged:)
forControlEvents:UIControlEventValueChanged];
You will want to do this for each slider.
In your sliderValueChanged: function:
- (void)sliderValueChanged:(id)sender {
[self calculateResult];
}
In calculateResult you can do your calculations and set your result box.
If you need to make sure your segmented control is in some state first, just add an if clause to sliderValueChanged:
- (void)sliderValueChanged:(id)sender {
if ([mySegmentedControl selectedSegmentIndex] == 1) {
[self calculateResult];
}
}

I came up with something that worked using mopsled's answer:
if (UIControlEventValueChanged && mySeg.selectedSegmentIndex == 0) {
float floatResult = [f3.text floatValue]-[f2.text floatValue];
float coldFactor = [fcold.text floatValue]*floatResult;
float result = [f1.text floatValue]+coldFactor;
NSString *tpResult = [[NSString alloc] initWithFormat:#"%.0f",result];
f4Result.text = tpResult;
}

Related

How can I take the current integer value out of my array loop when I press my button on Xcode?

I need to take the current value of mtype and pass it forward to Mselect so that the image pushed forward is the same as the one rotating in the animation.
here is the code sample
- (void)viewDidLoad
{
//Array to hold Images
NSMutableArray *imageArray = [[NSMutableArray alloc] initWithCapacity:Mush_Count];
for (mtype = 1; mtype < Mush_Count; mtype++)
{
[imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:#"Mush%i.png", mtype]]];
//make button
SelectBt.imageView.animationImages = [NSArray arrayWithArray:imageArray];
SelectBt.imageView.animationDuration = 5;
[SelectBt.imageView startAnimating];
Mselect = mtype;
}
}
-(IBAction)Selection:(id)sender{
[self Placement];
}
-(void)Placement{
if (Place1Occ == NO) {
[self Place1];
}
}
-(void)Place1{
if (Place1Occ == NO) {
Place1.image =[UIImage imageNamed:[NSString stringWithFormat:#"Mush%i.png", Mselect]];
Place1Occ = YES;
}
}
The animation loops just fine and the selection of the images works but it's not selecting the image the is currently on the screen it selects the last image on the Array.
Any suggestions?
for (mtype = 1; mtype < Mush_Count; mtype++)
{
[imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:#"Mush%i.png", mtype]]];
}
That loop is enough to build the array of images - you don't need to set the imageView's animation images until this loop completes and the array is populated.
As it stands, you reset the animation images each loop and set Mselect to type each iteration: this is why you always get the last image index stored in Mselect
This code should be after the for loop:
SelectBt.imageView.animationImages = [NSArray arrayWithArray:imageArray];
SelectBt.imageView.animationDuration = 5;
[SelectBt.imageView startAnimating];
As far as I know, you can't get the current frame of the animation directly - you have to create a timer when you start the animation and have this count the number of frames (by increasing your Mselect value. Then use that to load the current image
Is SelectBT a subclass of UIButton? if so is it the (id)sender coming from your IBAction?
If so then you can just grab the image directly from the SelectBT instance by calling sender.imageView.image
As an aside, objective-C convention is to capitalize ClassNames, but start instances with lowerCase like this ClassName *instanceOfClassName and you're likely to take flack for it around here if you don't adhere to that convention
Update
Could you bump your imageArray into a class variable or a property? It already contains all the fully loaded UIImages, which you can get back out by calling [imageArray objectAtIndex:i]
Then you just have to keep track of which numeric index is facing your user, or which index they tapped and pull the corresponding image.
Feel free to post your whole class if you'd like us to explain more.

UIStepper iOS 7

I am new in programming, let's say I am still swimming in the ocean. I am trying to set up an UIStepper for change a value on a text field. This value has to change using the decimal number (step value = 0.1), I have read many documents but still not sure from where need to start.
The following is the code I am using:
- (IBAction)valueChanged:(UIStepper *)sender {
sender.stepValue = 0.1;
float value = [sender value];
self.txtRspeed.text = [NSString stringWithFormat:#"%.0f", value];
seems like I get the step Value that I want 0.1 but doesn't show up in the simulator. I also tried setting as initial value the one in the text field with some code but I still encounter errors.
What I am doing wrong?
- (IBAction)valueChanged:(UIStepper *)sender {
_value = txtRspeed.text.floatValue;
sender.value = self.stepper.stepValue + _value;
self.txtRspeed.text = [NSString stringWithFormat:#"%g", sender.value];
}
(void)viewDidLoad{
[super viewDidLoad];
self.stepper.value = 0.1f;
self.stepper.minimumValue = -100.0f;
self.stepper.maximumValue = 100.0f;
self.stepper.stepValue = 0.1f;
}
I am getting the increment but no the decrement.
If a set the stepper value in a viewDidLoad it start from 0
So create the UIStepper and set its stepValue property to 0.1. You can get the current value of the control using it's value property.
You'll probably want to create the stepper in your storyboard or .xib file and connect it to an IBOutlet in a view controller so that your view controller can access it easily.

Strange UItextView behavior setting range within textViewDidChangeSelection

I am getting a strange behavior setting the selectedRange property for a textView within the textViewDidChangeSelection delegate.
My code in viewDidLoad is:
hiddenTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 500, 500)];
//[hiddenTextView setHidden:YES];
_hiddenTextViewText=#"ulrd";
hiddenTextView.text = _hiddenTextViewText;
hiddenTextView.delegate = self;
_hiddenTextViewDefaultRange = NSMakeRange(2,0);
hiddenTextView.selectedRange = _hiddenTextViewDefaultRange; //horizontal and vertical center of the textview
[self.view addSubview:hiddenTextView];
[hiddenTextView becomeFirstResponder];
if (_keyboardShown)
[hiddenTextView resignFirstResponder];
I define the textViewDidChangeSelection as follows:
- (void)textViewDidChangeSelection:(UITextView *)textView {
NSLog(#"%lu",(unsigned long)textView.selectedRange.location);
if (textView.selectedRange.location != _hiddenTextViewDefaultRange.location)
{
hiddenTextView.selectedRange = _hiddenTextViewDefaultRange;
}
}
I set a 4 character text and put the selection index in position 2 (middle). The result is that if I press up arrow on the keyboard in simulator NSLog outputs 0 (start of text) and then 2 (reseting the position) which is correct. If I press up again it does the same thing so still correct. Problem is that if I hit up x times I have to hit down equal times before I am able to go to the end of the text (position 4).
I tried resetting the position with a UIButton instead of doing it programmatically and there it works fine. Any ideas?
I managed to overcome the problem. Instead of:
hiddenTextView.selectedRange = _hiddenTextViewDefaultRange;
I used:
dispatch_async(dispatch_get_main_queue(), ^{
textView.selectedRange = hiddenTextViewDefaultRange;
});
...to execute the command async. It worked, however I am not sure why it needed to be like that.

iOS: Moving a uibutton by its tag outside of where it was generated

Is it possible? I have an array of buttons created in an void and I want to move one when a neighbouring one is called in the buttonTapped void. I have no trouble moving the button thats pressed because it is the sender, but I also need to move the one next to it and can't seem to get it to move. Each button in the array has a tag value so they're unique.
Thanks
Reasonable intuitive assumption: you generated the buttons so that they are ordered in the array...
- (void)moveButton:(UIButton *)sender // whatever
{
NSUInteger idx = [buttonArray indexOfObject:sender] + 1;
UIButton *nextButton = idx < buttonArray.count ? [buttonArray objectAtIndex:idx] : nil;
// do something with `nextButton`
}

Modify UIStepper autorepeat behaviour

I have a client who wants me to modify a UIStepper's autorepeat behaviour.
If the user selects touches and holds say the + button of the stepper, the value of the stepper starts increasing at a rate of 1 every half second or so, and then after about 3 seconds, the values start changing much more rapidly.
Is there anyway to modify how this works, so that, for example, the values will increase at the faster rate immediately if the user taps and holds?
I've looked at the UIStepped documentation and I didn't see anything regarding this, but I wondered if there was a way to do this through an IBAction or something.
First, add two actions for your stepper:
[theStepper addTarget:self action:#selector(stepperTapped:) forControlEvents:UIControlEventTouchDown];
[theStepper addTarget:self action:#selector(stepperValueChanged:) forControlEvents:UIControlEventValueChanged];
Here's what those actions look like:
- (IBAction)stepperTapped:(id)sender {
self.myStepper.stepValue = 1;
self.myStartTime = CFAbsoluteTimeGetCurrent();
}
- (IBAction)stepperValueChanged:(id)sender {
self.myStepper.stepValue = [self stepValueForTimeSince:self.myStepperStartTime];
// handle the value change here
}
Here's the magic code:
- (double)stepValueForTimeSince:(CFAbsoluteTime)aStartTime {
double theStepValue = 1;
CFAbsoluteTime theElapsedTime = CFAbsoluteTimeGetCurrent() - aStartTime;
if (theElapsedTime > 6.0) {
theStepValue = 1000;
} else if (theElapsedTime > 4.0) {
theStepValue = 100;
} else if (theElapsedTime > 2.0) {
theStepValue = 10;
}
return theStepValue;
}
It seems that it is not possible to modify this behaviour, and using custom UIButtons is the best solution.

Resources