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
- 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.
- Add a new Objective-C Class file to the project and name it AlertViewDelegates.m.
- Add the following lines in bold to the AlertViewDelegates.h file:
#import <Foundation/Foundation.h> @interface AlertViewDelegates : NSObject <UIAlertViewDelegate> @end
- Add the following lines in bold ...
Get Beginning iOS 5 Application Development now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.