user2336727
user2336727

Reputation: 7

calling delegates in viewdidload

Quick question,

i have made a custom delegate

PupilView.h

@protocol DismissPupilViewPopoverDelegate
- (int) getPupilViewReason;
@end


@interface PupilView : UIViewController{
    id<DismissPupilViewPopoverDelegate> delegate;
}

@property (nonatomic, assign) id<DismissPupilViewPopoverDelegate> delegate;

It is called in PupilView.m like follows

[[self delegate] getPupilViewReason];

in in my maincontroller.h

#import "PupilView.h"

@interface MainScreen : UIViewController<DismissPupilViewPopoverDelegate>

maincontroller.m

-(int) getPupilViewReason
{
    return 100;
}

If i put the [[self delegate] getPupilViewReason]; in any function in pupilview.m it works perfectly, returns 100 i can see it with a breakpoint etc.

If i put it in viewdidload it dosn't load, returns 0, dosnt hit breakpoints etc. Any help as to why.

thanks

Upvotes: 0

Views: 881

Answers (3)

make a custom init method for the view controller where you pass the delegate so you can set the delegate in the init method before viewdidload is called.

@interface
- initWithDelegate:(id)aDelegate nibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
//...
@end

@implementation
    - initWithDelegate:aDelegate nibName:nibNameOrNil bundle:nibBundleOrNil{
      self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
          if (self) {
            _delegate = aDelegate;
            ///rest of init implementation
           }
    }

    - (void)viewDidLoad{
           [super viewDidLoad];

           [self.delegate getPupilViewReason];

    }
//...
@end

Upvotes: 1

Dipak Chaudhari
Dipak Chaudhari

Reputation: 655

Yes, you can call the delegate in viewDidLoad function.

Suppose your delegate(request owner) name is "delegate" then you can call like -

       - (void)viewDidLoad
           {
               [super viewDidLoad];

               [<OBJ>.delegate GetPupilViewReason];

           }

OBJ - is the instance of a class, have assign the delegate.

Upvotes: 0

Maxthon Chan
Maxthon Chan

Reputation: 1189

Go ahead and call. If the delegate is set up properly then it should work. Do you mean when you're doing a segue UI transfer?

Upvotes: 0

Related Questions