【问题标题】:transferring floats to a different class?将浮点数转移到不同的类?
【发布时间】:2011-02-19 12:36:49
【问题描述】:

我有一个问题,我一直在寻找解决方案,但似乎找不到可行的解决方案。 UserInput.xib , Calculations.h & .m ,DataOutput.xib ...该计算没有 .xib 。

UserInput.h

DataOutput *dataOutput;
UITextField *tf1;
UITextField *tf2;
UITextField *tf3;

@property (nonatomic, retain) DataOutput *dataOutput;
@property (nonatomic, retain) IBOutlet UITextField *tf1;
@property (nonatomic, retain) IBOutlet UITextField *tf2;
@property (nonatomic, retain) IBOutlet UITextField *tf3;

UserInput.m

@synthesize dataOutput;
@synthesize tf1;
@synthesize tf2;
@synthesize tf3;

- (IBAction)calculate:(id)sender {
DataOutput *dataOut = [[DataOutput alloc] initWithNibName:@"DataOutput" bundle:nil];

Calculations *calc = [[Calculations alloc] init];
dataOut.dataCalc = calc;
dataOut.dataCalc.tf1 = tf1.text;
dataOut.dataCalc.tf2 = tf2.text;
dataOut.dataCalc.tf3 = tf3.text;

dataOut.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:dataOut animated:YES];
[dataOut release];

DataOutput.h

Calculations *dataCalc;
UILabel *results1;
UILabel *results2;
UILabel *results3;

@property (nonatomic, retain) Calculations *dataCalc;
@property (nonatomic, retain) IBOutlet UILabel *results1;
@property (nonatomic, retain) IBOutlet UILabel *results2;
@property (nonatomic, retain) IBOutlet UILabel *results3;

DataOutput.m

@synthesize results1;
@synthesize results2;
@synthesize results3;

- (void)viewDidLoad {
self.results1.text = dataCalc.tf1;
self.results2.text = dataCalc.tf2;
self.results3.text = dataCalc.tf3;

Calculations.h

NSString *tf1;
NSString *tf2;
NSString *tf3;

NSString *results1;
NSString *results2;
NSString *results3;

float *tempResults1;
float *tempResults2;
float *tempResults3;

@property (nonatomic, retain) NSString *tf1;
@property (nonatomic, retain) NSString *tf2;
@property (nonatomic, retain) NSString *tf3;

@property (nonatomic, retain) NSString *results1;
@property (nonatomic, retain) NSString *results2;
@property (nonatomic, retain) NSString *results3;

- (float)getResults1;
- (float)getResults2;

Calculations.m

@synthesize tf1;
@synthesize tf2;
@synthesize tf3;

@synthesize results1;
@synthesize results2;
@synthesize results3;

- (float) getResults1 {
 float temp1 = [tf1 floatValue];
 float temp2 = [tf2 floatValue];

if (temp1 <= 1 && temp2 >= 3) {
  if (temp2 ==10) {tempResults1 = 50;}
  else if (temp2 == 11) {tempResults1 = 52;}

等等等等 好的,这是我的问题..通过我的设置方式,我可以从 userInput 携带数据,抛出计算并将它们显示在 DataOutput 的标签中。但当 使用我在calculation.h 中声明的那些if 语句中的浮点数...我无法将浮点数(实际数据计算)带到DataOutput 屏幕。在 DataOutput 上,当我尝试从它无法识别的计算中将其设置为浮点数时,它会识别 NSString 但不会识别浮点数。我尝试将 float tempResults1 转换为 NSString results1,但我不断收到错误消息。我已经尝试了几种不同的方法来从这里的不同问题和答案中做到这一点,但无法弄清楚为什么它不起作用。谁能帮我解决这个问题?

我想做的是能够在数据输出屏幕上显示计算结果。

我知道这必须是一些简单的事情,也许我做错了什么或忽略了一些我没有做的事情......我不知道,虽然我知道这么多,但我可以使用一些指导。

【问题讨论】:

    标签: iphone objective-c cocoa-touch


    【解决方案1】:
    if (temp1 <= 1 && temp2 >= 3) // this is ok
    {
      if (temp2 ==10) { // exact floating point comparisons are dangerous,
                        // and should be avoided. you must rewrite this.
                        // turn up your compiler warnings
    
            tempResults1 = 50;     // this is not what you think it is.
                                   // turn up your compiler warnings. you 
                                   // are assigning the address for the
                                   // pointer's value. this will lead to a
                                   // crash after you use it. what exactly
                                   // are you trying to accomplish with this
                                   // statement? this must be rewritten.
        } 
      else if (temp2 == 11) {tempResults1 = 52;} // as above
    

    【讨论】:

      猜你喜欢
      • 2011-03-15
      • 1970-01-01
      • 2011-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多