按钮不是第一次切换

时间:2011-03-17 13:29:09

标签: iphone objective-c

我有一个按钮,这是第一次没有取消选中时切换......

我必须按两次使其切换后才能正常工作.... 我的代码如下......

-(IBAction)toggleAutomaticlogin:(id)sender
{
    if([sender tag]==1)
    {
        [sender setImage:[UIImage imageNamed:@"ckbox_selected.png"] forState:UIControlStateNormal];
        [sender setTag:0];
    }           
    else if([sender tag]==0)
    {
        [sender setImage:[UIImage imageNamed:@"ckbox.png"] forState:UIControlStateNormal];
        [sender setTag:1];          
    }
}

第一次点击两次后效果很好......

2 个答案:

答案 0 :(得分:1)

如果您不想弄乱标签,也可以执行类似

的操作
-(IBAction)toggleAutomaticlogin:(id)sender
{
    if([sender currentImage]== [UIImage imageNamed:@"ckbox.png"])
    {
        [sender setImage:[UIImage imageNamed:@"ckbox_selected.png"] forState:UIControlStateNormal];
    }
    else
    {
        [sender setImage:[UIImage imageNamed:@"ckbox.png"] forState:UIControlStateNormal];
    }
}

答案 1 :(得分:0)

请查看以下代码。

-(IBAction)toggleAutomaticlogin:(id)sender
{
    if([sender tag]== 0)
    {
        [sender setImage:[UIImage imageNamed:@"ckbox_selected.png"] forState:UIControlStateNormal];
        [sender setTag:1];


    else if([sender tag]==1)
    {
        [sender setImage:[UIImage imageNamed:@"ckbox.png"] forState:UIControlStateNormal];
        [sender setTag:0];

    }
}