O'Reilly logo

Beginning iOS 5 Application Development by Wei-Meng Lee

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

NOTIFYING OTHER OBJECTS USING THE NSNOTIFICATION CLASS

In this book, you have seen that all the delegate methods of objects are defined in the same class as the object. For example, one common class that you have seen is the UIAlertView class. When you have more than one button displayed in a UIAlertView object, you need to implement the alertView:clickedButtonAtIndex: method to handle the clicking of the buttons. This method can be defined with the same class it is used in (for example, in a View Controller class), or it can be declared separately in another class. However, if the method is defined in another class, how would you notify the View Controller when a button is clicked? This is the challenge: How do different classes communicate with one another? The following Try It Out shows you one way to do this using the NSNotification class.

TRY IT OUT: Using Notifications

image

  1. Using Xcode, create a new Single View Application (iPhone) project and name it Notifications. You need to set the class prefix to the project name and ensure that you have the Use Automatic Reference Counting option unchecked.
  2. Add a new Objective-C Class file to the project and name it AlertViewDelegates.m.
  3. Add the following lines in bold to the AlertViewDelegates.h file:
    #import <Foundation/Foundation.h>
    
    @interface AlertViewDelegates : NSObject
    <UIAlertViewDelegate>
    
    @end
  4. Add the following lines in bold ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required