2012-11-21 45 views
0

只有部分如果我有一个方法叫调用一个方法

-(void) changeButton:(UIButton *)button andAlpha:(float)alpha andEnabled:(BOOL)enabled { 
    button.alpha = alpha; 
    button.enabled = enabled;  
} 

反正我只能拨打[self changeButton:buttonName andAlpha:0.5];和错过andEnabled(BOOL)启用,使其将保持相同的值。

+2

否。创建一个不同的方法,遗漏代码。 – trojanfoe

回答

2

不,只有当你声明其他方法。

-(void) changeButton:(UIButton *)button andAlpha:(float)alpha { 
    [self changeButton:button andAlpha:alpha andEnabled:button.enabled]; 
} 

-(void) changeButton:(UIButton *)button andAlpha:(float)alpha andEnabled:(BOOL)enabled { 
    button.alpha = alpha; 
    button.enabled = enabled;  
} 

但要记住,这个方法并不总是好的。例如启用属性可以通过一些自定义setter来备份,即使您不想更改该值,该属性也会被调用。

0

你不能这样做,你必须声明另一种方法。

-(void) changeButton:(UIButton *)button andAlpha:(float)alpha; 
0

我相信你问的是C++的默认参数化函数。

但是Objective-C不支持这个。

您可以创建,虽然2种方法:

-(void) changeButton:(UIButton *)button andAlpha:(float)alpha { 
    button.alpha = alpha; 
} 

-(void) changeButton:(UIButton *)button andAlpha:(float)alpha andEnabled:(BOOL)enabled { 
    button.alpha = alpha; 
    button.enabled = enabled;  
} 

对于C:有使用ObjC加入到C子集没什么特别的。任何无法在纯C中完成的事情都无法通过在ObjC中进行编译来完成。这意味着,您不能拥有默认参数,也不能重载一个函数。改为创建2个功能。

一种替代方式(位冗长,因为有人会很少使用)是有一个标志,并检查标志是/否。

-(void) changeButton:(UIButton *)button andAlpha:(float)alpha andEnabled:(BOOL)enabled withFlag:(BOOL)flag{ 
    button.alpha = alpha; 
    if(flag){   
     button.enabled = enabled;  
    } 
}