If using Xcode to make an iOS calculator, how would I add a decimal button?

孤街醉人 提交于 2019-12-02 10:27:13

You're not giving me much information in your question. How do the other buttons work?

If I was making a calculator I would have a label at the top showing the current reading. On press of a number button I would update the label with the number at the end.

For a decimal button you just add a . to the end of the label. You might want to have a global variable BOOL hasDecimalPlace and set it to true so you know if there is already a decimal place. Just remember to set it to false again when you clear the view or do a calculation or similar.

- (IBAction)Decimal:(id)sender{
    NSString *currentText = Text.text;
    if ([currentText rangeOfString:@"." options:NSBackwardsSearch].length == 0) {
        Text.text = [Text.text stringByAppendingString:@"."];  
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!