UIButton setTitle切换,自动重载?

时间:2013-08-18 10:33:33

标签: ios objective-c uibutton

我有按钮,bool作为切换。每当我更改切换时,我希望标题发生变化。但每当我更改切换时,标题不会自动更改。有人可以告诉我该怎么做吗?

代码:

@interface OrderController ()
{
    std::string currentValue;
    bool buttonToggle;
    NSString *orderNumber;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    buttonToggle = true;
}

- (IBAction)button1:(id)sender
{
    if (buttonToggle)
    {
        [sender setTitle:@"1" forState:UIControlStateNormal];
        currentValue += "1";
        orderNumber = [NSString stringWithCString:currentValue.c_str() encoding:NSUTF8StringEncoding];
    }
    else
    {
        [sender setTitle:@"A" forState:UIControlStateNormal];
        orderNumber = [NSString stringWithFormat:@"%@%@", orderNumber, @"A"];
        buttonToggle = true;
    }
}

- (IBAction)swipeUp:(id)sender
{
    buttonToggle = false;
}

1 个答案:

答案 0 :(得分:0)

尝试这样做:

- (IBAction)button1:(UIButton *)sender  //line was changed
{
if (buttonToggle)
{
    sender.titleLabel.text=@"1"; //line was changed
    currentValue += "1";
    orderNumber = [NSString stringWithCString:currentValue.c_str() encoding:NSUTF8StringEncoding];
}
else
{
    sender.titleLabel.text=@"A"; //line was changed
    orderNumber = [NSString stringWithFormat:@"%@%@", orderNumber, @"A"];
    buttonToggle = true;
}
}