ios - My Delegate method is not getting Called -
i started learning objective-c, breaking head why below simple code protocols , delegates not working, please clarify me why delegate method not getting called. 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 resources can recreated. } -(void)converttofahrenheit:(int)celsius{ float fah = ((1.8)*celsius)+32; msgtext.text = [nsstring stringwithformat:@"the %i deg celsius equal %.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]; // additional setup after loading view. } - (ibaction)convert:(id)sender { [delegate converttofahrenheit:[textfield.text intvalue]]; //[self dismissviewcontrolleranimated:yes completion:nil]; } @end
i totally confused why delegate method not getting called. newbie objective c please me.
i hope following code fulfil 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 resources can 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 %.2f fah",celsius,fah]]; } @end
Comments
Post a Comment