1

I have three viewControllers in my storyboard and three viewController classes for each of them. From my main viewController, I am opening a navigation viewController in a 'modal' type segue, which is a multi step form and has two views in it. When the user hits 'Finish' on the last (which is second) view, the modal is dismissed and user is back to the main screen.

I am doing this with delegates. and the code for the finish button is also in a delegate and is placed on the main viewController's implementation file. In achieving this I passed the delegate from main view to the navigation's first view, and then from the first view on clicking 'next', I passed the delegate to the second (last) view (which has the finish button).

the passing of delegate from main to navigation's first page is like this:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"CreateCast"])
    {
        UINavigationController *navigationController = segue.destinationViewController;
        CreateCastStepOneVC *createCastStepOneVC = [[navigationController viewControllers] objectAtIndex:0];
        createCastStepOneVC.delegate = self;
    }
}

the passing of delegate from navigation's first view to second view is like this:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"ToCastStepTwo"])
    {
        CreateCastStepTwoVC *createCastStepTwoVC = 
        segue.destinationViewController;
        createCastStepTwoVC.delegate = delegate;
    }
}

Things are done well and delegate is doing its job as required. But a warning pops up which is a concern:

Passing '_weak id' to parameter of incompatible type 'id'

Property declaration in first navigation view is like this:

@property (nonatomic, weak) id <CreateCastStepOneVCDelegate> delegate;

Property declaration is second navigation view is like this:

@property (nonatomic, weak) id <CreateCastStepTwoVCDelegate> delegate;
Firdous
  • 4,624
  • 13
  • 41
  • 80

2 Answers2

1

How have you declared the delegate property on CreateCastStepTwoVC? Also, are your delegates conforming to a protocol you have defined?

A typical declaration for a delegate property would look something like this:

@property (nonatomic, __unsafe_unretained) id<MyProtocol> delegate;

or if you're not using a protocol (not recommended):

@property (nonatomic, __unsafe_unretained) id delegate;

EDIT:

Having seen your property declarations, you need to change weak to __unsafe_unretained as per this question: Recommended way to declare delegate properties with ARC

Community
  • 1
  • 1
Will Pragnell
  • 3,073
  • 1
  • 20
  • 21
  • is this way of passing delegate through a view controller even recommended? or is there any other better way? – Firdous Feb 24 '12 at 11:17
  • I see no reason not to use delegates to do this. That siad, I've not worked much with storyboards of segues so am not entirely familiar with the best practices there. – Will Pragnell Feb 24 '12 at 11:34
0

You can subclass UINavigationController and add a custom protocol in your subclass. With this approach you will be able to call your delegate from all your view controllers inside your navigation controller. For example, this is the way I used to do that:

@class CustomNavigationController;

@protocol CustomNavControllerDelegate <NSObject>

- (void)editImageController:(CustomNavControllerDelegate *)controller
didFinishPickingMediaWithInfo:(NSDictionary *)info;
- (void)editImageControllerDidCancel:(CustomNavControllerDelegate *)controller;

@end

@interface CustomNavigationController : UINavigationController

@property (nonatomic, weak) id <UINavigationControllerDelegate, CustomNavControllerDelegate> delegate;

@end

In this example I've implemented a similar functionality to a UIImagePickerController. In fact, this is the way the picker is implemented if you look at it's declaration file.

amb
  • 4,798
  • 6
  • 41
  • 68