-3

I have been studying delegate.

so i write code that delegate then run this code.

Can you tell me what's the problem?

this is my code.

ViewController2.h

@protocol ViewController2Delegate <NSObject>;
@required
-(void)practiceDelegateMethod:(Float32)var1 andVar2:(Float32)var2;
@end
@interface ViewController2 : UIViewController
@property (assign, nonatomic) id <CompressSetupViewControllerDelegate> delegate;

ViewController2.m

@synthesize delegate;

- (IBAction)compressSetupCancleAction:(id)sender {
   [self.delegate practiceDelegateMethod:var1 andVar2:var2];
   [self dismissViewControllerAnimated:YES completion:nil];
  }

ViewController1.h

@interface HomeViewController : UIViewController< ViewController2Delegate >

ViewController1.m

-(void) practiceDelegateMethod:(Float32)var1 andVar2:(Float32)var2{
    NSLog(@"delegate var1 : %@    var2 : %@",[NSString stringWithFormat:@"%f",var1],[NSString stringWithFormat:@"%f",var2]);
}
boraseoksoon
  • 2,164
  • 1
  • 20
  • 25
Jisoo Kwon
  • 21
  • 1
  • 1
  • 6

4 Answers4

-1

In your HomeViewController When ever you make instance of ViewController2 assign delegate self

    ViewController2 *controller = [[ViewController2 alloc]init];
    controller.delegate = self
Rahul Patel
  • 1,822
  • 12
  • 21
-1

I think the problem is that you are not getting the reference of the viewController2 in viewController1 due to some problem. get the non-nil reference of viewController2 in viewcontroller1 and then

<ref of ViewController 2>.delegate = self

I think you have reference of viewController 2 in viewcontroller1 as nil.

LearneriOS
  • 309
  • 6
  • 18
-1

Try after replacing

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

with:

@property (assign, nonatomic) id <ViewController2Delegate> delegate;
Mehul Sojitra
  • 1,181
  • 9
  • 15
  • original code is CompressSetupViewControllerDelegate. – Jisoo Kwon Dec 30 '15 at 05:39
  • Have you set classObj.delegate = self; in ViewController1 after creating object of class? – Mehul Sojitra Dec 30 '15 at 05:43
  • Then where is your code for CompressSetupViewControllerDelegate protocol? – Mehul Sojitra Dec 30 '15 at 05:51
  • Hi your question is little bit confusing: because you say that original code is CompressSetupViewControllerDelegate and there is no code for CompressSetupViewControllerDelegate protocol and in ViewController1.h file you write . Basically you want to work with ViewController2Delegate or CompressSetupViewControllerDelegate? – Mehul Sojitra Dec 30 '15 at 06:22
  • thanks your help. i'm solved problem use notification. – Jisoo Kwon Dec 30 '15 at 09:08
-1

There is one other way to pass data between viewcontrollers NSNotification

From the class sending the message, post a notification like :

[[NSNotificationCenter defaultCenter] postNotificationName: @"YOUR_NOTIFICATION_NAME" object: anyobjectyouwanttosendalong(can be nil)];

In the view controllers where you want to be notified of the notification when posted:

In the viewDidLoad do:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(METHOD_YOU_WANT_TO_INVOKE_ON_NOTIFICATION_RECEIVED) name:@"YOUR_NOTIFICATION_NAME" object:sameasbefore/nil];

Important! Don't forget this in your viewDidUnload():

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"YOUR_NOTIFICATION_NAME" object:sameasbefore/nil];

Note:-When it's only one object notifying another one, you're better off using protocols :) But in this case since there are multiple view controllers listening, use notifications

if you want to go with protocol then refer above link

Click Here

Community
  • 1
  • 1