2010-12-09 45 views
0

对于这样一个愚蠢的问题抱歉抱歉 我正在改变一个变量值,这取决于分段控件的索引,但是随后想在随后的计算中使用这个变量;我确定这与变量范围有关?关于传递实例变量的真正基本的Obj-C问题

- (IBAction)calculate:(UIButton *)button { 
if([sSeg selectedSegmentIndex]==1){ 
    float s=0.5; 
    NSLog(@"s=%f", s); 
} 
else if ([sSeg selectedSegmentIndex]==0) 
{ 
    float s=1; 
    NSLog(@"s=%f", s); 
} 
NSLog(@”s now = %f”, s); 

}

帮助非常感谢!

+0

只需在你的接口文件中的花括号如float s之间声明你的float var;并在您的IBAction方法(和其他任何地方)中,您可以直接访问它。 – Rog 2010-12-09 01:40:28

回答

4
- (IBAction)calculate:(UIButton *)button { 
    float s = 0; 
    if([sSeg selectedSegmentIndex]==1){ 
     s=0.5; 
     NSLog(@"s=%f", s); 
    } 
    else if ([sSeg selectedSegmentIndex]==0) 
    { 
     s=1; 
     NSLog(@"s=%f", s); 
    } 
    NSLog(@”s now = %f”, s); 

}

呀,它的范围 - 变量只是你的大括号内可见。

0
- (IBAction)calculate:(UIButton *)button { 
    float s; 
    if([sSeg selectedSegmentIndex]==1){ 
     s=0.5; 
     NSLog(@"s=%f", s); 
    } 
    else if ([sSeg selectedSegmentIndex]==0) 
    { 
     s=1; 
     NSLog(@"s=%f", s); 
    } 
    NSLog(@”s now = %f”, s); 
} 
+0

是的,这就是我的想法。我试图在if语句之外声明float(如上面的答案),但它仍然不能在if语句之下访问。我得到本地声明的s隐藏实例变量警告。 – zed111 2010-12-09 01:48:42