Resolved Need some help with my view controller, please.

Discussion in 'iOS Programming' started by Tander, Jul 14, 2012.

  1. Tander, Jul 14, 2012
    Last edited: Jul 14, 2012

    Tander macrumors 6502a

    Tander

    Joined:
    Oct 21, 2011
    Location:
    Johannesburg, South Africa
    #1
    Hi Guys,

    So, I am busy with Stanford's online courses (CS193P)

    With the first assignment, we're tasked with adding a 'history display' to our calculator. The display must hold all digits pressed and retain them, until I press clear. (Clear was also a homework assignment)

    Problem I have is:

    If I type in : 44 + 99

    This shows up on history display - but the main display will not clear the text after pressing the operation button.

    So history display will show:
    Main display will show:
    I'm using an instance variable "userIsinMiddleOfEnteringNumber" do tell the compiler when it should / should not append text strings.

    Here is the code I have for CalculatorViewController.m

    Code:
    #import "CalculatorViewController.h"
    
    @interface CalculatorViewController ()
    
    @end
    
    @implementation CalculatorViewController
    
    
    //If brain is has not start it - start it - otherwise don't. (We only want, one copy of brain) 
    
    - (CalculatorBrain *)brain 
    {
        if (!brain) brain = [ [CalculatorBrain alloc] init]; 
        return brain;
        
    }
    
    //Implimentation for digipressed: 
    
    -(IBAction)digitPressed:(UIButton *)sender
    
    {
            //Get the current digit pressed 
        
        NSString *digit = sender.currentTitle;  
        
        //If user is in the middle of entering number - append the next number to current number 
    
        if (userIsInMiddleOfEnteringNumber) {
            [display setText:[[display text] stringByAppendingFormat:digit]]; 
            [historyDisplay setText:[[historyDisplay text] stringByAppendingFormat:digit]]; 
            
        }
        else //Don't append. 
        {
            [display setText:digit];
            [historyDisplay setText:digit]; 
            userIsInMiddleOfEnteringNumber = YES; 
        }
    }
    
    
    -(IBAction)operationPressed: (UIButton *)sender
    
    {    
        
        NSString *operationPressed = sender.currentTitle;   
        if (userIsInMiddleOfEnteringNumber)
        {
           [historyDisplay setText:[[historyDisplay text] stringByAppendingFormat:operationPressed]]; 
             
            [[self brain ] setOperand:[[display text] doubleValue]];
        
                }
        else 
        {
            [historyDisplay setText:operationPressed]; 
        }
    
        NSString *operation = sender.currentTitle;  
        double result = [[self brain] preformOperation:operation]; 
        [display setText:[NSString stringWithFormat:@ "%g", result]];
    }
    
    - (IBAction)clearPressed 
    {
        display.text = @""; 
        historyDisplay.text = @""; 
        brain = nil;   
    }
    @end
    
    This is CalculatorViewController.h

    Code:
    //
    //  CalculatorViewController.h
    //  Calculator
    //
    //  Created by Robert Clegg on 2012/07/10.
    //  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    #import "CalculatorBrain.h" 
    
    @interface CalculatorViewController : UIViewController
    {
        
        //To provide YES/NO answer. 
        
    BOOL userIsInMiddleOfEnteringNumber; 
        
    
        //Main display for calculator 
        
    IBOutlet UILabel *display; 
    
        //History Display (Number entrered) 
        
    IBOutlet UILabel *historyDisplay;
    
        
    CalculatorBrain *brain; //** Connection to model** 
    }
    
    //Method declerations for pressing numbers. 
    
    -(IBAction)digitPressed:(UIButton *)sender; 
    
    -(IBAction)operationPressed: (UIButton *)sender; 
    
    - (IBAction)clearPressed;
    
    @end
    
    Can someone please tell me what silly mistake I am making here?
     
  2. Tander thread starter macrumors 6502a

    Tander

    Joined:
    Oct 21, 2011
    Location:
    Johannesburg, South Africa
    #2
    Hi guys,

    Okay, so after posting this code, I went back to the project and went through the code step by step. I also, looked at the walkthrough and realized that one part of the code was not needed and that I had set the history display to behave the same way as my main display - only difference was that main display was not detecting :userIsInMiddleOfEnteringNumber.

    So, I made the following changes (See code in red) in my implementation file:

    Code:
    #import "CalculatorViewController.h"
    
    @interface CalculatorViewController ()
    
    @end
    
    @implementation CalculatorViewController
    
    
    //If brain is has not start it - start it - otherwise don't. (We only want, one copy of brain) 
    
    - (CalculatorBrain *)brain 
    {
        if (!brain) brain = [ [CalculatorBrain alloc] init]; 
        return brain;
        
    }
    
    //Implimentation for digipressed: 
    
    -(IBAction)digitPressed:(UIButton *)sender
    
    {
            //Get the current digit pressed 
        
        NSString *digit = sender.currentTitle;  
        
        //If user is in the middle of entering number - append the next number to current number 
    
        if (userIsInMiddleOfEnteringNumber) {
            [display setText:[[display text] stringByAppendingFormat:digit]]; 
            
        }
        else //Don't append. 
        {
            [display setText:digit];
            userIsInMiddleOfEnteringNumber = YES;
        }
     [COLOR="Red"]   [historyDisplay setText:[[historyDisplay text] stringByAppendingFormat:digit]]; [/COLOR]
    
    }
    
    
    -(IBAction)operationPressed: (UIButton *)sender
    
    {    
        
        NSString *operationPressed = sender.currentTitle;   
        if (userIsInMiddleOfEnteringNumber)
        {
           [historyDisplay setText:[[historyDisplay text] stringByAppendingFormat:operationPressed]]; 
             
            [[self brain ] setOperand:[[display text] doubleValue]];
    [COLOR="red"]        userIsInMiddleOfEnteringNumber = NO; [/COLOR]
        
        }
    
        
        NSString *operation = sender.currentTitle;  
        double result = [[self brain] preformOperation:operation]; 
        [display setText:[NSString stringWithFormat:@ "%g", result]];
        
    }
    
    - (IBAction)clearPressed 
    {
        display.text = @""; 
        historyDisplay.text = @""; 
        brain = nil;   
    }
    @end
    
    Now both displays behave as they are intend to. :cool:

    (P.S: Im starting to have fun with code!! ) :D
     
  3. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
  4. Tander thread starter macrumors 6502a

    Tander

    Joined:
    Oct 21, 2011
    Location:
    Johannesburg, South Africa
    #4
    Yes, thank you. Working as I expect it.

    Now if I can just figure out how to do back space and adding a period button, I'm good to go!
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    Hint.
     

Share This Page