あなたのIBOutlet接続に問題があると思われます。以下のコードは問題なく動作します:
@interface testViewController : UIViewController {
UITextField *tf1;
UITextField *tf2;
UILabel *result;
}
@end
@implementation testViewController
- (void)dealloc{
[tf1 release];
[tf2 release];
[result release];
[super dealloc];
}
-(void) click:(id) obj {
float firstFloat = [tf1.text floatValue];
float secondFloat = [tf2.text floatValue];
float answer = secondFloat/firstFloat * 100;
result.text = [NSString stringWithFormat:@"%.1f%%",answer];
}
-(void) viewWillAppear:(BOOL)animated {
tf1 = [[UITextField alloc]initWithFrame:CGRectMake(10, 10, 200, 30)];
tf1.backgroundColor = [UIColor whiteColor];
tf2 = [[UITextField alloc]initWithFrame:CGRectMake(10, 50, 200, 30)];
tf2.backgroundColor = [UIColor whiteColor];
result = [[UILabel alloc]initWithFrame:CGRectMake(10, 90, 200, 30)];
result.backgroundColor = [UIColor whiteColor];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(10, 130, 50, 30);
[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[self.view addSubview:tf1];
[self.view addSubview:tf2];
[self.view addSubview:result];
}
@end
firstFloatの値は何ですか?私はそれがゼロだと思う。 – taskinoor
"IF"ステートメントの何らかの形式を使用する必要があるのだろうかと思います。 – TWcode