CS193P - Adding cancel button to iOS calculator

老子叫甜甜 提交于 2019-12-08 01:28:17

问题


I recently started following the online course on iPhone development from Stanford University on iTunes U.

I'm trying to do the homework assignments now for the first couple of lectures. I followed through the walkthrough where I built a basic calculator, but now I'm trying the first assignment and I can't seem to work it out. It's a follows:

Add a “C” button that clears everything (for example, the display in your View, the operand stack in your Model, any state you maintain in your Controller, etc.). Make sure 3 7 C 5 results in 5 showing in the display. You will have to add API to your Model to support this feature.

What API do i need to add?

I tried something like this:

- (IBAction)CancelPressed {
    self.Display.text = 0;
}

I know this is wrong. I need some guidance. Thanks in advance..Sorry if the question is stupid..


回答1:


I am also going through the fall 2011 version on iTunesU. Here is the way I accomplished this.

- (IBAction)clearPressed {
    self.display.text = @"0";
    self.userIsInTheMiddleOfEnteringANumber = NO;
    self.brain = nil;
}

The only thing is I actually did not have to add API to my model. Because the controller has a CalculatorBrain instance variable I just toss that out, and since we lazily instantiated our brain getter, I'll get a brand new one (already cleared) next time I call the getter.




回答2:


Here is how I did it:

Code within CalculatorViewController.m:

//********************************************************
//
//This method is called when the user presses the Clear
//button (labeled "C").
//
//********************************************************

- (IBAction)clearPressed {
    self.historyDisplay.text = @"";         //Clear history label
    self.display.text = @"0";               //Reset calculator display label to 0
    _userIsInTheMiddleOfTypingANumber = NO; //Reset the user tracking feature
    [self.brain clearStack];                //Calls method to "clear" the stack

    //The following line may not be needed depended on your implementation of the
    //decimal button. You may need something for your decimal implementation.
    _userAlreadyEnteredDecimal = NO;        //Reset the decimal boolean 
}

And then within CalculatorBrain.m:

//********************************************************
//
//"Clear" all values off of the stack.
//
//********************************************************
- (void)clearStack
{
    _operandStack = nil; //Deallocate instance of the stack
}



回答3:


You basically want to be resetting all of your variables. Assuming you are using a UILabel, it will take an NSString so you will be better off with:

self.display.text = @"0";

Then go through all other properties and instance variables that you have and set then to defaults. Anything that is an object wants to be set to nil. so if you are storing any strings for example. And any numbers that you are keeping hold of, set to 0. Or 0.0f if they are floats.

Not sure exactly what this calculator example is, but hopefully this will put you in the right direction.

If not and you need any more help, don't hesitate to let me know :)




回答4:


You should clear the stack as well:

- (IBAction)clearPressed {
    double result = [self.brain performOperation:@"C"];
}

and in performOperation: add:

else if ([operation isEqualToString:@"C"])
{
    [self.operandStack removeAllObjects];
    result = 0;
}



回答5:


I think cancelPressed shld be something like this:

- (IBAction)CancelPressed:(id)sender {

        double result = [self.brain performOperation:@"C"];
        NSString *resultString = [NSString stringWithFormat:@"%g",result];
        self.Display.text = resultString;

}



回答6:


Your CancelPressed: looks good.

Note that you can remove the (id)sender in the call. You do not have the know which button sent the message.

And you can do just (is a bit shorter):

self.Display.text = @"";


来源:https://stackoverflow.com/questions/9048117/cs193p-adding-cancel-button-to-ios-calculator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!