Monday, March 2, 2009 at 3:16 PM |  
Here are some tips I picked up while trying to create some of my own projects.

1) To access the application delegate class do this:

YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];

Then you can access objects from that class.

2) When using TextFields/TextViews you'll want to conform to the UITextFieldDelegate and UITextViewDelegate protocols. That way when you are done typing in either field you can hide the keyboard with the following code:

-(BOOL)textFieldShouldReturn:(UITextField *)theTextField
{
  if(theTextField == textField)
  {
     [textField resignFirstResponder];
  }
  return YES;
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
  if([text isEqualToString:@"\n"])
  {
    [self.textView resignFirstResponder];
    return NO;
  }
  return YES;
}

3) It's possible to detect when a tab bar controller switches tabs. This is done by specifying a delegate of the tab bar controller, similar to specifying the delegate of the text field. Implement the tab bar controller delegate protocol. Set an object that you want to be the delegate for the tab bar controller, and in that class implement this method:

(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
Posted by JP

0 comments: