I have a text field in my app. I am trying to store whatever is entered into the text field in an array and display it in my root view controller (which is a table view) on click of a button.
The method for the button is as follows:
-(IBAction)addNewCountry:(id)sender
{
[rootViewController.details addObject:nameField.text];
NSLog(#"Country name is %#", rootViewController.details);
[self.navigationController pushViewController:rootViewController animated:YES];
[rootViewController.tableView reloadData];
NSLog(#"new country added");
}
details is the array declared in RootViewController
However, the text field text is not retrieved. Can anybody tell me what am i missing?
Change this
[rootViewController.details addObject:nameField.text];
to this
NSString *name = [NSString stringWithString: nameField.text];
[rootViewController.details addObject: name];
You cont directly insert your string data to your array, you need to store as a string
NSString *stringval = [NSString stringWithString: nameField.text];
then add it in to your string using addobject
[rootViewController.details addObject: stringcal];
Is your details array declared as NSMutableArray? It must be declared as an NSMutableArray in order for it to be modifiable. Also when you initialized the array did you add this after the allocation:
[details retain];
Related
In my IOS app, I have two view controllers. ControllerA and ControllerB. I have an array of objects in ControllerA. ControllerB creates a new Object and adds it to the array in ControllerA. I do this with a reference to the array and pass that to ControllerB. I add a new object in ControllerB, but when I move back to ControllerA, the array has the right size, but all of the contents are the same.
The array is of type Group.
What do you think the issue is?
Code that adds object in controllerB:
NSString *groupName = userName.text; //gets value from UITextField
NSString *pword = password.text;
Group *newgroup = [[Group alloc]init:groupName :pword]; //init values are just NSString's in a class called Group
[groups addObject:newgroup];
[ViewController print];
[self dismissModalViewControllerAnimated:YES];
I have project where user can add some Items to TableView. My data source for this TableView are objects with 2 properties NSString * nameOfItem and NSNumber * numberOfItem. Is any possibility to check my NSMutableArray if it contain string #"someString" as property nameOfItem and if yes than change numberOfItem +1 ?
UPDATE:
I try to do it with for(...in...) but it works just when i have only one object in my NSMutableArray. If i have more objects there it create one new object and than change value to ++ on old one:
Here is some code what i tried:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString*nameToCheck = [NSString stringWithFormat:#"%#", [self.ivc.objects objectAtIndex:indexPath.row]];
for (Items *itemNamed in self.ivc.shoppingList.items) {
if ([itemNamed.nameOfItem isEqualToString:nameToCheck]) {
[itemNamed setNumberOfItem:[NSNumber numberWithInt:[itemNamed.numberOfItem integerValue]+1.0]];
} else {
Items *item = [Items new];
[item setNameOfItem:name];
[item setNumberOfItem:#(1)];
[self.ivc.shoppingList.items insertObject:item atIndex:0];
}
I want to create new one only if is not yet in that list.
Any help appreciated.
I don't know something like this but you can use a NSDictionary instead of NSArray using the name property as a key. And if you don't want to do that, you can sort the array and make a binary search for element.
I have searched online and am at a loss.
I have a text field and when a user types something in and presses 'return' the text will save to my NSMutablearray (userAnswers)
NSMutableArray *userAnswers;
in my init method I have allocated the array:
userAnswers = [[NSMutableArray alloc] initWithObjects:#"Test", nil];
have also tried:
userAnswers = [[NSMutableArray alloc] init];
//When the user presses 'return' on keypad
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[answerText resignFirstResponder];
if (levelSelected == 1) {
[userAnswers insertObject:answerText.text atIndex:0];
[userAnswers replaceObjectAtIndex:0 withObject:answerText.text];
//Have tried both of these!
NSLog(#"SHOULD INSERT INTO ARRAY");//This prints because level one is selected
}
NSLog(#"ARRAY: %#", userAnswers); //Just print null
}
I have no idea what I am doing wrong??
Thanks for any help!
It looks like your array is not allocated at the moment of the insertion and it even prints this (as you commented). You should allocate the array before putting there anything. Your allocation has to be placed in the wrong place so at the returning stage the array is nil or you are assigning nil to the array reference at some point. The reason the array is nil may also depend on how you declared that ivar. This is what I can say on the basis of this code sample.
I think your array is not allocated when the function is call so you can try to alloc your array in viewDidLoad:.
And also decleare your array in .h file not in init or viewDidLoad:
If you are using storyboard allocate array in initWithCoder instead init.
Initializing a View Controller
In my app I created a bunch of object classes for data that I save in NSUserDefaults.
I get the item by:
LauncherToDoItem *item = [[ActionHelper sharedInstance] actionList][indexPath.row];
then i pass it on to a editing view controller:
LauncherEditActionViewController *editActions = [[LauncherEditActionViewController alloc] initWithToDoItem:item];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:editActions];
[self presentViewController:navController animated:YES completion:nil];
In the view controller I have a table that shows data from a editing version of the item.
- (id)initWithToDoItem:(LauncherToDoItem *)toDoItem {
self=[super initWithStyle:UITableViewStyleGrouped];
if (self) {
item = toDoItem;
editedToDoItem = toDoItem;
}
return self;
}
When I edit the editedToDoItem it also writes the the item, so I assume it's also writing to the version in the array? Why is it that by editing 1 of them affect it all? I dont save it back to the array yet but values save automatically.
That's because both item and editedToDoItem point to the same object — and the same exact location in memory. When you write item = toDoItem, what you're really doing is saving the pointer to toDoItem inside the variable item. It's a reference to the original object, not a copy, because Objective-C object variables are pointers to the objects in memory.
Consider the following code:
NSMutableString *string = [#"Hello, world!" mutableCopy];
NSMutableString *a = string;
NSMutableString *b = string;
// string, a, and b point to the same exact object.
[string appendString:#" Hi again!"];
NSLog(#"%#", a); // => "Hello, world! Hi again!"
[b appendString:#" Whoa!"];
NSLog(#"%#", a); // => "Hello, world! Hi again! Whoa!"
When you fetch the item from your array, then store it twice in the editing controller, you're just passing around references to the same object, which, when edited, will reflect on all other references that you have — because they point to the same location in memory.
If you really want several different copies of the object (so that editing one doesn't affect the others), you have to actually copy the objects by making them conform to the NSCopying Protocol and using [item copy].
So I am trying to concatenate a bunch of input strings together as one string so I can save that to a text file.
So far I am trying to write something like this
NSString *tempString = [[NSString alloc]initWithFormat:#"%#%#%#", text1, text2, text3];
The only problem with this is that I need a total of 30 strings stored this way. I need a way to do this without typing out each string name. Is there a way to use a for loop or something to accomplish this? Type the strings like this perhaps?
text(i)
So that the variable name would change each time it went through the for loop. I've tried doing something like this and I can't get it to work. If you can help me with this method or another way that you know to do it I would be very thankful.
Okay, so all of the answers here take the wrong approach (sorry guys).
The fundamental problem is that you are using your "text boxes" as a data source, when they should simply be views. When someone changes the text, you should immediately store them in your model (which could be a simple array) and then reference that model later. (This is part of MVC. Look it up if you aren't familiar, as you should be if you are programming for iOS!)
Here is what I would do. (I'm assuming that your "text boxes" are UITextField's.)
Set the delegate for each text field to your view controller.
Set the tag for each text field to a number which represents the order that you want the strings joined in. (ie 1-30)
If you don't have a separate class for your data model, then setup a declared property in your view controller which stores a reference to a NSMutableArray which can contain all of the strings in order. Let's call it dataSource. In viewDidLoad: set this to an actual mutable array filled with empty values (or previously stored values if you are saving them). The reason that we store empty values is so that we can replace them with the user entered strings for any index, even if they are entered out of order:
- (void)viewDidLoad
{
[super viewDidLoad];
self.dataSource = [[NSMutableArray alloc] initWithCapacity:20];
for(int i = 0; i < 30; i++)
[self.dataSource addObject:#""];
}
Then, use the following text field delegate method which stores the strings into the array as they are entered:
// This is called every time that a text field finishes editing.
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if (textField.tag > 0)
[self.dataSource replaceObjectAtIndex:textField.tag-1 withObject:textField.text];
}
Congratulations! All of your strings are now stored in one array. Now we just have to combine them all:
NSMutableString *theString = [self.dataSource componentsJoinedByString:#""];
Note that I have NOT tested all of this so there may be typos. This should get you pointed in the right direction though!
If you set up your text boxes in Interface Builder with an IBOutletCollection(UITextField) you would have an array of text boxes that you could access the text value using KVC and join them.
//interface
...
#property (nonatomic, strong) IBOutletCollection(UITextField) NSArray *textBoxes;
//implementation
...
NSString *tempString = [[textBoxes valueForKey:#"text"]
componentsJoinedByString:#""];
Using iOS 4's IBOutletCollection
If you programmatically create your text boxes then add them to an array as you create them.
NSMutableString's appendString: method is your friend.
NSArray *strings = [NSArray arrayWithObjects: #"Hi", #" there", #" dude", nil];
NSMutableString *result = [[NSMutableString alloc] init];
for (NSString *string in strings) {
[result appendString:string];
}
NSLog(#"result: %#", result); // result: Hi there dude