Display a series of user selected integers, separated by a comma
I need to produce a list of integers in a label.
On the iPhone screen there are a series of 12 buttons and a label-
(0) (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11)
[___________]
The user is to click a button, and the buttons's id should then be displayed in a label.
If the user clicks 0,4,7,3,10
Then I want the label to display- [0,4,7,3,10]
This is similar to a calculator app, bu开发者_如何学JAVAt a calculator is base10 and its numbers combine to form a string. I would like to keep each integer separate so that other calculations may be performed on the user selected order.
in my @implementation i have tried to modify my calculator app's code, but have had no progress.
Any ideas?
Sounds like you need to keep an array (or some sort of stack) of the numbers pressed, and append to the label's text every time the user hits a button. What are you having issues with?
Your buttons could fire this IBAction
:
- (IBAction)addNumberToLabel:(UIButton *)sender
{
[numbers addObject:[[sender titleLabel] text]];
[label setText:[NSString stringWithFormat:@"[%@]", [numbers componentsJoinedByString:@", "]];
}
Since you want the numbers to be displayed [1, 2, 3, 4]
, it is necessary to change the whole text of the label every time a new number is added to numbers
, an NSMutableArray you initiate in your setup code.
Create a new string StrOldValue
, and get the actual value of label, in the set to label use
NSString *strOldValue= yourLabel.text;
yourLabel.text= [NSString stringWithFormat:@"%@,%@",strOldValue, strNewValue)];
Keep your things in a Mutable array. Then call componentsJoinedByString method like
NSLog(@"%@",[pathArray componentsJoinedByString:@","]);
You can use any string in place of 'Comma' as your delimiter.
精彩评论