2014-12-03 91 views
0

我在我的板球計分板應用程序中有三個文本字段。在ios中轉換拆分字符串

如果我點擊的每一個動作按鈕,我想每一個由0.1,0.2,0.3,0.4,0.5

每個接管數0.5後,它應該是1.0,那麼就應該繼續爲1.1,1.2。 ...... 1.5,2.1,2.2,2.3,2.4,2.5,3.1 ......

對於每一個動作按鈕,請幫助我,這

IBOutlet UITextField *score; 
IBOutlet UITextField *wickets; 
IBOutlet UITextField *overs; 

-(IBAction)dot:(id)sender; 
-(IBAction)extras:(id)sender; 

這是我曾嘗試:

-(IBAction)dot:(id)sender 
{ 
    if(txtOvers.Text.split(「.」)[1]! = 5) 
    txtOver.text = txtOver.Text + .1; 
    else 
    txtOver.text = 0.5; 
} 
+0

請告訴我們您嘗試過的。 – Vino 2014-12-03 04:51:30

+0

- (IBAction爲)點:(ID)發送方{ 如果(txtOvers.Text.split()[1] = 5 txtOver.text = txtOver.Text + 0.1; 否則 txtOver.text = O「」! .5; – 2014-12-03 04:54:56

回答

0
@property (nonatomic, strong) IBOutlet UITextField *txtOver;\ 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.txtOver.text = @"0.1"; 
} 

-(IBAction)dot:(id)sender 
{ 
    NSArray *textSplitCollection = [self.txtOver.text componentsSeparatedByString:@"."]; 

    int lastPrecisionValue = ([textSplitCollection count] > 1)?[[textSplitCollection objectAtIndex:1] intValue]:0; 

    if (5 == lastPrecisionValue) 
    { 
     self.txtOver.text = [NSString stringWithFormat:@"%.01f", [self.txtOver.text floatValue] + 0.5]; 
    } 
    else 
    { 
     self.txtOver.text = [NSString stringWithFormat:@"%.01f", [self.txtOver.text floatValue] + 0.1]; 
    } 
} 
+0

感謝它對我的工作 – 2014-12-03 05:57:33