简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM