Iphone 问题1:加入一个“$&引用;给小费计算器

Iphone 问题1:加入一个“$&引用;给小费计算器,iphone,objective-c,cocoa-touch,Iphone,Objective C,Cocoa Touch,我正在尝试在我的应用程序的提示结果中添加“$”,但遇到了一些困难。我在Xcode和Objective-C旁边,我不确定需要将符号放在哪里,因为它会导致错误。下面是我的.m代码的副本 非常感谢您的帮助 #import "tipcalcViewController.h" @implementation tipcalcViewController - (void)dealloc { [super dealloc]; } - (IBAction)aSliderChanged:(id)se

我正在尝试在我的应用程序的提示结果中添加“$”,但遇到了一些困难。我在Xcode和Objective-C旁边,我不确定需要将符号放在哪里,因为它会导致错误。下面是我的.m代码的副本

非常感谢您的帮助

#import "tipcalcViewController.h"

@implementation tipcalcViewController


- (void)dealloc
{

    [super dealloc];
}
- (IBAction)aSliderChanged:(id)sender {
    UISlider *slider = (UISlider *)sender;
    if (slider == tipslide) {
        NSString *tip = [NSString stringWithFormat:@"%.2f", slider.value * 100];
        int tipPercentage = [tip intValue];
        NSString *multiplier;
        if (tipPercentage < 10) {
            multiplier = [NSString stringWithFormat:@"1.0%i", tipPercentage];
        }
        else if (tipPercentage >= 10) {
            multiplier = [NSString stringWithFormat:@"1.%i", tipPercentage];
        }
        [costWithTipLabel setText:[NSString stringWithFormat:@"%.2f", ([[costWithoutTip text] intValue] * [multiplier floatValue])]];
        [tipTextLabel setText:[[NSString stringWithFormat:@"Tip(%i", tipPercentage] stringByAppendingString:@"%):"]];
        [self performSelector:@selector(updateNumOfPeop)];
    }
    else if (slider == peopleslide) {
        NSString *p = [NSString stringWithFormat:@"%.f", slider.value*10];   
        int numberOfPeople = [p intValue];
        [numberOfPeopleTextLabel setText:[NSString stringWithFormat:@"Each(%i):", numberOfPeople]];
        [numberOfPeopleLabel setText:[NSString stringWithFormat:@"%.2f", [[costWithTipLabel text] floatValue]/numberOfPeople]];
    }
    [totalBillCost setText:[costWithTipLabel text]];
}
- (void)updateNumOfPeop {
    NSString *p = [NSString stringWithFormat:@"%.f", peopleslide.value*10];   
    int numberOfPeople = [p intValue];
    [numberOfPeopleTextLabel setText:[NSString stringWithFormat:@"Each(%i):", numberOfPeople]];
    [numberOfPeopleLabel setText:[NSString stringWithFormat:@"%.2f", [[costWithTipLabel text] floatValue]/numberOfPeople]];
}
/*
- (IBAction)aSliderChanged:(id)sender {
    UISlider *slider = (UISlider *)sender;
    if (slider == tipslide) {
        NSString *tip = [NSString stringWithFormat:@"%.f", slider.value * 100];
        float tipPercentage = [tip floatValue];
        NSString *multiplier = [NSString stringWithFormat:@"1.%.f", tipPercentage];
        [costWithTipLabel setText:[NSString stringWithFormat:@"%.2f", [[costWithoutTip text] floatValue] * [multiplier floatValue]]];
        [tipTextLabel setText:[[NSString stringWithFormat:@"Tip (%.f", slider.value *100]stringByAppendingString:@"%):"]];
    }
    else if (slider == peopleslide) {
        NSString *p = [NSString stringWithFormat:@"%.f", slider.value*10];
        float numOfPeople = [p floatValue];
        [numberOfPeopleTextLabel setText:[NSString stringWithFormat:@"Each (%.f):", numOfPeople]];
        [numberOfPeopleLabel setText:[NSString stringWithFormat:@"%.2f", [[costWithTipLabel text] floatValue]/numOfPeople]];
    }
    [totalBillCost setText:[NSString stringWithFormat:@"%.2f", [[costWithTipLabel text] floatValue]]];
}
*/
- (void)viewDidLoad {
    [costWithoutTip becomeFirstResponder];
    [costWithoutTip setDelegate:self];
    [costWithoutTip setKeyboardType:UIKeyboardTypeNumbersAndPunctuation];
    [tipslide setValue:0.01];
    [peopleslide setValue:0.1];
    [super viewDidLoad];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
    [costWithoutTip setText:[NSString stringWithFormat:@"%.2f", [costWithoutTip text].floatValue]];
    [costWithoutTip resignFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [costWithoutTip resignFirstResponder];
    return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    [costWithoutTip resignFirstResponder];
    return YES;
}
- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}
*/

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
#导入“tipcalcViewController.h”
@tipcalcViewController的实现
-(无效)解除锁定
{
[super dealoc];
}
-(iAction)aSliderChanged:(id)发送方{
UISlider*滑块=(UISlider*)发送器;
如果(滑块==倾斜滑块){
NSString*tip=[NSString stringWithFormat:@“%.2f”,slider.value*100];
int-tipPercentage=[tip-intValue];
NSString*乘法器;
如果(倾斜度<10){
乘数=[NSString stringWithFormat:@“1.0%i”,TipperEntage];
}
否则,如果(tipPercentage>=10){
乘数=[NSString stringWithFormat:@“1.%i”,TipperEntage];
}
[costWithTipLabel setText:[NSString stringWithFormat:@“%.2f”,([[CostWithWithOutTip text]intValue]*[multiplier floatValue]);
[tipTextLabel setText:[NSString stringWithFormat:@“Tip(%i”,TipperEntage]stringByAppendingString:@“%”:“];
[自执行选择器:@selector(updateEnumofPeop)];
}
else if(滑块==人滑块){
NSString*p=[NSString stringWithFormat:@“%.f”,slider.value*10];
int numberOfPeople=[p intValue];
[numberOfPeopleTextLabel setText:[NSString stringWithFormat:@“每个(%i):”,numberOfPeople];
[numberOfPeopleLabel setText:[NSString stringWithFormat:@“%.2f”,[[costWithTipLabel text]floatValue]/numberOfPeople]];
}
[totalBillCost setText:[costWithTipLabel text];
}
-(void)updateEnumofPeop{
NSString*p=[NSString stringWithFormat:@“%.f”,peopleslide.value*10];
int numberOfPeople=[p intValue];
[numberOfPeopleTextLabel setText:[NSString stringWithFormat:@“每个(%i):”,numberOfPeople];
[numberOfPeopleLabel setText:[NSString stringWithFormat:@“%.2f”,[[costWithTipLabel text]floatValue]/numberOfPeople]];
}
/*
-(iAction)aSliderChanged:(id)发送方{
UISlider*滑块=(UISlider*)发送器;
如果(滑块==倾斜滑块){
NSString*tip=[NSString stringWithFormat:@“%.f”,slider.value*100];
浮动TipperEntage=[tip floatValue];
NSString*乘数=[NSString stringWithFormat:@“1.%.f”,tipPercentage];
[costWithTipLabel setText:[NSString stringWithFormat:@“%.2f”,[[costWithoutTip text]浮点值]*[乘数浮点值]];
[tipTextLabel setText:[NSString stringWithFormat:@“Tip(%.f”,slider.value*100]stringByAppendingString:@“%”:“];
}
else if(滑块==人滑块){
NSString*p=[NSString stringWithFormat:@“%.f”,slider.value*10];
float numOfPeople=[p floatValue];
[numberOfPeopleTextLabel setText:[NSString stringWithFormat:@“Each(%.f):”,numOfPeople];
[numberOfPeopleLabel setText:[NSString stringWithFormat:@“%.2f”,[[costWithTipLabel text]floatValue]/numOfPeople]];
}
[totalBillCost setText:[NSString stringWithFormat:@“%.2f”,[costWithTipLabel text]floatValue]];
}
*/
-(无效)viewDidLoad{
[无需成为第一响应者的成本];
[CostWithout Tip setDelegate:self];
[无提示设置键盘类型的成本:UIKeyboardTypeNumbers和标点符号];
[tipslide设定值:0.01];
[peopleslide设定值:0.1];
[超级视图下载];
}
-(void)textfielddidediting:(UITextField*)textField{
[costWithoutTip设置文本:[NSString stringWithFormat:@“%.2f”,[costWithoutTip文本].floatValue];
[无TIP辞职的成本第一响应者];
}
-(BOOL)textField应返回:(UITextField*)textField{
[无TIP辞职的成本第一响应者];
返回YES;
}
-(BOOL)textField应取消编辑:(UITextField*)textField{
[无TIP辞职的成本第一响应者];
返回YES;
}
-(无效)未收到记忆警告
{
//如果视图没有superview,则释放该视图。
[超级记忆警告];
//释放所有未使用的缓存数据、图像等。
}
#pragma标记-视图生命周期
/*
//实现viewDidLoad以在加载视图(通常从nib)后执行附加设置。
-(无效)viewDidLoad
{
[超级视图下载];
}
*/
-(无效)视图卸载
{
[超级视频下载];
//释放主视图的所有保留子视图。
//例如,self.myOutlet=nil;
}
-(布尔)应自动旋转指针面定向:(UIInterfaceOrientation)interfaceOrientation
{
//对于支持的方向返回YES
返回(interfaceOrientation==UIInterfaceOrientationGraphic);
}
@结束

如果您试图格式化货币,请使用NSNumberFormatter

NSNumber *someMoney = [NSNumber numberWithFloat:5.95];

NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];

NSString *formattedAsDollars = [currencyFormatter stringFromNumber:someMoney];
[currencyFormatter release];

如果你感到勇敢,你甚至可以冒险进入NSDecimalNumber的世界。

请在嵌入代码时使用适当的标记格式:有一件事我可以肯定,问题不在
/*viewDidLoad*/
shouldAutorotateToInterfaceOrientation:
之内。您甚至没有试图通过删除与问题无关的代码来简化我们(回答者)的生活。stackoverflow的人不是机器,如果你向他们提问,他们会吐出答案。我只能为自己说话,但我不想回答一个提问者没有任何努力的问题。你想在哪个uilabel上显示货币符号?