I've a plist file named as "data.plist" like this:
I have six arrays, and I also have 6 buttons and a text field. I would like to save the string in the text field to the correct array when I press the right button.
There are many approaches, here is one:
Name the keys for the arrays in the plist dictionary livello_0 - livello_5.
Assign tags 0 - 5 to the buttons (indexes are zero-based).
When a button is pressed, get the tag (let tag = sender.tag) and get the array with let array = data["livello_\(tag)"].
Update / write the value.
If it is a Swift collection type, assign the array back to the dictionary (value semantics).
Make the changes as below.
Structure of the pList:
Make the button titles as the keys in the plist.
Logic to save:
When a button is pressed, fetch the array of the button's title and save the text from the textField into the array. Update this new array as the value for the key(selectedButton's title)
Set tag to each buttons in a pattern. eg. Button tag corresponding to 0th row of plist will be 0+1000
By doing this you will get the corresponding index of array in plist when user taps the button by doing (tag-1000)
On button tap get the string from text field and save to plist.
Note: It is necessary to set tag more than 0 because by default all the ui elements have 0 as tag.
Related
I want to get the selected item ids in a String array (preferably). How to get the selected item ids when multiple items are selected?
The documentation is often helpful :-)
You can select an item with the corresponding setValue() method. In multiselect mode, the property will be an unmodifiable set of item identifiers. If no item is selected, the property will be null in single selection mode or an empty collection in multiselect mode.
So after casting comboBox.getValue() to Set<?> you can call toArray(new String[0]) to get the desired array.
I have a series of simple sentences e.g. "Hi", "How are you?", "I am fine", "Nice to see you".
I would like to store them in an array.
The problem is I want to display only the first sentence in a single textview. Then when I press a button, it display the second sentence.
So i want it display each of the sentences after a button press.
Which kind of array i should use and how to code it?
Many Thanks
Create an NSMutable array that will hold all of your strings. Set the tag of all the buttons starting from 0 to i less than the size of array which will act as an index for the array. Then when you click on any button use the tag of button to access the array contents at that particular index.
so i created a weather app.
It has a text field where user types the city and then click the button.
The city name will be passed to a function that will request a data for this city.
I have passed the city name to this function
-(void)getCurrent:(NSString *)query
by this way
[theWeather getCurrent:self.TextField.text];
But now i decided to change text field to uipickerview and i got stuck
How can i pass the selected row in my picker view to this function ?
You can access your selected row by accessing the array:
You can get it in the following manner:
NSInteger row;
row = [self.pickerView selectedRowInComponent:0];
getCurrent:_pickerData[row]
Since you have a UIPickerView you can inspect its selectedRowInComponent: to determine which item the user has selected. Assuming you populated this picker view from an array you can get the item out if this array using the index returned here. Also assuming that you have a PickerView with only a list of cities pass 0 for the component. The component is there for more complex views like a time picker where you have hours, minutes, and AM/PM.
I'm using a UIPickerView, and it has the first value chosen by default. When the user doesn't move the UIPickerView from this initial selection, however, the value selected comes back as NSNull. How can I make it so either:
The first value is recognized whether the user changes the selection or not.
The UIPickerView contains a blank value that I can check for in the first spot.
You can use
[_picker selectedRowInComponent:0]
nil means no one move the picker (you can make the first item blank or sth like "--" )
Reference : https://stackoverflow.com/a/27058363/471840
I have a view controller that controls 2 text fields and an array that displays in a table. How would I go about keeping a button disabled until the 2 fields have at least one character and the array is not empty. I am thinking about using cocoa bindings, however I can't seem to figure out a solution.
Currently my button is binded to
BOOL buttonIsEnabled;
I use that in a notification function in order to keep the button disabled, except the button will only reenable if I call that notification function.
-(void)controlTextDidChange
This means if i make a change in an array, the button wont reenable until I re-enter text. I can't seem to figure out an alternative solution. Any suggestions? Thank you.
One solution:
bind the two text fields two two strings (say text1 and text2)
in the text field bindings check Continuously Updates Value
Add this code:
- (BOOL)buttonIsEnabled {
return (self.text1.length>0 && self.text2.length>0);
}
+ (NSSet *)keyPathsForValuesAffectingButtonIsEnabled {
return [NSSet setWithObjects:#"text1",#"text2",nil];
}
Lastly bind the buttons enabled binding to buttonIsEnabled.
Because of the Continuously Updates Value text1 or text2 will change whenever characters are added to or removed from the text fields.
And the + (NSSet *)keyPathsForValuesAffectingButtonIsEnabled method will cause a Key-Value change notification to be posted for buttonIsEnabled whenever text1 or text2 change.
You can create a custom cell, give outlets to your 2 textfields and button.
Now on cell for row at index path after filling the textfields text values, put a condition checking the length of the textfields. If length is greater than 0 you can enable the button or keep it disable.
Along with this you need to put same code in delegate method (textFieldDidChange) of textfield. So that when ever new text is entered the button gets enable or disabled.