Display a series of user selected integers, separated by a comma - iphone

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, but 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.

Related

Indenting on linewrap

I'm creating one long string from a number of strings in an array like this:
oneString = #"First Line\n";
for (int i = 0; i < itemsToAdd.count; i ++){
oneString = [NSString stringWithFormat:#"%# %#\n", oneString, stringToAdd];
}
This all works great until stringToAdd is long enough to wrap. If you notice the spaces between %# %#, this is acting as an indentation. Most of the strings I'm adding don't wrap, but I'm trying to figure out a way to add spaces before each line after the first.
The reason I'm building up this one long string is to calculate the height of it in order to adjust the size of a tableViewCell based on the length of this string. itemsToAdd is a variable size array. Sometimes it will be one line, sometimes it will be 15+ items.
This is sample output:
First Line
Item 1 is good
Item 2 is fine
Item 3 is long and it wraps so
it goes like this and it sucks
Item 4 is fine
Try like this for insert tab space:
oneString = [NSString stringWithFormat:#"%#%8s\n", oneString, [stringToAdd UTF8String]];
from how to add tab space into UILabel.text
You need to specify the indent in a way understood by the text rendering system, for example, by using NSParagraphStyle. You will need to make some changes to your cells depending on which versions of iOS your app supports. You may want to use a 3rd party label class or a framework like DTCoreText.

How best to store temperature data for different cities?

It is a little confusing but I will do my best to explain it, I hope it will be fine.
I am trying to create the following functionality in iOS:
I have red button on each row of tableview. The red button is dragged and dropped over the (prototype) tableViewcell and in each row the button will be used. The button text shows the temperature in Fahrenheit. But it changes when tapped so that the color of the button changes to green. And I want the extra functionality at this point; The temperature in Fahrenheit will change into Celcius. (I have the problem on this functionality)
The problem here is that I have a list of tableviewcells (Each is another city), not just one. So I have got that number of buttons and I don't use database to save the temperature data. The data is read through a service and put into an array and transferred to this view by NSUserDefaults.
For example; I have got a list of cities in my tableview. Each is a row and each temperature is put over a red button. Temperature values are the texts of the red buttons.
Berlin 45F
Istanbul 70F
Miami 100F
Chicago 30F
...etc.
So for example, when I tap on the button on Berlin row to change the Fahrenheit to Celcius, there is no problem for the first time so I can get the Celcius value for Berlin, no problem. But when I tap it next time to change it to Fahrenheit again, the value does not come correctly. The Fahrenheit value of Berlin is confused with the other values. Because I don't use a database and it does read the value from an array and it doesn't know which value to use. After some clicks, all the Fahrenheit values come to a single value which is the last city's Fahrenheit value (I use the indexPath.row so it gets the last clicked value)
How can I maintain the temperature data for Berlin without using a database so that when I tap that again after tapping the other cities' buttons, it will show me the correct value (45F or the correct Celcius value)?
Thanks for your help!
While I suggest you use some form of key-based storage (like creating a class "Cities" that stores the temperature, or even storing the city -> temperature key combo in a dictionary), you could read the button text when you click it and convert the value from NSString back to a double or integer.
I assume the function that gets called when you click the button looks something like the one I'm about to write. You can then get the button you clicked and the text it displays like so:
- (IBAction)convertTemperature:(id)sender {
UIButton *myButton = (UIButton *)sender;
NSString *buttonText = [myButton titleForState:UIControlStateNormal];
//...
}
After this, you just convert the string to get the temperature, convert it and display it back.

Set selected row for a given value in UIPickerView iPhone

In my app, I ask a worker to choose a letter from the alphabet (for example A) and the alphabet will have some explications that will appear in a UIPickerView with 3 # rows.
I want my UIPickerView to be display contents based on the indicator that has been chosen by a worker.
How do I do this?
selection. in this case, it means showing the appropriate row in the middle
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated;
scrolls the specified row to center.
please provide some code so That I can Get What exact issue is

iPhone: Phone Number UITextView vs UILabel

I have a UITableView. In row I need to setup text, so I need to use UILabel or UITextView. I need to detect phone number in text, so I use UITextView. BUT I need to set text only in 2 lines and if it's longer, setup ellipses in the end , because row height isn't big, so that work UILabel do perfect. So how to be ??? Can UILabel detect phone number ? or can i set UITextView in 2 lines and set ellipses in the end ?? Thanks...
I do not think that the UITextView has a property that let you set ellipses at the end if the text is to long. However that is simple to fix:
NSString documentation
You could use this method if all you want to do is to allow let say 120 characters, and starting from 0.
substringToIndex:
Returns a new string containing the
characters of the receiver up to, but
not including, the one at a given
index.
(NSString *)substringToIndex:(NSUInteger)anIndex
NSString *shortText =
[[someLongTextVariable substringToIndex: 120] stringWithString:#"..."];
Maybe you should use a mutable string for this, I do not know what performes the best. But if you do this, you could atleast detect phone numbers.

cycle through entries in NSDictionary

Im writing a game that has two sets of buttons. The first set is like blank spaces, and the second set is a custom keyboard.
So I've created a dictionary to keep track of selected buttons (blank spaces) in my interface. The user can select say 5 of these and update all of their titles at once. When the user presses a key (on my custom keyboard made of buttons in a separate subview) I want to change all of the titles to show the letter on the key the pressed.
I have my keys numbered (alphanumerically) from 1 to 26 (ie. a=1, z=26) and the current set of labels on the blanks will be kept in similar numeric form in an array called "currentSolution"
Im imagining i can set up a for loop to go through selected blanks and give them all the title of whatever key is selected in the keyPressed method. Can anyone give me any guidance here? im kinda a noob and dont really know how i should go about this
thanks
You can use (only in ios 4)-
[yourDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id object, BOOL *stop) {
NSLog(#"dic = %# : %#", key,object);
//Do what you want for every object...
UILabel *label = (UILabel*)object;
label.text = #"bla bla";
}
}];
Hope it will help