UIImageView center not updating properly - ios

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.

Related

Changing overlaying labels text on a ScrollView

This is sort of hard to explain so I'm including a picture.
So I have a scrollview (the yellow part) and then an overlaying box with score and how many upgrades the player purchased, etc. The green box with the labels stays in the same position while the yellow part scrolls. That works fine, except I am trying to get the labels to change when the player purchases an item. So lets say the player purchases a pickaxe. The upgrades owned should change from "0/20" to "1/20".
This is my setup. I am using SpriteBuilder by the way. I have a class that handles the scrollview, like the buy buttons and descriptions, etc... Then I have another class just for the overlay on the side, and this is basically the only method in it:
- (void)didLoadFromCCB {
if (_doge < 1000000000000) {
balanceLabel.string = [NSString stringWithFormat:#"%.2Lf", _doge];
} else {
balanceLabel.string = #"A lot!";
}
upOwnedLabel.string = [NSString stringWithFormat:#"%d", _upOwned];
upMaxedLabel.string = [NSString stringWithFormat:#"%d", _upMaxed];
NSLog([NSString stringWithFormat:#"%d", _upOwned]);
}
I've tried a few things and this is the one that I think is "most correct". In the didLoadFromCCB on the scroll class, I have this:
UpgradesScene *upgradesScene = [[UpgradesScene alloc] init];
[upgradesScene didLoadFromCCB];
It's in the correct place (under buy button) but for some reason it doesn't update the label. I do get an NSLog message telling me the level, but for some reason the label doesn't work.
I'm very new to this language so please go easy on me :) Thank you
The problem is that you're creating a new upgradesScene with alloc init rather than getting a reference to the one that's in your scroll view. You should have a property (or IBOutlet if you're making this in IB) that points to that view.

UIPickerView Random Spin UILabel 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.

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";
}

cocos2d: why isn't label appearing?

Hello I am making a side scrolling cocos2d game and I want a label to show how far the user has flown in the game. For some reason with the code I wrote the label is not appearing. Here is my GameEngine class that calls the class method that is supposed to make the label appear:
//Set the meterDistance
meterDistance = [MeterDistance createTheMeterDistance];
[self addChild:meterDistance z:10];
Here is the code in the MeterDistance class:
meters = 1;
meterLabel = [CCLabelBMFont labelWithString:#"0" fntFile:#"green_arcade-ipad.fnt"];
meterLabel.position = ccp(200, screenHeight - 100);
[self addChild:meterLabel z:10];
meterLabel.anchorPoint = ccp(1.0, 0.5);
[self schedule:#selector(updateLabel:)interval:1.0f/20.0f];
Here is the updateLabel method:
-(void)updateLabel:(ccTime)delta{
meters++;
NSString* scoreString = [NSString stringWithFormat:#"%d", meters];
[meterLabel setString:scoreString];
}
It's been a while since I last dealt with cocos2d code...
What you've written looks ok.
Take it one step at a time and see where it goes wrong.
Position your label in the middle of the screen (maybe screenheight is off, or anchorPoint moves the label outside of the screen).
Another possible cause is if the font file name is not exactly #"green_arcade-ipad.fnt".
Maybe you missed a capital letter?
Otherwise maybe some other element of your layer could be obstructing the label.

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.

Resources