UIView hidden is not getting set - ios

I have a UIView subclass that is added as an arranged subview of a UIStackView. Depending on the data in the model, I want to either hide or show the arranged subview (called myView), but the problem is that when I go to hide it, even if I set myView.hidden = NO, it still shows that myView.hidden = YES.
For example, the following is the code that I have. It starts out with the view being hidden and depending on whether or not myModel.someProperty is set, it will show myView. Or that is what is supposed to happen.
I have set a breakpoint and stepped through this code and used LLDB to verify that self.myView.hidden == YES before line 4 is executed. I then checked the value right after stepping over line 4 and it was still YES. But line 4 explicitly sets it to NO and nothing in the implementation of myView overrides or even sets or checks the hidden property of itself. So setting hidden on this just goes to the standard UIView setHidden: method. So how could it still be YES?
1. //currently, self.myView.hidden is YES
2.
3. if (self->_myModel.someProperty) {
4. self.myView.hidden = NO;
5.
6. //for some reason, self.myView.hidden is still YES
7.
8. while (self.myView.isHidden) {
9. NSLog(#"myView is hidden, but it should not be");
10. self.myView.hidden = NO;
11. }
12. NSLog(#"myView is no longer hidden");
13. }
I added a loop on line 8 that will cause the view to be hidden again. It works this time. So if I set myView.hidden = NO two times, then it actually will get set to NO. But if I only set it one time, then it stays at YES. I do not understand what is going on.
Does anyone know what might be wrong here or how to troubleshoot this further? I have used LLDB's po command to view the value of myView.isHidden before and after each set of the property. So before line 4, it was set to YES, which is correct. Then, after line 4, I checked it and it was still set to YES, even though it was explicitly set to NO on the previous line. Then, I checked and it entered the loop on line 8 (even though it should not have if it would have been non-hidden like it should have been). And then I checked again before line 10 and myView.hidden was still YES and I checked after line 10 and it was finally correctly set to NO.
But I am just not sure what is going on. This is very counterintuitive as I am explicitly setting it to NO, but it is not getting set until I set it twice to NO.
Is there a good way to troubleshoot this or to figure out what is wrong or does anyone have any suggestions on what might be the problem?
Update
I have updated the code to add some extra log statements. I have also used p self.myView.hidden when checking that property in LLDB.
1. // at this point, self.myView.hidden = YES
2.
3. if (self->_myModel.someProperty) {
4. NSLog(#"Before setting hidden=NO: %#", self->_myView);
5. self.myView.hidden = NO;
6. NSLog(#"After setting hidden=NO: %#", self->_myView);
7.
8. while ([self.myView isHidden]) {
9. NSLog(#"SHOULD NOT BE HERE - Before setting hidden=NO again: %#", self->_myView);
10. self.myView.hidden = NO;
11. NSLog(#"SHOULD NOT BE HERE - After setting hidden=NO again: %#", self->_myView);
12. }
13.
14. NSLog(#"Finally, no longer hidden: %#", self->_myView);
15. }
Here are the log statements from this code. The first log statement is correct, as it shows myView.hidden == YES. The second log statement, however, seems wrong to me because it is still showing myView.hidden == YES even though on the previous line it was just set to NO.
Before setting hidden=NO: <MyView: 0x117ef6eb0; frame = (0 49.6667; 123.667 20.3333); hidden = YES; layer = <CALayer: 0x280ddaa20>>
After setting hidden=NO: <MyView: 0x117ef6eb0; frame = (0 49.6667; 123.667 20.3333); hidden = YES; layer = <CALayer: 0x280ddaa20>>
The next set of log statements are inside the loop, which it should not even enter anyway since I am setting myView.hidden to NO, but it goes in anyway because the value is still YES. And here it looks like it works correctly. The first log statement shows it is visible and then the next log statement shows it is hidden.
SHOULD NOT BE HERE - Before setting hidden=NO again: <MyView: 0x117ef6eb0; frame = (0 49.6667; 123.667 20.3333); hidden = YES; layer = <CALayer: 0x280ddaa20>>
SHOULD NOT BE HERE - After setting hidden=NO again: <MyView: 0x117ef6eb0; frame = (0 49.6667; 123.667 20.3333); layer = <CALayer: 0x280ddaa20>>
Finally, no longer hidden: <MyView: 0x117ef6eb0; frame = (0 49.6667; 123.667 20.3333); layer = <CALayer: 0x280ddaa20>>
Update 2
I know this code seems to be working on its own, but it is not working for me in my project. I will show the code for my view class here and also the output from a debug session showing the same behavior observed in the code.
And I know it might be in my code, but at the same time, I just do not see how. All my code consists of here is a call to setHidden:. Nothing extra. Before calling setHidden, the value of hidden is YES. After calling setHidden:NO, the value is still YES. I do not understand that. I am wondering if this is maybe a compiler issue. I know these compilers are very well tested, but at the same time I also do not understand how it is my code. I am simply setting hidden = NO, but it is not working unless I do it twice.
Debug Session
Here is the output from LLDB. I set a breakpoint right before the view was about to be unhidden (line 3 in the previous code snippets). At this point, myView.hidden = YES.
So all I did was to print the value of hidden for that view, and it correctly showed YES. After this, I ran call self.myView.hidden = NO to try to update it, but that doesn't work as can be seen in the debug statement that is printed out right below the call statement. It still shows hidden = YES;. I also went ahead and printed the value again just to be sure, and it still shows hidden = YES.
(lldb) p self.myView.hidden
(BOOL) $12 = YES
(lldb) call self.myView.hidden = NO
<MyView: 0x12b138980; frame = (0 49.6667; 123.667 20.3333); hidden = YES; layer = <CALayer: 0x283addfe0>> MyView::setHidden:NO
(BOOL) $13 = NO
(lldb) p self.myView.hidden
(BOOL) $15 = YES
Next, I just set the value to NO again and this time it works as can be seen by the debug statement and I also printed the value again for good measure.
(lldb) call self.myView.hidden = NO
<MyView: 0x12b138980; frame = (0 49.6667; 123.667 20.3333); layer = <CALayer: 0x283addfe0>> MyView::setHidden:NO
(BOOL) $16 = NO
(lldb) p self.myView.hidden
(BOOL) $17 = NO
Here is the code for my view class that gets shown and hidden. I am not overriding or doing anything with the hidden property, so any call to setHidden: goes straight to the method on UIView.
MyView.h
#import <UIKit/UIKit.h>
#import "MyModel.h"
#interface MyView : UIView
#property (strong, nonatomic, nullable) MyModel *myModel;
#end
MyView.m
#import "MyView.h"
#interface MyView ()
#property (strong, nonatomic) UILabel *label;
//other UI components are here, but they are just more labels and an image view
#end
#implementation MyView
- (instancetype)init {
return [self initWithFrame:CGRectZero];
}
- (instancetype)initWithCoder:(NSCoder *)coder {
if (self = [super initWithCoder:coder]) {
[self initialize];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self initialize];
}
return self;
}
- (void)initialize {
[self addSubview:self.label];
//add other labels and the image view
[NSLayoutConstraint activateConstraints:#[
[self.label.leadingAnchor constraintGreaterThanOrEqualToAnchor:self.leadingAnchor],
[self.label.topAnchor constraintGreaterThanOrEqualToAnchor:self.topAnchor],
[self.label.trailingAnchor constraintEqualToAnchor:self.trailingAnchor],
//more constraints for the other labels and the image
]];
}
- (void)setMyModel:(MyModel *)myModel {
self->_myModel = myModel;
[self updateDisplay];
}
- (void)updateDisplay {
//set the text of all the labels based on the model
}
- (UILabel *)label {
if (!self->_label) {
self->_label = [[UILabel alloc] init];
self->_label.translatesAutoresizingMaskIntoConstraints = NO;
self->_label.numberOfLines = 0;
self->_label.text = #"My Text:";
[self->_label setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
[self->_label setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
}
return self->_label;
}
#end
Please let me know if there is anything else that I should post that would help or if there is anything I could try. I can just write the value twice in my code, but without understanding why I have to do it, I feel that is sort of dangerous because how do I know that two times will always be sufficient? Plus, it is just weird to have to set a variable to the same value twice in a row for it to work.
Thank you to everyone for your help with this.

Yes, there is a bug / quirk when animating the showing / hiding of arranged subviews in a UIStackView.
You should be able to correct the issue by adding this to your custom view class:
- (void)setHidden:(BOOL)hidden {
if (self.isHidden != hidden) {
[super setHidden:hidden];
}
}
Here is a complete example that shows the problem, and shows the "fix": https://github.com/DonMag/StackViewBug

It looks like this is due to a bug in the UIStackView where if you hide a view more than once, it accumulates the hidden count for that view. So, for example, imagine a view hierarchy like this:
UIStackView
MyView
Then, if you set MyView hidden=YES three times, it will take three times of setting hidden=NO to allow it to actually be set to NO. This does not appear to be an issue going the other way. So if you set it hidden=NO three times, you can set it to hidden=YES just once and it will be hidden.
There is more information in this StackOverflow answer:
https://stackoverflow.com/a/45599835/5140550
I am not sure if this bug has been reported to Apple or not, but it appears to be a bug in UIStackView. Now I just need to figure out a clean way to handle this issue in my code.

Related

programmatically added uiview does not displayed

I'm trying to create a uiview programmatically and adding it to a stack_view which was also programmatically created and added to the view.
This is the code:
the .h
#interface ViewController : UIViewController
#property (strong, nonatomic, nullable) UIStackView * stack;
#end
the .m
#implementation viewController
#synthesize stack;
- (void) viewDidLoad {
[super viewDidLoad];
CGRect * vframe = self.view.frame;
// - Option 1
stack = [[UIStackView alloc] initWithFrame:CGRectMake(vframe.origin.x, 100,vframe.size.width,300)];
stack.axis = UILayoutConstraintAxisHorizontal;
stack.aligment = UIStackViewAligmentTop;
stack.distribution = UIStackViewDistributionFill;
[self.vew addSubview:stack];
// Option 2
// stack.traslateAutorezisingMaskIntoConstraints = NO;
// [stack.leadingAnchor constraintsEqualToAnchor: self.view.leadingAnchor].active = YES;
// [stack.topAnchor constraintsEqualToAnchor: self.view.topAnchor constant: 100].active = YES;
UIView * pView = [[UIView alloc] init];
pView.backgroundColor = [UIColor blueColor];
[stack addArrangedSubview:pView];
}
#end
This code does not show the view at all, I've tried to prove the option 2 (appeared commented in the code) and it does not work either. It is not supposed that the view, upon inserted in the stack, will get the size of the stack, since the distribution of it is "Fill"?. None of this work either even if I define Pview with frame=CGRectMakeRect(0,0,self.view.frame.size.width,100), for instance.
What am I doing wrong?
EDIT: I already fix the misspelled in the code (the * in the CGRect and the self.vew instead of self.view). I made these mistakes when I was manually copying the code, I did not copy and paste the code; tha's is why is made them and that's why the original code compile well
To diagnose this first test the pView. Init it with a fixed size like CGRectMake(100,100,100,100) and add it directly to the viewContoller's view. You should be able to see it no problem if not you have a deeper issue.
If that goes well try the UIStackView with a fixed size. Color its background to see it better if you need. If you still dont see it then double check it still has the correct frame in viewDidAppear of the viewController. It might have adjusted itself after creation. If that is correct go to Debug -> View Debugging -> Capture View Heirachy in Xcode after you have it up in the simulator. If you dont see it there then there was an issue adding it as a subview (note the typo [self.vew addSubview:stack];)
If that goes well then there is a problem with [stack addArrangedSubview:pView]. Similar to the previous step, loop through all the arrangedSubviews in UIStackView in viewDidAppear of its viewController.
CGRect vframe = self.view.frame;
In viewDidLoad, your self.view.frame has not been calculated yet. You need to do this in viewDidLayoutSubviews or viewDidAppear. both of these will get called multiple times so be careful.
EDIT:
As suggested below by danh (I overlooked it) you should remove * from the above line , also, you have several misspelling in your code, don't know how this code really even compiled for you.

Why viewDidLayout only got called once on iOS 7, but several times on iOS 8

I have an project using autolayout,
And I notice that after viewWillAppear, viewWillLayoutSubViews and viewDidLayoutSubViews pair will be called several times on iOS 8, for my case, it is 2-3 times usually.
The fist viewDidLayoutSubViews will get incorrect frame size, so I have to avoid for first viewDidLayoutSubViews, and init my views afterwards.
However, when I tested it on iOS 7, I found that only ONE viewWillLayoutSubViews and viewDidLayoutSubViews pair got called, so my code broke again.
My question is, what is changed on iOS 8 for this behaviour?
EDIT:
I have pasted my demo code here:
In the code, _pieChart will be added to self.ChartViewCanvas, and self.ChartViewCanvas is using autolayout. _pieChart is from old project code, which is drawn without auto layout.
I was required to draw the pie chart before viewDidAppear, because drawing in viewDidAppear will have a 1 sec delay compare to other views in storyboard. This is not allowed for me.
Is there any way to know when is the final viewDidLayoutSubViews? Calling [self.ChartViewCanvas addSubview:_pieChart]; multiple times will lead to lower performance, and sometimes _pieChart's drawInRect will not be called every time, so the chart is not update.
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
_pieChart.delegate = self;
if (!_pieChart) {
_pieChart = [[PieChartView alloc] initWithFrame:CGRectMake(0, 0, pieRadius * 2, pieRadius * 2)];
}else {
[_pieChart setFrame:CGRectMake(0, 0, pieRadius * 2, pieRadius * 2)];
}
//_pieChart.translatesAutoresizingMaskIntoConstraints = NO;
if ([_pieChart superview]) {
[_pieChart removeFromSuperview];
}
[self.ChartViewCanvas addSubview:_pieChart];
}
Probably only Apple knows, but I won't deal with that too much if everything is working fine. In iOS8 Apple changed a lot view controllers (again) in they way they are presented from containers VC as for rotation and UITraitCollections.
For instance UIAlertView is now a view controller, when you show one you trigger all the mechanism related to present a VC.
If this fact is creating an issue it must be said that you should not rely on how many times those methods are called because they were always be unpredictable there are too many variables to be taken into account.
A quick and dirty solution could be wrap your code in a dispatch_once if you want that it will be called only one time.
If you add your view using auto layout correctly you won't see any sort of bug.
[EDIT]
Here is a little snippet about how it might look your viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
//.. your stuff
//We don't need any frame autolayout wil take care of calculating it on its pass
_pieChart = [[PieChartView alloc]initWithFrame:CGRectZero];
_pieChart.delegate = self;
_pieChart.translatesAutoresizingMaskIntoConstraints = NO;
[self.ChartViewCanvas addSubview:_pieChart];
NSDictionary *bindings = NSDictionaryOfVariableBindings(_pieChart);
// We create constraints to tell the view that it needs to sctretch its bounds to the superview
NSString *formatTemplate = #"%#:|[_pieChart]|";
for (NSString * axis in #[#"H",#"V"]) {
NSString * format = [NSString stringWithFormat:formatTemplate,axis];
NSArray * constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:bindings];
[_pieChart.superview addConstraints:constraints];
}
// Do any additional setup after loading the view.
}
Of course that is going to call drawRect:, draw rect is called when a view is marked as dirty in the display pass, but before display is usually called the autolayout engine to calculate frames of views in needs for layout.
I tried this out on my application and found the same as you: 1 call on iOS7 and 3 on iOS8. From the stack traces this seems to be down to doing double layout after viewWillAppear and an extra layout following viewDidAppear not seen on iOS7.
My suggestion would be that you add any views in viewDidLoad (or viewWillAppear), then only do layout adjustments in the layout subview runs. Based on your updated post something like:
- (void)viewDidLoad{
[super viewDidLoad];
_pieChart = [[PieChartView alloc] initWithFrame:CGRectMake(0, 0, pieRadius * 2, pieRadius * 2)];
[self.ChartViewCanvas addSubview:_pieChart];
_pieChart.delegate = self;
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
[_pieChart setFrame:CGRectMake(0, 0, pieRadius * 2, pieRadius * 2)];
}
For interest the difference between iOS7 and 8 calling sequence was:
iOS7
i) viewWillAppear is called.
ii) layout of subviews is called. From the stack this seems to relate to the navigation bar and animation.
ii) viewDidAppear is called.
iOS8
i) viewWillAppear is called.
ii) layout of subviews is called. From the stack this seems to relate to the navigation bar and animation.
iii) exact same layout with exact same stack is called again. So something in the stack must request a rerun from some point.
iv) viewDidAppear is called.
v) An extra layout of subviews is called. This seems driven from a transaction pushed onto the run loop.

