我的委托方法没有被调用

时间:2015-04-25 11:38:10

标签: ios objective-c delegates protocols

我刚刚开始学习objective-c,我为什么在下面简单的协议和代理代码不起作用时为什么会这样,请澄清为什么这是委托方法没有被调用。提前谢谢。

ViewController.h

#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

ViewController.m

#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.h

#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

secondViewController.m

{{1}}

我完全搞砸了为什么我的委托方法没有被调用。我是Objective c的新手请帮助我。

1 个答案:

答案 0 :(得分:0)

我希望以下代码能满足您的要求。

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
相关问题