嗨,一个非常简单的应用程序,它通过 2 个文本框接收 2 个参数,然后将它们汇总并显示在名为 result 的标签中。这个想法是通过一个称为大脑的对象来处理它,在后面的部分中我已经给出了代码。问题是 foo 为零,当您单击按钮时,结果一无所获。计划是使用它为我已完成的更大应用程序构建更好的模型视图架构。
#import "calbrain.h"
#import "ImmyViewController.h"
@interface ImmyViewController ()
@property (nonatomic, strong) calbrain *brain;
@end
@implementation ImmyViewController
@synthesize brain;
@synthesize num1;
@synthesize num2;
@synthesize result;
-(calbrain *) setBrain
{
if (!brain) {
brain = [[calbrain alloc] init];
}
return brain;
}
- (IBAction)kickit:(UIButton *)sender {
NSString *number1 = self.num1.text;
NSString *number2 = self.num2.text;
NSString *foo;
foo = [brain calculating:number1 anddouble:number2];
self.result.text = foo;
// self.result.text = [brain calculating:self.num1.text anddouble:self.num2.text];
}
@end
@implementation calbrain
-(NSString *) calculating:(NSString *)number1 anddouble:(NSString *)number2
{
double numb1 = [number1 doubleValue];
double numb2 = [number2 doubleValue];
double newresult = (numb1 + numb2);
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
NSString *numberAsString = [numberFormatter stringFromNumber:[NSNumber n numberWithFloat:newresult]];
return numberAsString;}