iOS Dev – Assignment #2 – Extra Task

Add a memo-function to store a number

Start my rearranging the buttons to make space for three new ones:

iOS Dev – Assignment #2 - Extra Task – Additional Buttons
iOS Dev – Assignment #2 – Extra Task – Additional Buttons

Create a variable to store the value:

float numberInMemory;

Initialize it:

- (void)viewDidLoad
{
    ...
    numberInMemory = 0;
}

And connect the buttons to suitable methods:

- (IBAction)addMemoryButtonPressed:(UIButton *)sender {
    numberInMemory += [self.numberTextField.text floatValue];
}

- (IBAction)subtractMemoryButtonPresed:(UIButton *)sender {
    numberInMemory -= [self.numberTextField.text floatValue];
}

- (IBAction)getMemoryButtonPressed:(UIButton *)sender {
     self.numberTextField.text = [NSString stringWithFormat:@"%@", [NSNumber numberWithFloat:numberInMemory]];
}
Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Flattr this!

iOS Dev – Assignment #2 – Task #4

Delegation The BasicCalculator communicates its result to a delegate. This asynchronous implementation is a bit overhead in this simple example, but think of a scientific calculator with more elaborate functionality. You can start a very complex operation and the Calculator-Class takes care of processing this operation in the background. After some time it finishes and communicates its result to the controller. Have a close look at the implementation of the delegation in the Basic- Calculator class. Now make your ViewController class the delegate of the calculator and make the project a fully working calculator.

When the view has loaded initiate the calculator and set the view controller as delegate of the calculator:
Continue reading “iOS Dev – Assignment #2 – Task #4”

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Flattr this!

iOS Dev – Assignment #2 – Task #3

Protocols In the header file of the BasicCalculator you will find a protocol that defines a method

- (void)operationDidCompleteWithResult:(NSNumber*)result;

Make your ViewController-Class comply to that protocol.

First define the view controller as delegate of the calculator, then define required method of the protocol and let it display the result in the view:

// ViewController.h
@interface ViewController : UIViewController <BasicCalculatorDelegate>

- (void)operationDidCompleteWithResult:(NSNumber*)result;

// ViewController.m
- (void)operationDidCompleteWithResult:(NSNumber*)result {
    self.numberTextField.text = [NSString stringWithFormat:@"%@", result];
}

Please note that at the moment the view controller class only complies to the protocol. Even if its methods would already work as intended, nothing changed in the functionality compared with the previous task, because nobody actually uses the protocol …

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Flattr this!

iOS Dev – Assignment #2 – Task #2

Check some edge-cases Division by 0, double decimal points, leading 0s, …

Division by 0 is already handled in the code from the previous task.

For the double decimal points expand the commaPressed: method like in assignment #1:
Continue reading “iOS Dev – Assignment #2 – Task #2”

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Flattr this!

iOS Dev – Assignment #2 – Task #1

Model-View-Controller Since a calculator is something that might be used in various situations, you decide to rewrite your code to something more reusable. One advantage of MVC is that you can exchange the view but keep your model and parts of your controller. We provide you with a template for a class called BasicCalculator. Extend that class to provide a fully functional basic calculator. This class is your model.

The provided template is similar to the one from Assignment #1, where the calculator functionality is moved to the new model class. You could either use your code from the previous assignment and adjust it or redo the changes from the previous assignment inside the new template. Here we will choose the second approach.

First link the missing number buttons to the view controller class and change the tags of the buttons accordingly.
Continue reading “iOS Dev – Assignment #2 – Task #1”

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Flattr this!

iOS Dev – Lecture #3: Foundation Classes

The third lecture starts with a review of the last lectures followed by an introduction to data structures in Objective-C. Numbers are represented as int, float, double, NSNumber, booleans as BOOL, Strings as NSString and NSMutableString, dates as NSDate, and binary data as NSData. Further primitives are NSIndex and NSValue.
As collections there are arrays represented as NSArray and NSMutableArray, sets as NSSet and NSMutableSet, and dictionaries as NSDictionary and NSMutableDictionary.
In addition there are blocks for executable code.

Foundation classes are by default immutable which has the advantage of having no unwanted side-effects when passing objects to methods. To be changeable after initialization they have to be defined as Mutable.
Continue reading “iOS Dev – Lecture #3: Foundation Classes”

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Flattr this!

iOS Dev – Assignment #1 – Extra Task #2

Visualize the currently active operation by letting the button be pushed until the second operand is entered.

A button can be displayed as pushed by “highlighting”. However, when the resultButtonPressed: method fires, the button is already highlighted and will be dehighlighted shortly after. Thus it is necessary to “wait a little bit” actually till the “normal button functionality” has finished before highlighting the button. This can be achieved by adding an operation on the main queue.
To remove the highlighting, add a property to hold the currently pressed button.

@property (nonatomic, strong) UIButton *currentButton;

- (IBAction)operationButtonPressed:(UIButton *)sender {
	...
	else
	{
		...
		self.currentButton.highlighted = NO;
	}
	...
	[[NSOperationQueue mainQueue] addOperationWithBlock:^{
		sender.highlighted = YES;
		self.currentButton = sender;
	}];
}

- (IBAction)resultButtonPressed:(id)sender {
	...
	self.currentButton.highlighted = NO;
}

- (IBAction)clearDisplay:(id)sender {
	...
	self.currentButton.highlighted = NO;
}
Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Flattr this!