UIButton每次都不像预期的那样

时间:2011-12-16 20:21:34

标签: iphone objective-c ios

我找不到我创建的两个按钮无法使用的功能。如果我的NSString中有数据

,按钮将按预期工作
plantingEventData.seedingMethod

(如果它包含“常规”或“VR”)。但如果变量中有任何其他值,则该按钮不执行任何操作。该方法仍然被调用,并将进入按钮方法中的if语句,但不会更改属性。它几乎就像按钮没有连接UIButton。但如果变量中包含“常规”或“VR”字符串,则它可以完美运行。

plantingEventData.seedingMethod = @"conventional" or @"VR"

plantingEventData.seedingMetod = any other value

我创建两个按钮,这些按钮将设置为一个或两个都不被选中:

UIButton *conventionalSeedingButton = [UIButton new];
conventionalSeedingButton.frame = CGRectMake(200, ((i * 40) + 143), 20, 20);
conventionalSeedingButton.tag = 1;
[conventionalSeedingButton addTarget:self action:@selector(conventionalSeedingMethodBtn:) forControlEvents:UIControlEventTouchUpInside];
if ([plantingEventData.seedingMethod isEqualToString:@"conventional"])
{
    NSLog(@"convention button is set to true");
    [conventionalSeedingButton setImage:[UIImage imageNamed:@"radio-btn-selected.png"] forState:UIControlStateNormal];
    conventionalSeedingButton.selected = TRUE;
}
else
{
    NSLog(@"convention button is set to false");

    [conventionalSeedingButton setImage:[UIImage imageNamed:@"radio-btn.png"] forState:UIControlStateNormal];
    conventionalSeedingButton.selected = FALSE;
}

UIButton *VRSeedingButton = [UIButton new];
VRSeedingButton.frame = CGRectMake(200, ((i * 40) + 143), 20, 20);
VRSeedingButton.tag = 2;
[VRSeedingButton addTarget:self action:@selector(VRSeedingMethodBtn:) forControlEvents:UIControlEventTouchUpInside];
[VRSeedingButton setImage:[UIImage imageNamed:@"radio-btn.png"] forState:UIControlStateNormal];
if ([plantingEventData.seedingMethod isEqualToString:@"VR"])
{
    NSLog(@"VR button is set to true");

    [VRSeedingButton setImage:[UIImage imageNamed:@"radio-btn-selected.png"] forState:UIControlStateNormal];
    VRSeedingButton.selected = TRUE;
}
else
{
    NSLog(@"VR button is set to false");

    [VRSeedingButton setImage:[UIImage imageNamed:@"radio-btn.png"] forState:UIControlStateNormal];
    VRSeedingButton.selected = FALSE;
}

按钮调用的方法:

-(IBAction)conventionalSeedingMethodBtn:(id)sender
{        
    NSLog(@"self.conventionalSeedingMethodButton.selected: %@", self.conventionalSeedingMethodButton.selected ? @"YES" : @"NO");
    if (self.conventionalSeedingMethodButton.selected)
    {
        NSLog(@"switching from true to false");
        [self.conventionalSeedingMethodButton setImage:[UIImage imageNamed:@"radio-btn.png"] forState:UIControlStateNormal];
        self.conventionalSeedingMethodButton.selected = FALSE;
    }
    else
    {
        NSLog(@"switching from false to true");
        [self.conventionalSeedingMethodButton setImage:[UIImage imageNamed:@"radio-btn-selected.png"] forState:UIControlStateNormal];
        self.conventionalSeedingMethodButton.selected = TRUE;
    }

    [self.VRSeedingMethodButton setImage:[UIImage imageNamed:@"radio-btn.png"] forState:UIControlStateNormal];
    self.VRSeedingMethodButton.selected = FALSE;

}

-(IBAction)VRSeedingMethodBtn:(id)sender
{        
    NSLog(@"self.VRSeedingMethodButton.selected: %@", self.VRSeedingMethodButton.selected ? @"YES" : @"NO");

    if (self.VRSeedingMethodButton.selected)
    {
        NSLog(@"switching from true to false");
        [self.VRSeedingMethodButton setImage:[UIImage imageNamed:@"radio-btn.png"] forState:UIControlStateNormal];
        self.VRSeedingMethodButton.selected = FALSE;
    }
    else
    {
        NSLog(@"switching from false to true");
        [self.VRSeedingMethodButton setImage:[UIImage imageNamed:@"radio-btn-selected.png"] forState:UIControlStateNormal];
        self.VRSeedingMethodButton.selected = TRUE;
    }

    [self.conventionalSeedingMethodButton setImage:[UIImage imageNamed:@"radio-btn.png"] forState:UIControlStateNormal];
    conventionalSeedingMethodButton.selected = FALSE;
}

我很困惑=?

1 个答案:

答案 0 :(得分:0)

您的代码有点令人困惑。看起来你在Interface Builder中定义了一些东西以及你以编程方式创建的东西。

如果您打算使用IB:

  1. 不要以编程方式创建新按钮,并确保正确配置IB按钮。如果要更改/设置任何属性,可以在viewDidLoad中配置按钮。
  2. 如果您的目的是以编程方式创建/配置按钮:

    1. 您的按钮未添加到视图中。这意味着您在应用中看到的任何按钮必须已添加到其他位置(可能是IB)。

    2. 您没有将按钮分配给iVars。这没关系,但您在事件处理程序中指的是iVars。如果没有iVars,请通过其标签属性检索按钮。