UIView gradient does not change on second save

I'm creating a preview from something that people can configure in settings. They can choose a color in settings.
My view hierarchy:
Settings (Choose color for tree, and preview tree)
Choose color for tree (Here the user can choose for colors of the tree, with a save button)
I have an unwindSegue for my save button, that calls the setupCell method. But the background gradient layer of my UIView does not want to change.
Code:
- (IBAction)setupCellSegue:(UIStoryboardSegue *)segue{
[self setupCell];
}
-(void)setupCell{
//Corners
self.Preview.layer.cornerRadius = 25;
self.Preview.layer.masksToBounds = YES;
//Background
self.Preview.backgroundColor = [UIColor clearColor];
CAGradientLayer *grad = [CAGradientLayer layer];
grad.frame = self.Preview.bounds;
grad.colors = [[[Kleuren sharedInstance] kleurenArray] objectForKey:[standardDefaults objectForKey:#"Kleur"]];
[self.Preview.layer insertSublayer:grad atIndex:0];
//Text
self.optionOutlet.textColor = [Kleuren.sharedInstance.tekstKleurenArray objectForKey:[standardDefaults objectForKey:#"TekstKleur"]];
}
EDIT:
Here is the NSLog from my sublayers from the view. How can i replace the old CAGradientLayer with the new one? Tried this:
[self.Preview.layer replaceSublayer:0 with:grad];
(
"<CAGradientLayer: 0xcce9eb0>",
"<CALayer: 0xccef760>"
)
EDIT 2:
After trying some more, i've noticed that the CAGradientLayer is just added and added on top? Maybe this is the problem?
(
"<CAGradientLayer: 0x170220100>",
"<CAGradientLayer: 0x170220e20>",
"<CAGradientLayer: 0x170029f40>",
"<CALayer: 0x17803d960>"
)
EDIT 3:
View hierarchy, so the View that is highlighted in the screenshot is the one with the background.
EDIT 4:
Tried this: but now it doesn't have a CALayer anymore:
[[self.Preview layer] replaceSublayer:[[[self.Preview layer] sublayers] objectAtIndex:0] with:grad];
So I would get the following error:
2014-07-08 18:57:18.365 ///[3324:60b] View hierarchy unprepared for constraint.
Constraint: <NSIBPrototypingLayoutConstraint:0xe88e0b0 'IB auto generated at build time for view with fixed frame' UILabel:0xe88dae0.left == UIView:0xe88da50.left + 20>
Container hierarchy:
<UIView: 0xe88da50; frame = (59 54; 202 202); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0xe88dab0>>
| <CAGradientLayer: 0xe88cd50> (layer)
View not found in container hierarchy: <UILabel: 0xe88dae0; frame = (20 20; 162 162); text = 'Option'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0xe88db90>>
That view's superview: NO SUPERVIEW
So it seems to be that because the CALayer is deleted, the UILabel gives this error because it doesn't have a superview.
Try adding
[self.Preview.layer setNeedsDisplay];
after setupCell. This should redraw the layer
Not immediately sure what your issue is.
The first thing that jumped out at me is that in your first EDIT, you misused replaceSublayer:with:. See the documentation for that method for details, but basically you used an integer literal when you need to pass a CALayer - I'm surprised the compiler didn't complain. The declaration of that method is:
- (void)replaceSublayer:(CALayer *)oldLayer with:(CALayer *)newLayer
To use this method correctly, you'll want to hold onto a reference to the gradient layer. Maybe make a new property called grad and store it there. Later, when you want to replace it, pass self.grad into replaceSublayer:with: as oldLayer.
After you've fixed that, here are some things to check:
Are you sure setupCell gets called when you think it does? Set a breakpoint there to check.
Are you sure that when you recalculate the gradient layer, the colors have changed? Use a breakpoint to examine the value of grad.colors each time you pass through setupCell.
Found it, I've placed my CAGradientLayer *grad; in my implementation declaration between the {}
By placing the declaration in my implementation of my class. I could re-use the declaration, and not making a new instance each time by putting it in the setupCell.
#implementation class {
CAGradientLayer *grad
}
-(void)setupCell{
// Do stuff here with the gradient layer.
}
I do not know that this is the right explanation but it works. Was the last thing I came up on!

Why is an 'animations' property in UIImageView on custom UITableViewCell removed after calling [super prepareForReuse]?

recently, I am making a custom UITableViewCell with animatable UIImageView to play animated GIF. During making it, I found that an animation is not shown after the cell was reused.
It seems that UIImageView is affected by [self prepareForReuse]. When I did debugging it, I could get the following information.
** before calling [super prepareForReuse] **
(lldb) po self.testImageView
<UIImageView: 0x1ab681c0; frame = (39 0; 78 61); opaque = NO; autoresize = W+H; userInteractionEnabled = NO; animations = { UIImageAnimation=<CAKeyframeAnimation: 0x1ab77870>; }; layer = <CALayer: 0x1ab68b60>> - (null)
** after calling [super prepareForReuse] **
(lldb) n
(lldb) po self.testImageView
<UIImageView: 0x1ab681c0; frame = (39 0; 78 61); opaque = NO; autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0x1ab68b60>> - (null)
As it show, "animations = { UIImageAnimation=; };" is removed.
Fortunately, I could work around that problem by making a new instance of UIImage and setting it to UIImageView again as a workaround. For doing this workaround, it's important to re-create an instance of UIImage not to reuse an instance which is already set to UIImageView.
Anyway, what I really want to know is why prepareForReuse method removes the information about an animation.
Please let me know an internal reuse logic of the method which is hidden.
Upon calling [super prepareForReuse], stopAnimation is called on self.testImageView. I think this is the right logic being used so that the cell can be reused. You just need to call the [self.testImageView startAnimation] method once the cell is prepared for reuse. You need not resort to the "workaround" method that you have mentioned.
Found a solution. I had to set the UIImageView's image property to nil in prepareForReuse(), before setting a new (or even the same) animated UIImage.

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

Resources