Sunday, January 17, 2010

Text Fields and dismissing the keyboard

I was going back and making changes to my text fields and keyboard dismissing and wanted to move to the next field only if that field was blank. I relized then, that I had not taken any notes here on how to dismiss a keyboard, or how to assign a new text field. Here is the code to dismiss a field, and then assign the next one. I'll just write some if statements to figure out the order in which I want them to populate if the user needs to enter them all.

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {

if (theTextField == userName) {

[userName resignFirstResponder];

[userPassword becomeFirstResponder];

}

if (theTextField == userPassword) {

[userPassword resignFirstResponder];

[serverAddress becomeFirstResponder];

}

if (theTextField == serverAddress) {

[serverAddress resignFirstResponder];

}

return YES;

}



Ok, I wrote the if statements before I even finished writing this blog. It's messy, so if you know a bette/faster way to do this, do it your way!


- (BOOL)textFieldShouldReturn:(UITextField *)aTextField {

if (aTextField == userName) {

[userName resignFirstResponder];

if ([userPassword.text length] == 0) {

[userPassword becomeFirstResponder];

} else if ([serverAddress.text length] == 0) {

[serverAddress becomeFirstResponder];

}

}

if (aTextField == userPassword) {

[userPassword resignFirstResponder];

if ([serverAddress.text length] == 0) {

[serverAddress becomeFirstResponder];

}

}

if (aTextField == serverAddress) {

[serverAddress resignFirstResponder];

}

return YES;

}

Thursday, January 14, 2010

Working with SQL Databases on iPhone

I'm using a small sqlite database for my iPhone app and was having problems with the data. I was making changes to the database in either CLI or in Firefox SQLite Manager, deleting the database file from my iPhone project, and copying the new file in. It would never use the new data, only the old data was being accessed. I finally figured out what was going on because there was not enough rows when I was stepping through it and set out looking for an answer. Below is the first hit I found and it answered it perfectly.

Short answer: It get's cached like data for a normal application. Go look in your folder for the iPhone Simulator/USR/Applications and you will be able to delete it from there. Then compile your code again and it should be working.

Monday, January 11, 2010

Refreshing views while using a Tab Bar on the iPhone

In my tab bar based iPhone application I had a view that would connect to a web service and pull down the requested data. It would then use that data to populate labels and text fields on other views. I had it working for the first time using "- (void)viewDidLoad", but was not able to get it to update when I would go back to the main view and refresh the information.

I finally found that the viewWillAppear method just didn't have a place holder but I could add it and use it. So, just add the following code to the .m file controlling your view and it will update every time you switch back to it.

- (void)viewWillAppear:(BOOL)animated {

// Do stuff here

[super viewWillAppear:animated];

}


Thursday, January 07, 2010

Handling iPhone errors part two

Now that I can make a pop-up message I wanted to clean up the code and do all of my error processing from one place. To this end I removed all of the error handling I was doing and instead changed it so my individual functions would return a value. The number 0 if everything was ok, and a number that would signify the error type if everything was not ok. I had about 15 possible errors at the time so I gave them all numbers and converted my functions from -(void) to -(int) and added the numbers to the return lines. Below you will see an example of the code and how I'm now processing the errors. Everything works fine, but if there is a better way to do this, please let me and everyone else know.

// Header file

- (IBAction)refreshInformation:(id)sender;

- (int)verifyUserInput;

- (void)handleErrors:(int)errorNumber;


// Call the function to check user input, and then pass the results to the error checking //function

- (IBAction)refreshInformation:(id)sender {

NSInteger funcReturn = 0;


// Verify they have entered everything we need to get the information

funcReturn = [self verifyUserInput];


// Did we have any errors?

if (funcReturn) {

[self handleErrors:funcReturn];

}

}



// Function to check the user input

- (int)verifyUserInput {


// Initialize some local variables with the input from the user

NSString *nameString = userName.text;

NSString *passString = userPassword.text;

NSString *serverString = serverAddress.text;

// Verify that people are putting the right stuff into the fields

if ([nameString length] == 0) {

return 11;

} else if ([passString length] == 0) {

return 11;

} else if ([serverString length] == 0) {

return 11;

}


return 0;

}


// Function to process the error numbers

- (void)handleErrors:(int)errorNumber {

NSString *errorText;

switch (errorNumber) {

case 11:

errorText = @"All fields are required";

break;

case 21:

errorText = @"Authentication failed";

break;

case 22:

errorText = @"System login failure";

break;

case 23:

errorText = @"Read-Only user required";

break;

case 24:

errorText = @"Network connection failed";

break;

}


UIAlertView *error = [[UIAlertView alloc] initWithTitle:[[NSString alloc] initWithFormat:@"Error: %D", errorNumber]

message:[[NSString alloc] initWithFormat:@"%@", errorText]

delegate:nil

cancelButtonTitle:@"OK"

otherButtonTitles: nil];

[error show];

[error release];

}


Wednesday, January 06, 2010

Handling Errors on the iPhone

I read that you need to have a popup error if your application can't get to a network and it's required, so I did some quick checking on how to do that and here is the code I found. Very simple to use and modify to add specific errors. Just place this into a function and call it, or put it right into your code where an error occurs.

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"

message:@"This is the error or get a string"

delegate:nil

cancelButtonTitle:@"OK"

otherButtonTitles: nil];

[alert show];

[alert release];


iPhone TabBar icons



Happy New Year everyone!

As with all fun projects, I've taken a new tactic at how to do my iPhone application. It now includes a tab bar at the bottom for switching between pages. I now had a new problem. All of the icons were coming up as grey blobs in the bar and it took me a while to figure out how to fix it. I never really found a good answer on the internet, so it's my own info here (not a link like normal).

I found that the icons should be 30x30 pixels and should be in grey scale. I also have found that anything that is colored shows up as a light grey. It doesn't seem to mater if this is white, grey, black, etc. Everything else should be transparent and that will will show up black.

The pictures above are black lines on a transparent background.

-Tige