UIPickerView Random Spin UILabel iOS - ios

I have my UIPickerWheel setup to spin by a flick and populate a UILabel with whatever word value it lands on and that works just fine.
This issue that I'm having is when I added a button that spins my picker wheel and randomly chooses a word value. It spins the wheel just fine, and it populates my UILabel as well, but the UILabel and what is showing on the picker wheel doesn't match. I'm going to paste the code below.
I know that the way I have it setup right now is basically saying when user presses the button, spin the wheel and select a random value, and set the Text to label for the selectedItem. Right now the selectedItem = ? is where I'm having the issue. if I enter a string (e.g. #"hello") the label will say "hello" no matter what is lands on and if I type a number value of 0-5610 (the words on my array) then it will show that word.
Right now I have it just randomly choose a word. So I know that issue is on that line.
The line where im assuming my issue is coming from would be:
selectedItem =
/// HERE'S THE CODE///
- (IBAction)spin:(id)sender {
[pickerView selectRow:(arc4random() % [self pickerView:pickerView numberOfRowsInComponent:0]) inComponent:0 animated:YES];
selectedItem = [words objectAtIndex:(arc4random() % [words count])];
[label setText:selectedItem];
}

You are creating two different random numbers. You set the picker with one and choose a word from the word list with the other. Instead, create one random number. Use it for both operations:
- (IBAction)spin:(id)sender {
NSUInteger number = arc4random_uniform(words.count);
[pickerView selectRow:number inComponent:0 animated:YES];
selectedItem = words[number];
label.text = selectedItem;
}
This assumes the words in the picker match the words in the words array.

Related

Changing the text of a label on button press with dynamic integer

