Sorry to kind of be vague in the question. I currently have a pickerView populated by an array of strings. The string is set by this function...
_homePlayer = _homePlayersArray[indexPath.row];
// add to the copy
[_homeConfirmedPlayersArray addObject:[NSString stringWithFormat:#"%d %# %#",_homePlayer.number,_homePlayer.firstName,_homePlayer.lastName]];
I have it populate the pickerView which works properly. I now want to be able to pick from the picker and take just the first part of the string and set it as an NSString. Is this possible and if so how would i go about doing this? Could i change some things to be able to accomplish this? thanks in advance!
In the delegate for your UIPickerView
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
Player *player = _homePlayersArray[row];
NSInteger number = player.number;
}
Related
I have a UIViewController, and in that I have an array of questions that I pull from a sqlite3 query. I am then using a for loop to iterate through each question in the array to change the UILabel.text to display the question on the screen. This is actually working for the first question in the array!
I then have four buttons for answers. I want to make it so if one of the buttons is pressed, the answer is saved and the next question in the loop updates the UILabel.text.
The four answers never change as it is more of a survey than answers, so one of the answers is "I agree" or "disagree", so the button text never changes.
Is this possible?
I have been on here and Google to find a way to link the button pressed with completing each iteration of the loop without any luck.
Why are you iterating through questions and changing UILabel's text? Shouldn't be it changed only on tapping one of the survey buttons?
If I got you correctly, you should do following:
1) Declare three properties in your controller: NSArray *questions, NSMutabelArray *answers, NSInteger currentIndex;
2) Init/alloc them in viewDidLoad (except currentIndex, of course, set it to 0).
3) Fill up questions array with your question strings.
4) Set text to UILabel, label.text = questions[currentIndex];
5) create IBAction method and link it to all survey buttons.
6) in IBAction method, insert button's title to answers array and show next question.
- (void)viewDidLoad {
[super viewDidLoad];
self.questions = {your questions array};
self.answers = [[NSMutableArray alloc] init];
self.currentIndex = 0;
}
- (IBAction)btnClicked:(id)sender {
UIButton *btn = (UIButton *)sender;
NSString *title = btn.titleLabel.text;
[self.answers addObject:title];
currentIndex++;
label.text = questions[currentIndex];
}
I hope you will understand the code.
In short, yes this is possible.
You'll first want to keep track of the question that your user is currently on. You can do this by storing an index in an instance variable or, if you plan on allowing the user to open the app and start from where they left off, you can use NSUserDefaults, which writes to disk and will persist.
// In the interface of your .m file
int questionIndex;
// In viewDidLoad of your controller, however this will start for index 0, the beginning of your questions array
questionIndex = 0
By storing the index in NSUserDefaults, you can grab it in ViewDidLoad, and start from where the user last left off:
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:questionIndex] forKey:#"questionIndex"];
To store your answer, you could add a method for your buttons called answerTapped:,
- (void)answerTapped:(UIButton *)answerButton
{
// Grab the answer from the text within the label of the button
// NOTE: This assume your button text is the answer that you want saved
NSString *answer = answerButton.titleLabel.text;
// You can use your questionIndex, to store which question this answer was for and you can then take the answer and store it in sqlite or where you prefer...
}
You can add this method to your buttons like so
[answerButton addTarget:self action:#selector(answerTapped:) forControlEvents:UIControlEventTouchUpInside];
You could then write a method to increment questionIndex now that an answer button has been pressed.
- (void)incrementQuestionIndex
{
// Increment index
questionIndex += 1;
// Update and save value in UserDefaults
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:questionIndex] forKey:#"questionIndex"];
}
You could then call a separate, final method to update the question label.
- (void)updateQuestionLabel
{
// Grab the question using the index (omit this line and go straight to the next if storing the index in an iVar)
questionIndex = [[[NSUserDefaults standardUserDefaults] objectForKey:#"questionIndex"] integerValue];
// Grab the question using the index. Assumes you have a questions array storing your questions from sqlite.
NSString *question = [questions objectAtIndex:questionIndex];
// Update the question UILabel
[questionLabel setText:question];
}
I have a UIPickerView inside a custom tableViewCell (subclassed). I'am able to populate it and get data back from it. More or less.
I have this method I implement in order to get info everytime some of the components changed:
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if (self.pickerDelegate !=nil &&[self.pickerDelegate conformsToProtocol:#protocol(PickerCellDelegate)]) {
if ([self.pickerDelegate respondsToSelector:#selector(somethingOnThePickerIsSelected:selectionArray:)]){
NSMutableArray *pepe;
for (int i=0; i<[[self.cellPickerInputDictionary objectForKey:#"components"] count]; i++) {
NSObject*foo=[[[self.cellPickerInputDictionary objectForKey:#"components"] objectAtIndex:i] objectAtIndex:[self.cellPicker selectedRowInComponent:i]];
[pepe addObject:foo];
NSLog (#"foo: %#", foo);
}
NSLog (#"pepe: %#", pepe);
[self.pickerDelegate somethingOnThePickerIsSelected:self selectionArray:pepe];
}
}
}
I have two components, but in order to make it "universal" (independent of a particular situation) I don't want to hard-write numbers here and there.
In the example shown, I don't understand why the NSLog shows correct for the variable foo but shows null for the NSMutableArray pepe.
Any ideas? Thanks in advance.
You are not allocating your mutable array pepe.
NSMutableArray *pepe=[[NSMutableArray alloc]init];
In fact, you can do that on your init method, and declare pepe as property
In my app, there is component for sending one message to multiple users at the same time.
I would like to implement it similar to facebook message composing. By using uitextfield with local notifications, I'm able to filter my UITableView like this;
self.searchTextFieldObserver = [[NSNotificationCenter defaultCenter]
addObserverForName:UITextFieldTextDidChangeNotification
object:self.searchTextView
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *note) {
if ([self.displayArray count]) {
NSArray *tempUsersArray = nil;
NSArray *tempNonUsersArray = nil;
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"fullName CONTAINS[cd] %#", self.searchTextView.text];
self.displayArray = [MyAddressBook sortedContacts:self.contacts];
if ([self.searchTextView.text length] > 0) {
tempUsersArray = [self.displayArray[0] filteredArrayUsingPredicate:predicate];
self.displayArray = #[tempUsersArray];
}
}
[self.tableView reloadData];
}];
This works as expected... As I'm providing input for textfield, my table gets filtered. What I want to implement is this approach:
When I select one user by searching the table view, his nickname should be inserted in my textfield and predicate should be reseted to empty string, so I can start filtering again. Back button should select already inserted name and on second backspace tap, it should delete it.
I was thinking about this for some time, and all I could come up with, was using multiple element on top of each other. UITextView would change it's frame as users are added up (saved in array, which is source for UILabel's text, that partly replaces frame of textview), but I hitted wall, when I had to have possibility of removing already picked users.
If you guys know about another approach, or about any library that could help me, I would be very glad.
Sorry for making this an answer instead of just a comment, but I don't have enough reputation yet to comment.
Anyway, I think I would probably have an array of the selected user names that have been added to the textField. Then you could use UIControlEventEditingChanged to check for the text changing, and in that method that gets called take the textField's current text, strip out any strings that match those in the selected user names array as well as commas and such, then set your filter string to what's left over.
As an example of how you could use UIControlEventEditingChanged:
[_textField addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
In the case of removing users from the textField, you could use that same textFieldDidChange method to compare the array of added users to the textField, and if one exists in the array that doesn't exist in the textField anymore remove it before re-filtering.
-Stephen
Currently I have a custom view table cell and a text field just above it. I want to get the text from the UItextfield and put that into an NSMutableArray.
Pseudocode:
String text = _textfield.text;
[array addObject:text];
NSLog{array}
In my header file I have created the textfield and the array.
I currently receive the error : 'CustomTableView:[340:11303] array: (null)' when I NSLog.
I am not to sure why the text from the textfield is not getting added to the array. If any one is able to help it will be greatly appreciated.
Note - My textfield is above the custom cell not in it. I have even tried just adding a string to the array directly and logging it and I get the same error. So I would assume that this is something to do with the array.
did you initialize your Array.take a MutableArray and initialize it.
NSMutableArray *array=[NSMutableArray alloc]init];
You mentioned that you have declared the textfield and the array in your header file...
Have you initialised the variable array?
e.g.
array = [NSMutableArray new];
It looks like you are not actually creating the array. In Objective C, you do not create things in header file, you declare them. The implementation files(.m files) do all the work.
Try this:
NSString *text = _textfield.text;
array = #[text]
NSLog( #"%#", array );
This is how you should print your array,
NSLog(#"%#", array);
It looks as if your a newbie to ios.Go through the objective-c and Read the apple documentation carefully.
NSString * text = self.textfield.text;
NSMutableArray *array = [NSMutableArray alloc] init];
[array addObject:text];
NSLog(#"%#",array);
For me this is what worked...
I have taken one textfield inside tableviewcell. I am creating textfields based on dynamic data. My requirement is , I need to get textfields text which are created dynamically.
For getting text in another method
NSIndexPath *indexPath = [tableViewObj indexPathForCell:customCell];
if (indexPath.row==0)
{
[arrayPhoneNumbers addObject:customCell.textFieldObj.text];
NSLog(#"array is :%#",arrayPhoneNumbers);
}
else if(indexPath.row==1)
{
[arrayPhoneNumbers addObject:customCell.textFieldObj.text];
NSLog(#"array is :%#",arrayPhoneNumbers);
}
else if(indexPath.row==2)
{
[arrayPhoneNumbers addObject:customCell.textFieldObj.text];
NSLog(#"array is :%#",arrayPhoneNumbers);
}
Like this I have added textfield text to array. Let me know if you have any doubts.
Anyone knows how to make continuous values in a component of a custom picker, like months wheel in date UIPickerView? Here is my source array data:
self.one =[[NSMutableArray alloc]init];
for (int i=0; i<=101; i++) {
[one addObject:[NSNumber numberWithInt:i]];
}
//and here my titleforrow method
if (component==0) {
return [[one objectAtIndex:row] stringValue];
}
Take a look at DialController here.
There's a video and some example code.
You might be interested in this DLPickerView. This picker view can totally replace UIPickerView. And yes, you can make DLPickerView scroll cyclically. And config many other new features that UIPickerView doesn't have.