Try the search, it's linked to some great forums

Showing posts with label delegate. Show all posts
Showing posts with label delegate. Show all posts

Monday, June 11, 2012

Delegation - passing info between controllers

The flipside template in Xcode provides probably the simplest implementation of a custom delegate that there is.  It allows the programmer to pass data from the flipside (settings data, flagHeight) to the MainView.  Here it is;

Create the delegate protocol and it's methods, define the delegate property and define a property to pass (flipsideInfo);
in FlipsideViewController.h -
@class FlipsideViewController;    // forward reference

@protocol FlipsideViewControllerDelegate   // delegate name & required method
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
@end

@interface FlipsideViewController : UIViewController

@property (weak, nonatomic) id delegate;
@property (strong, nonatomic) IBOutlet UITextField *flipsideInfo;  // data to pass on

Oh, and don't forget to synthesize the delegate;
in FlipsideViewController.m - 
@implementation FlipsideViewController
@synthesize delegate = _delegate;

Set the MainViewController to conform the the delegate, FlipsideViewControllerDelegate;
in MainViewController.h -
@interface MainViewController : UIViewController <FlipsideViewControllerDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate>

Implement the required delegate method flipsideViewControllerDidFinish: 
in MainViewController.m -
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
    self.myAssistantLabel.text = controller.flipsideInfo.text;  // flipsideInfo is a property of the flipsideVC
    [self dismissModalViewControllerAnimated:YES];
}

And that's all she wrote!

Sunday, June 10, 2012

Delegation - implementing canned delegate protocols

Building on the prior post on creating your own delegates, this one describes the simple process of implementing a delegate provided to you by Xcode (or someone else I guess)

Delegation - creating your own delegate

Delegation is a way to have an unrelated class (object) perform methods for another class (object).  A delegate is a protocol, and you declare the delegate in the class that you want it to operate on.  The following example shows all of the code details for building your own custom delegate.  Many delegates come prepackaged in iOS and you implement them in a similar way shown below this example.

You declare a delegate in the class header file either above or below the @interface code.  You give it a name & list the methods that it can perform.  The methods can either be optional or required.  Required is the default.   The (float)smileForFaceView: is a required method that must be implemented by whoever declares themselves conforming to the delegate. NOTE; that we send a copy of ourself (FaceView) along with the method.  This allows the other object to look at the FaceView object.
in FaceView.h -
@protocol FaceViewDataSource
- (float)smileForFaceView:(FaceView *)sender;
@end

@interface FaceView : UIView

then in a class that you want to conform to the delegate, you add the following to it's @interface line.
in HappinessViewController.m - (for a private delegation??)
 @interface HappinessViewController()

or in HappinessViewCntroller.m - (more common way to do)
  @interface HappinessViewController

 We next need to declare ourselves as the delegate like;
in FaceView.h - (?? declares a property of FaceView.  This allows the conforming class to ??
@property (nonatomic, weak) IBOutlet id dataSource;

We then also need to declare the new class as the delegate??
in HappinessViewController.m - the method - - (void)setFaceView:(FaceView *)faceView
self.faceView.dataSource = self;

Now the final thing to do is just implement the required methods of this delegate in the conforming class (HappinessViewController).  We then implement smileForFaceView like;
in HappinessViewController.m - 
- (float)smileForFaceView:(FaceView *)sender   // this just convert the happiness property to +/- 1
{
    return (self.happiness - 50) / 50.0;
}

And finally ??????;
in FaceView.m, the - draawRect method - 
    float smile = [self.dataSource smileForFaceView:self]; // delegate our View's data
 See the next Post for implementing canned delegates;

 (as always many thanks to the incomparable Paul Hegerty and his Stanford iOS lectures