Inherits from NSObject
Declared in OCAUtilities.h

Overview

General utility routines

Most of the methods are Category Extensions.

For convenience, this header also includes 3 macros that “extend” NSLog.

They all use other Cocoa macros to display method names and line numbers on NSLog statements.

  • DLog is useful for general Debugging. For release builds they compile out.
  • ALog is similar to DLog, but they Always Log.
  • ELog is a special version that knows how to parse an NSError (to a limited extent). Like ALog, they Always Log.

Class Methods

dismissKeyboard

Dismisses the Keyboard Calls resignFirstResponder (safely) on the UIView and all its subViews recursively.

+ (void)dismissKeyboard

Discussion

Performance is linear with respect to the number of subViews. Do not use this method if the number of subViews may be large.

Declared In

OCAUtilities.h

getScreenBoundsForCurrentOrientation

Gets screen bounds allowing for device orientation

+ (CGRect)getScreenBoundsForCurrentOrientation

Return Value

Oriented CGRect

Discussion

UIScreen bounds are always in Portrait mode. This routine swaps H + W if Landscape.

Declared In

OCAUtilities.h

isValidEmailFormat:

Examines the string to determine if it is formatted like an email address - e.g., name@domain

+ (BOOL)isValidEmailFormat:(NSString *)email

Parameters

email

The string to validate

Return Value

YES if the string appears to be formatted correctly, NO otherwise

Declared In

OCAUtilities.h

offsetTextField:inScrollView:withNotification:

Offsets a UITextField or UITextView if hidden when the keyboard is shown

+ (void)offsetTextField:(CGRect)fieldRect inScrollView:(UIScrollView *)scrollView withNotification:(NSNotification *)aNotification

Parameters

fieldRect

The CGRect of UITextField or UITextView to expose, in the scrollView’s coordinate system.

scrollView

The UIScrollView containing the activeField

aNotification

The NSNotification object

Discussion

This routine follows Apple’s recommendations here: http://developer.apple.com/library/ios/#DOCUMENTATION/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html

There are several steps to use this routine but the pattern is simple enough:

  1. Register (and remove) yourself for keyboard notifications - recommended to do this in viewWillAppear and viewWillDisappear
  2. If you have multiple UITextFields and/or UITextViews, keep track of which is “active” - typically a global updated in textFieldDidBeginEditing. Note that UITextView seems to get its notification after textViewDidBeginEditing, so figure out a way to deal with that.
  3. Invoke this method in your keyboardWasShown delegate. See the note about converting to the scrollView’s coordinate system.
  4. Invoke resetTextField:inScrollView in your keyboardWillHide delegate

NOTE - if the textField/View is in a subview of the scrollView, you’ll need to translate it’s coordinates into the scrollView’s. For example:

CGRect fieldRect = [self convertRect: activeField.frame toView: scrollView]; CGPoint scrollOffset = [OCAUtilities offsetTextField: fieldRect inScrollViews: scrollView withNotification: myKeyboardNotification];

Declared In

OCAUtilities.h

resetTextField:inScrollView:

Resets a UITextField or UITextView to its original position after editing (using keyboard)

+ (void)resetTextField:(UIView *)activeField inScrollView:(UIScrollView *)scrollView

Parameters

activeField

The UITextField or UITextView to expose

scrollView

The UIScrollView containing the activeField

Discussion

see offsetTextField:inScrollView:withNotification for more information

Declared In

OCAUtilities.h

showAlertWithMessageAndType:alertType:

Display an alert message to the user

+ (void)showAlertWithMessageAndType:(NSString *)theMessage alertType:(NSString *)theType

Parameters

theMessage

The message to display

theType

Alert type to display (typically, Information, Warning, Error)

Declared In

OCAUtilities.h

showErrorWithMessage:

Display an alert message to the user indicating Error

+ (void)showErrorWithMessage:(NSString *)theMessage

Parameters

theMessage

The message to display

Declared In

OCAUtilities.h

showWarningWithMessage:

Display an alert message for the user indicating Warning

+ (void)showWarningWithMessage:(NSString *)theMessage

Parameters

theMessage

The message to display

Declared In

OCAUtilities.h