[Tutorial] Currency Converter [Part II of 2]

Discussion in 'iOS Development' started by allbrokeup, Oct 3, 2008.

  1. allbrokeup

    allbrokeup New Member

    Joined:
    Mar 7, 2008
    Messages:
    51
    Likes Received:
    0
    OK, now, save and close Interface Builder and return to Xcode.

    Open up your MainView.h file, it should look something like this:

    Code:
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    
    @interface MainView : UIView {    
    IBOutlet UITextField *dollarField;    
    IBOutlet UITextField *newAmount;    
    IBOutlet UITextField *rateField;	
    float amount;	
    
    }
    
    - (IBAction)convert:(id)sender;
    - (IBAction)xrates:(id)sender;
    - (IBAction)rates:(id)sender;
    - (IBAction)clear:(id)sender;
    
    @end
    Now, if it doesn't look exactly like above, change yours to the one I have, because it is from my project. Please Note: You can change the Action names and Outlet names to something a little more memorable, but as this is a tutorial, it is wise to follow it. Comment out the "xrates:", "clear:" and "rates:" IBAction definitions for now ("//" in front of the line). Remember to uncomment (remove "//") before finishing, or you will get disfunction in the UI and Warnings with compilation.

    Open up your MainView.m file now. Copy in the code relative to the blocks. Now, you will not find the "Clear", "Rates" or the "Xrates" blocks in your code by default, so don't include them now, just copy in the code for the Outlets and Actions you have. We will cover the others later.

    Code:
    #import "MainView.h"
    
    @implementation MainView
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    	// Return YES for supported orientations	
    return YES;
    }
    
    - (IBAction)clear:(id)sender {
        dollarField.text = [NSString stringWithFormat:@""];
    	rateField.text = [NSString stringWithFormat:@""];
    	newAmount.text = [NSString stringWithFormat:@""];
    }
    
    //- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    	//return 1;
    //}
    
    //- (NSInteger)tableView:(UITableView *)tableView //numberOfRowsInSection:(NSInteger)section {
    //	return 2;
    //}
    
    - (IBAction)convert:(id)sender {
        amount = [dollarField.text floatValue] * [rateField.text floatValue];
    	newAmount.text = [NSString stringWithFormat:@"%f", amount];
    }
    
    - (IBAction)xrates:(id)sender {
    	 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.x-rates.com"]];
    }
    
    - (IBAction)rates:(id)sender {
        NSString * errorString = [NSString stringWithFormat:@"EUR/USD = +0.322 %, CAD/USD = +0.135 %, JPY/USD = -0.394 %, AUD/USD = +0.820 %, GBP/USD = +0.033 %, GBP/EUR = -0.288 %, JPY/EUR = -0.714 %, GBP/AUD = -0.781 %, JPY/AUD = -1.205 %"];
    	NSLog(@"Opened up Rates Alert");	
    	UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Exchange Rates" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];	
    	[errorAlert show]; 
    }
    
    - (BOOL)textFieldShouldReturn:(UITextField *)textField {
    	[textField resignFirstResponder];
    	return YES;
    }
    
    @end
    Okay, now click the Project menu in Xcode, set the Active SDK to "Simulator 2.1". Build & Go.

    You shouldn't get any errors, if you get warnings about overlapping subviews or anything, ignore them.

    OK, have you tested it? It works? OK, now go back into IB.

    Click the UIView (MainView), go to the Identity Tab and add two actions "xrates:" and "clear:". Click the third UITextField, go to its Attributes in Inspector and Disable User Interaction. Click on the First Text Field, Control-click from it to the MainView, delegate it. Same with the "Rate:" text field. Now in the Library, Add two UIButtons, one named Clear and one named "Get Latest Rates...". Click the MainView again, go to the Connections Tab and wire "clear:" to the Clear Button with Touch Up Inside, and the "xrates:" action to the Get Latest button with Touch Up Inside also. Save and Quit.

    Now Build and Go, again in Xcode.

    When the Simulator opens, you should get a nice interface, kind of simple unless you customized it with UINavigation bars or custom graphics.

    End of Tutorial.

    P.S. If you want the "rates:" Action listed in my code, simply create another action named "rates:" in IB, wire it up to Touch Up Inside on a UIButton and copy in the code from my sample. This brings up a UIAlertView with late September Exchange rates. As of late, I cannot compile this code and update it since I am on Windows until the weekend.

    Feedback welcome, PM or reply if there are any errors in compiling and I will try to work them out and I will update the tutorial if needed.

    Cheers!

Share This Page