2011-04-16 57 views
0

我試圖在我的應用中添加一個「$」到小費結果,但我遇到了一些困難。我在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 
+0

在嵌入代碼時請使用適當的Markdown格式:http://daringfireball.net/projects/markdown/syntax – 2011-04-16 23:17:21

+4

我確信有一件事,問題不在'/ * viewDidLoad * /'或'shouldAutorotateToInterfaceOrientation: 。您甚至沒有嘗試通過刪除與問題無關的代碼來讓我們的(回答者)生活更輕鬆。如果你向他們提出問題,那麼在stackoverflow的人不是吐出答案的機器。我只能爲自己說話,但我不想回答提問者沒有表現出任何努力的問題。 – 2011-04-16 23:37:51

+0

您想在哪個uilabel上顯示貨幣符號? – 2011-04-17 01:21:51

回答

1

如果你想格式化的貨幣,使用NSNumberFormatter。

NSNumber *someMoney = [NSNumber numberWithFloat:5.95]; 

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

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

如果你感覺很勇敢,你甚至可以冒險進入NSDecimalNumber世界。