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의 가치는 무엇입니까? 나는 이것이 0 인 것으로 생각한다. – taskinoor
"IF"구문의 일부 형식을 사용해야하는지 궁금합니다. – TWcode