简体   繁体   中英

My Delegate method is not getting Called

I just started learning objective-c, i was breaking my head why this below simple code for protocols and delegates is not working, please clarify me why this is delegate method is not getting called. Thanks in advance.

ViewController.h

#import <UIKit/UIKit.h>
#include "secondViewController.h"

@interface ViewController : UIViewController<ConverterDelegate>

@property (strong,nonatomic) secondViewController *secondview;

@property (strong, nonatomic) IBOutlet UILabel *msgtext;

@property (strong, nonatomic) IBOutlet UIButton *converbutton;

@end

ViewController.m

#import "ViewController.h"
#import "secondViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize msgtext,converbutton,secondview;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.secondview=[[secondViewController alloc]init];
    secondview.delegate=self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)convertToFahrenheit:(int)celsius{

    float fah = ((1.8)*celsius)+32;
    msgtext.text = [NSString stringWithFormat:@"the %i deg Celsius             equal to %.2f Fah",celsius,fah];
}
@end

SecondViewController.h

#import <UIKit/UIKit.h>

@protocol ConverterDelegate <NSObject>
@required
-(void)convertToFahrenheit:(int)celsius;
@end

@interface secondViewController : UIViewController

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

@property (strong, nonatomic) IBOutlet UITextField *textfield;
- (IBAction)convert:(id)sender;

@end

secondViewController.m

#import "secondViewController.h"

@interface secondViewController ()

@end

@implementation secondViewController
@synthesize textfield,delegate;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}    

- (IBAction)convert:(id)sender {
    [delegate convertToFahrenheit:[textfield.text intValue]];
    //[self dismissViewControllerAnimated:YES completion:nil];
}
@end

I was totally confused why my delegate method is not getting called. I a newbie to Objective c please help me.

I hope following code will fulfil your requirements.

ViewController.h

#import <UIKit/UIKit.h>
#import "Calculation.h"

@interface ViewController : UIViewController<CalculationDelegate>

@property (weak, nonatomic) IBOutlet UILabel *msgtext;
@property (weak,nonatomic) IBOutlet UITextField * inputField;
@property (weak, nonatomic) IBOutlet UIButton *converbutton;

-(IBAction)convertMetod:(id)sender;
@end

ViewController.m

    #import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize msgtext,converbutton,inputField;

- (void)viewDidLoad
{
    [super viewDidLoad];
}

-(IBAction)convertMetod:(id)sender
{
    Calculation * CalculationObj = [[Calculation alloc] init];
    CalculationObj.delegate = self;
    [CalculationObj convertCelToFaher:[inputField.text intValue]];

}
-(void)showTheAns:(NSString *)ans
{
    NSLog(@"delegate method called");
    msgtext.text = ans;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end

Calculation.h

 #import <Foundation/Foundation.h>
@protocol CalculationDelegate <NSObject>
@required
-(void) showTheAns:(NSString *) ans;
@end

@interface Calculation : NSObject
@property (strong,nonatomic) id <CalculationDelegate> delegate;
-(void)convertCelToFaher:(int)celsius;
@end

Calculation.m

#import "Calculation.h"
@implementation Calculation
@synthesize delegate;
-(void)convertCelToFaher:(int)celsius
{

    float fah = ((1.8)*celsius)+32;

    [delegate showTheAns:[NSString stringWithFormat:@"the %i deg Celsius             equal to %.2f Fah",celsius,fah]];
}
@end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM