我几个小时以来一直困在一个问题上,似乎无法解决这个问题。我是Objective-C的新手,我真的很烦我不能得到这个。我想在NSLog
中显示bmiLabel
值,但我的bmiLabel始终显示NULL
。
这是我的代码: ViewController.h:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UITextField *num1;
@property (strong, nonatomic) IBOutlet UITextField *num2;
@property (strong, nonatomic) IBOutlet UILabel *resultLabel;
@property (strong, nonatomic) IBOutlet UILabel *bmiLabel;
- (IBAction)bmiButton:(id)sender;
@end
ViewController.m:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize num1, num2, resultLabel, bmiLabel;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (IBAction)bmiButton:(id)sender
{
float sum = [self.num2.text floatValue] / ([self.num1.text floatValue] *
[self.num1.text floatValue]);
NSString *Output = nil;
Output = [[NSString alloc] initWithFormat:@"%.2f", sum];
self.resultLabel.text = Output;
NSString *mystring;
NSString * bmi = [[NSString alloc] NSStringWithFormat:@"%@", mystring];
self.bmiLabel.text = bmi;
if(sum <= 18.5)
{
NSLog(@"You are underweight", mystring);
}
if(sum <= 18.5)
{
NSLog(@"you are normal");
}
if (sum <= 25.0)
{
NSLog(@"You are overweight");
}
if(sum <= 30)
{
NSLog(@"You are obese");
}
}
@end
答案 0 :(得分:1)
嗯,试着成为一个小小的通灵者,我注意到你实际上没有在BMI案件中加入任何东西。试试这个:
- (IBAction)bmiButton:(id)sender {
float sum = [self.num2.text floatValue] / ([self.num1.text floatValue] *
[self.num1.text floatValue]);
NSString *Output = [NSString stringWithFormat:@"%.2f", sum];
self.resultLabel.text = Output;
NSString *mystring= nil;
if (sum <= 18.5) {
mystring = @"You are underweight";
} else if (sum <= 25.0) {
mystring = @"you are normal";
} else if (sum <= 30.0){
mystring = @"You are overweight";
} else {
mystring = @"You are obese:";
}
self.bmiLabel.text = mystring;
}
答案 1 :(得分:0)
我不知道你在问什么,但这里有一个错误:
NSString *mystring;
NSString * bmi = [[NSString alloc] NSStringWithFormat:@"%@", mystring];
self.bmiLabel.text = bmi;
首先,你的字符串格式设置器:
[[NSString alloc] NSStringWithFormat:@"someformat: %@", someVariable];
应该是:
[NSString stringWithFormat:@"someformat: %@", someVariable];
但是,如果这些是正确的,这仍然会有这个问题:
NSString * bmi = [NSString stringWithFormat:@"%@", mystring];
当mystring已经是一个字符串时,这很奇怪。它适用于:
NSString * bmi = mystring;
基本上,这三行是等同于:
NSString * mystring;
self.bmiLabel.text = mystring;
问题是,你从来没有为mystring分配值
答案 2 :(得分:0)
我认为[[NSString alloc] NSStringWithFormat:@"%@", mystring];
代码存在问题。请尝试下面的代码
NSString *mystring;
NSString * bmi = [NSString stringWithFormat:@"%@", mystring];
self.bmiLabel.text = bmi;
此外,您必须初始化mystring
。如果你不mystring
总是返回null。
答案 3 :(得分:0)
此代码无用,因为bmi将是一个空字符串。所以bmiLabel将是空的。
NSString *mystring;
NSString * bmi = [[NSString alloc] NSStringWithFormat:@"%@", mystring];
self.bmiLabel.text = bmi;
你的其余代码很好。虽然在我看来你在代码中使用了太多的对象。例如,您的NSString不是必需的,您可以写这个
float sum = [self.num2.text floatValue] / ([self.num1.text floatValue] *
[self.num1.text floatValue]);
self.resultLabel.text = [[NSString alloc] initWithFormat:@"%.2f", sum];
答案 4 :(得分:0)
此:
[[NSString alloc] NSStringWithFormat:@“%@”,mystring];
StringWithFormat是一个将发送给类的类方法:
string = [NSString stringWithFormat:@"some string"];
这告诉类给你一个已经分配和初始化的字符串。
在你的情况下你应该(可以)使用:
string = [[NSString alloc]initWithFormat:@"some string"];
另外,关于代码中的这一行:
NSString * bmi = [[NSString alloc] NSStringWithFormat:@"%@", mystring];
你可以这样做:
[[self bmiLabel]setText:[[NSString alloc]initWithString:mystring]];
或者如果您更喜欢点符号
self.bmiLabel.text = [[NSString alloc]initWithString:mystring]];