【问题标题】:Xcode Buttons to Add Numbers to a LabelXcode 按钮将数字添加到标签
【发布时间】:2011-08-10 17:15:19
【问题描述】:

我正在 Xcode 中创建一个拨号盘应用程序(这与 iPhone Phone 应用程序非常相似)并且一切正常,但按钮链接到标签。

将某个数字添加到标签上其他数字的末尾的代码是什么(如果有意义的话!)?该数字将基于标签的标题(因此,如果按钮是数字 6 或数字 2 等,它将自动计算出来......)。

我需要输入数字的标签叫做 phoneNumberLabel。

这是我正在使用的代码(不起作用);

-(void)displayPhoneNumber
{
self.phoneNumberLabel.text = self.phoneNumberString;  

NSLog(@"Displayed Phone Number");
}

-(IBAction)numberButtonPressed:(UIButton *)pressedButton
{
int toneIndex = [pressedButton.titleLabel.text intValue];
SystemSoundID toneSSID = toneSSIDs[toneIndex];
AudioServicesPlaySystemSound(toneSSID);

self.phoneNumberString = [self.phoneNumberString stringByAppendingString:pressedButton.titleLabel.text];
[self displayPhoneNumber];

NSLog(@"Number Pressed");
}

【问题讨论】:

    标签: objective-c xcode phone-number


    【解决方案1】:

    你应该试试:

    NSMutableArray *pressedNumbers = [NSMutableArray alloc];
    [pressedNumbers addObject:self.phoneNumberLabel.text];
        //THEN
    NSMutableString *numberComplete = [NSMutableString alloc];
    for (NSString *temp in pressedNumbers) {
        [numberComplete appendString:temp];
    }
    

    如果我明白你想做什么

    【讨论】:

    • 嗨,是的,但它是否不需要获取按钮标题才能确定要添加到标签的数字,就像我上面的代码一样? :-)
    • P.S.无论如何它都没有用,当我点击一个按钮时出现了一个 NS 错误:-(
    • 呃,你有 10 个按钮吗?每个数字一个?
    • 确实,它们都有按钮标题(例如 1、2、3..)。请参阅我当前的代码以供参考(上图)。
    • 我找到了解决方案,看看(Pastie.org 很酷;))[link]pastie.org/private/q2md5vkbi5oaktotoeew
    猜你喜欢
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 2022-01-14
    • 2017-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多