Question

I am creating a scientific calculator application, I have got all of the buttons working and the math. What I am trying to do is add a second view that will show all the actions that the calculator has performed. The best way (I could think of) was to store all the button presses and display them in this secondary view. But I do not know how to pass button presses between views.

I have done the tutorials on passing data from datafields to labels and that is something similar to how it should work, but the results need to not be replaced when more buttons are pressed as with the datafield to NSString method.

Any help is greatly appreciated. If something is unclear please let me know.

Was it helpful?

Solution

You don't necessarily need two view controllers. You can use your main view as a container (call it containerView). Place two other UIViews in it with each taking up the whole view. Make sure that one is not inside the other. They should be at a peer level inside the containerView. One is you your current view (call it calculatorView). The other is a view for the the actions (call it actionsView). Add a button to each of the two views with an associated IBAction to transition between the views. Your program will always remain in the same view controller; no passing between controllers in necessary. You transition like this:

[UIView animateWithDuration:1.0 delay:nil options:UIViewAnimationOptionTransitionFlipFromRight animations:^{
    [calculatorView setHidden:YES];
    [actionsView setHidden:NO];
} completion:nil];

and

[UIView animateWithDuration:1.0 delay:nil options:UIViewAnimationOptionTransitionFlipFromRight animations:^{
    [calculatorView setHidden:NO];
    [actionsView setHidden:YES];
} completion:nil];

While you can use two view controller and it is a good design pattern to learn, this is an easy solution that avoids that complexity in your calculator App.

OTHER TIPS

You could have an array called historyArray where you store the numeric value or an operator as the buttons are pressed. If this is going to be a key feature of your application, make a model class with a name like OperationHistory that allows values to be passed in corresponding with the button presses, and allows for queries depending on the amount of history you want. Examples of querying would be -allHistory and -historySinceLastClear.

Example:

@interface OperationHistory : NSObject
{
    @private:
    NSMutableArray *history;
}
- (void)addNumberToHistory:(NSNumber *)number;
- (void)addOperatorToHistory:(NSString *)operator;
- (void)addClearCommandToHistory; // serves as a marker only; doesn't clear history

- (NSArray *)allHistory;
- (NSArray *)historySinceLastClear;

- (void)clearHistory;

Also, regarding your original title, your xib files should not (and can not) be concerned with data storage. xib files are interfaces which, in your case, should be providing the view for your data model.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top