I'm following a tutorial to create a simple game where you tap a button and the the game counts how many times you have pressed the button as you go along. The score is displayed on screen in the form of a label.
I can make it so that when you press the button the label text changes to say 'Pressed!'. But then i can not get the label to change when i try to add the changing score with a format specifier.
-(IBAction)buttonPressed{
count ++;
//scoreLabel.text =#"pressed";
scoreLable.text = [NSString stringWithFormat:#"Score\n%i", count];
}
Any help would be greatly appreciated. Thanks!
You may not have your label set up for multiple lines of text. Try getting rid of the "\n" in your format string. Other than that, you need to tell us what happens. Have you put a breakpoint to make sure your button IBAction is being called? Have you checked to make sure "scoreLable" (sic) is not nil? The devil is in the details.
-(IBAction)buttonPressed : (id) sender{
UIButton * btnPressed = (UIButton *)sender;
int buttonValue = [[btnPressed titleForState:UIControlStateNormal] intValue];
NSLog(#"Button value = %d", buttonValue);
//scoreLabel.text =#"pressed";
scoreLable.text = [NSString stringWithFormat:#"Score\n%i", count];
}
First You Can Set static int count = 0;

UIImageView center not updating properly

I'm practicing some iOS programming. I'm looking to move a UIImageView object to a given position on the screen. The storyboard contains
-Three textboxes to input X Y and Z components (xbox,ybox,zbox)
-Three labels to display said component values (XLabel,YLabel,ZLabel)
-UIImageView containing an image (ball)
-Go button (calls buttonPressed when pressed)
The following code successfully moves the image after inputing values into the textboxes n clicking "GO". However, when I uncomment the code to update the labels, the image does not move until I click "GO" twice in a row. The first time, the labels updates. The second time, the image updates. Why does it require two clicks when I have the label update code uncommented?
Thanks,
Rob
- (IBAction)buttonPressed {
[self graph: [xbox.text intValue] :[ybox.text intValue] :[zbox.text intValue]];
/*
XLabel.text = [NSString stringWithFormat:#"X:%#", xbox.text];
YLabel.text = [NSString stringWithFormat:#"Y:%#", ybox.text];
ZLabel.text = [NSString stringWithFormat:#"Z:%#", zbox.text];
*/
}
...
-(void)graph:(int)xpos : (int)ypos : (int)zpos{
ball.center = CGPointMake(xpos,ypos);
}
I'm going to provide this as an answer. Turn off auto-layout.

Data from DetailViewController not displaying

I'm making a chemistry calculator segment in an app I'm working on, and I cannot get the data and I cannot get the information to correctly populate the screen. There is a secondary issue, the alignment, and if someone can help me with that I'd greatly appreciate it, but it isn't the primary focus of this question - I'll make a dedicated topic for it.
So I'll go over what I want to do, and what I've tried. What I'm trying to do is make a chemistry calculator where depending on what equation is selected, a UIStepper max/min.Value is modified to include all possible derivations of that equation, as well as certain UILabels and UITextFields shown/hidden.
I have confirmed that I have data passed down from the MasterViewController as I've set the data to an NSString called _equation, and successfully used _equation to modify the title of the DetailViewController under self.title in viewDidLoad.
I have tried placing and initializing all UIStepper properties appropriately under a if/if else nest under viewDidLoad (which also quantizes the _equationName possible values to an integer (eqNum) so that it can be used in a switch statement). I have also tried placing the UITextField hidden properties under viewDidLoad, to no avail.
So without further ado, let's get to the code. I've truncated the code down to one equation so you can see what's going on here easier - note that this is nested under the IBAction for the Calculate button:
// Take _equationName quantization and use it in a switch case to determine the formula that IBAction will use:
if (dflt)
{
switch (eqNum)
{
case 1:
if ((stepper.value = 1))
{
// Change deriv_units appropriately:
deriv_units.text = #"Energy (Joules)";
// This is a Planck's constant calculation, we hide the second variable as the constant
// is stored:
value2.hidden = YES;
value2_type.hidden = YES;
// Now we set up the parameters of the first entry variable:
value1_type.text = #"Frequency (in Hz)";
double frequency = [value1.text doubleValue];
double Planck = 6.626069e-34;
double energy = Planck * frequency;
// Now we set up the return field to return results:
NSString* resultIntermediate = [NSString stringWithFormat:#"%f", energy];
result.text = resultIntermediate;
units.text = #"J";
}
and the subsequent code under viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
self.title = _equationName;
int eqNum;
if ((_equationName = #"Energy-Frequency Relation"))
{
eqNum = 1;
// Set stepper bounds for derivation:
[stepper setMinimumValue:1];
[stepper setMaximumValue:3];
self.stepper.stepValue = 1;
self.stepper.wraps = NO;
self.stepper.autorepeat = NO;
self.stepper.continuous = YES;
// This is primarily a two-variable equation, so hide UITextView and UILabel #3:
value3.hidden = YES;
value3_type.hidden = YES;
}
(Props to anyone who recognizes this - it's Planck's relation! :D)
Here is what the GUI is supposed to look like (as it appears in Storyboard):
Here is what it comes out looking like in the iOS Simulator:
Note the misalignment issue, which isn't the principle issue in play here.
Also note that right now, the switch statement for equation parameters is under an if tree that checks to see if dflt (a Boolean variable assigned to UISwitch) returns true for double-precision calculations. However, upon toggling the switch ON, the issue does not correct.
Here's an even more complete explanation:
value#.text is the number entered in one of the three UITextFields, from top to bottom.
value#_type is the text to be displayed in the corresponding UILabel.
deriv_units is the UILabel below the one marked "Derivation Units", and is used to display which derivation of the equation has been selected using the UIStepper.
At bottom: The rightmost UILabel is the result label, whereas the leftmost is the units label.
Many thanks to anyone who can help this beginning developer along the path of programming righteousness.
About your alignment issue: it looks as though you are creating the storyboard for 4" screen, while running it on a 3.5" screen. In the storyboard onnthe lower right there are some small buttons, one of thise allows you to change instantly between the display for either 4" or 3.5".
Have you made sure your controls are connected to your code?
- (void) viewDidAppear: (BOOL) animated{
[super viewDidAppear: animated];
// some rude NSAsserts, but let's be sure
NSAssert(value1_type != nil, #"Your control element is nil, it might not be hooked up");
// you should now see this text in the textfield
value1_type.text = #"Frequency (in Hz)";
NSAssert(result != nil, #"Your control element is nil, it might not be hooked up");
result.text = #"some test text";
NSAssert(units != nil, #"Your control element is nil, it might not be hooked up");
units.text = #"J";
}

Continuously Update Label Based on UISlider Value

I currently have a series of 5 UISliders that continuously update their respective labels to display what value the user is selecting in an hh:mm:ss format. I have the properties of each slider set to continuous and have their targets set to when the slider is changed (This example would be for just the first slider).
sliderOne.continuous=YES;
[sliderOne addTarget:self action:#selector(sliderOneChanged:) forControlEvents:UIControlEventTouchUpInside];
Each slider's individual label updates the time selected perfectly. Now however, I need to update another label underneath all of these sliders that continuously sums up the value of the sliders.
At the same time, I am also doing some conversions with the value of the slider using two segmented controllers -- a miles/kilometers segmented controller and various distances in another segmented controller. Each conversion results in a time in seconds that is then converted back into the hh:mm:ss format and displayed in the last label when the user selects which combo from the segment controller they want. I have all the math worked out for this, and the label will update correctly when changing segments, but not continuously.
How could I update a label to continuously show the sum of every UISlider as the user changes them?
For a continuous updating, use the UIControlEventValueChanged event along with the continuous property
sliderOne.continuous=YES;
[sliderOne addTarget:self action:#selector(sliderOneChanged:) forControlEvents:UIControlEventValueChanged];
Use KVO to get notified of changes in UISliders and update the UI accordingly. You want to wrap UI updates with dispatch_async to avoid locking the interface.
Using KVO-Notification-Manager it can be something like this (in viewDidLoad or similar):
id token1 = [slider1 addKVOBlockForKeyPath:#"value" options:NSKeyValueObservingOptionNew handler:^(NSString *keyPath, id object, NSDictionary *change) {
dispatch_async(dispatch_get_main_queue(), ^{
whateverSliderNeedsUpdate.value = /* your calculation result */;
});
}];
id token2 = [slider2 /* repeat for as many sliders as you want */];
...
It's important to remove the observers at some point.
- (void)dealloc {
[slider1 removeKVOBlockForToken:token1];
[slider2 ...];
}
If you feel adventurous you can try with ReactiveCocoa.
You could create a method that does all of your math and updates your UILabel with the result. For example:
- (void)updateSumLabel {
// this is obviously very simple
float num1 = [_sliderOne value];
float num2 = [_sliderTwo value];
float sum = num1 + num2;
[_sumLabel setText:[NSString stringWithFormat:#"#f",sum]];
}
The you can call updateSumLabel from any of your existing methods like sliderOneChanged. This is probably the most simple way todo this.

write with my buttons

I've made my own keyboard with some buttons made A-Z button and gave them Titles A button has title A, B button has title B and so on.
I added a label so when I click a button it would display the title of that button and it does.
When I click A it displays A, when I click B it displays B. The problem is that I want them to display next to each other but I can't get it work. like when I press ABC it would display ABC not first A then replace it with B and then replace it with C, because that is what it does now.
-(IBAction) clicked: (id)sender{
NSString *titleOfButton = [sender titleForState:UIControlStateNormal];
NSString *newLabelText = [[NSString alloc] initWithFormat:#"%#", titleOfButton];
labelsText.text = newLabelText;
}
Here is some code from a calculator where it does work but I can't find the right way to implant it with my code
//-(IBAction)buttonDigitPressed:(id)sender {
//currentNumber = currentNumber*10 + (float)[sender tag];
//calculatorScreen.text = [NSString stringWithFormat:#"%2f",currentNumber];
//}
is there any1 who can tell me how to get this working or knows an entire other way to get it working i've been looking and trying for almost a full week now thanks
This is the line that is causing you problems:
labelsText.text = newLabelText;
You are setting the label text to be ONLY newLabelText. What you want to do is concatenate the strings, which will combine the old text with the new text. This one change should do it:
labelsText.text = [NSString stringWithFormat:#"%#%#", labelsText.text, newLabelText]

Resources