Los datos de DetailViewController no se muestran

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): enter image description here

Here is what it comes out looking like in the iOS Simulator: enter image description here

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 resultado label, whereas the leftmost is the unidades que etiqueta.

Many thanks to anyone who can help this beginning developer along the path of programming righteousness.

preguntado el 09 de septiembre de 13 a las 08:09

Try using breakpoints to make sure your ifs and switches do what you expect them to do. -

2 Respuestas

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?

Respondido el 09 de Septiembre de 13 a las 09:09

Great; fixed! I'm assuming that's the difference between the 3/3GS/4/4S and iPhone 5 screen sizes. Any ideas on the main issue? - ecfedele

Yep (if that were it, I'd bow my head and run away in shame). Now that I think about it, there's a possibility that I might have erroneously hooked up the UISwitch or UIStepper to an IBOutlet, which obviously won't make the dflt statement true and do anything. Let me check that; I'll keep you posted. - ecfedele

Right now it's connected to IBOutlet. Does it need to be linked to IBAction? - ecfedele

A IBOutlet gives you access from your code to the object in IB; an IBaction gives an object in IB the ability to call code in your source. Put differently: an IBOutlet allows you to change the object fro within code (set it hidden, change its frame or color, remove it from superview etc), an IBaction is simply a method that will be shown in IB and might (not necessarily) be executed as a consequence of an action on the control in IB. So I think you would want your value3 be connected to an IBOutlet, and the calculate btn to an IBAction. - Joride

The Calculate button and double-precision switch are routed to IBActions, whereas everything else is IBOutlet. - ecfedele

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

Respondido el 10 de Septiembre de 13 a las 08:09

How do you declare the selector? It throws me "No visible @interface for UIViewController declares the selector 'viewDidAppear' - ecfedele

Excuse me, I made a mistake in the method name. - Joride

I added it and it gives me two things: a warning at the function declaration telling me that there's a conflicting return "void vs. id", secondly upon simulating when I enter the DetailViewController it throws an error. It doesn't seem to like the first NSAssert in particular. - ecfedele

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.