2011-09-26 84 views
0

我想使用一些逻辑的按钮click.i使用按钮作为复选标记,当我点击这个复选标记按钮的图像变成checked.png或unchecked.png.On那个基础上我使用这个代码...如何使用其他条件按钮上的按钮图像按钮?

if([UIImage imageNamed:@"checkbox_ticked.png"]) 
    { 
     isActiveStr=[arrayPickerData objectAtIndex:17]; 
     NSLog(@" is active value is %@ ",isActiveStr); 
     isActiveStr=nil; 

    } 
    else 
    { 
     NSLog(@"no vlaue send"); 
     isActiveStr=[[NSMutableString alloc]initWithString:@"1"]; 
     NSLog(@" is active value %@",isActiveStr); 
    } 

现在我不知道如何利用别人condtion ...我运行此代码,但它始终只运行,如果codition.It在其他conditon.I从不希望我codtion是事实,就如果部分进入,并且条件为false,则进入其他部分。以及如何使用按钮的图像属性来检查条件。

回答

0

试试这个if声明:

if ([[myButton imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:@"checkbox_ticked.png"]]) { 
... 

但对于我来说这是不错的办法,不如有一个指示按钮被选中一些布尔标志。

1

你必须有一个标志变量来保存按钮的状态(选中/未选中)。

BOOL checked; 

而且,你有当过你点击该按钮来更新它。

- (void)onButtonTapped:(UIButton *)button { 

    checked = !checked; 
    ... 
} 

,改变你的if语句这样,

if (checked) { 

    // The button is checked 

} else { 

    // The button is not checked 
} 
0

如果条件计算结果始终如此。您应该检查[myButton状态]以查看它是否已启用。您可能需要根据您的需求添加IBOutlet或IBAction。

0

使用这个在您的IBAction为方法

if(!btnCheckbox.selected) 
    { 
     [btnCheckbox setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"check-out_h" ofType:@"png"]] forState:UIControlStateNormal]; 
     btnCheckbox.selected=YES; 
    } 
    else { 
     [btnCheckbox setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"check-out" ofType:@"png"]] forState:UIControlStateNormal]; 
     btnCheckbox.selected=NO; 
    } 
0
-(void)checkBoxIsHideNationWideClicked 
{ 
    NSLog(@"nation wide button clicked"); 

    mybtn.selected=!mybtn.selected; 
    defaultsCheckBox=[NSUserDefaults standardUserDefaults]; 

    if (mybtn.selected) 
    { 
     [defaultsCheckBox setValue:@"One" forKey:@"CheckBox"]; 

     NSLog(@" is active value is %@ is fetched succesfuly",hideNationWideStr); 
     [CommonVar setHideNationwideData:hideNationWideStr]; 
     NSLog(@" is active value send to cmmon class %@ sent succesfuly",hideNationWideStr); 
    } 
    else 
    { 
     [defaultsCheckBox setValue:@"Two" forKey:@"CheckBox"]; 
     NSLog(@"no vlaue send"); 
     hideNationWideStr=[[NSMutableString alloc]initWithString:@"1"]; 
     [CommonVar setHideNationwideData:hideNationWideStr]; 
     NSLog(@" is active value send to cmmon class %@ sent succesfuly",hideNationWideStr); 
    } 
}