-1

Hey guys i have 2 views the first has a UITableView. The Second Has a textField and when the user presses a "Save" button on the second page, i want the textFields text to be added to the tableView. Here is the code i'm using

- (IBAction)saveButton:(id)sender {
CheckListPracticeViewController * obj = [[CheckListPracticeViewController alloc]init];
[obj.cells insertObject:textField.text atIndex:0];
[self dismissModalViewControllerAnimated:YES];
NSLog(@"%@ "[cells objectAtIndex:0]);
[obj.myTableView reloadData];}

For some reason the data isnt being added to the table View does anybody know whats wrong?? Also the NSLog doesnt Work in this method. Thanks a lot Guys :D

kurt moyer
  • 31
  • 1
  • 8

4 Answers4

2

That is because you are creating a new instance of CheckListPracticeViewController and updating it rather than the current one which has presented this view controller modally.

Change the first line to,

CheckListPracticeViewController * obj = (CheckListPracticeViewController *)self.parentViewController;

EDIT

First of all be consistent with your data model. If you are loading an array of dictionaries from the plist and later adding strings into that array then you have a serious problem. I will suggest that you create a dictionary object with name and other stuff and add that to the array. I would say doing [obj.cells insertObject:[NSDictionary dictionaryWithObject:textField.text forKey:@"name"] atIndex:0]; instead of [obj.cells insertObject:textField.text atIndex:0]; will fix this current error but I doubt that will fix your problem.

Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
  • ok thanks for the help, but this code didnt work? So do you know of anything else i did wrong?? Thanks :D – kurt moyer Jul 14 '11 at 20:10
  • What does your `NSLog` statement log? Provided it should be `NSLog(@"%@ ", [obj.cells objectAtIndex:0]);` and also remove the `reloadData` call at the end and add that to `CheckListPracticeViewController`'s `viewWillAppear:` method. – Deepak Danduprolu Jul 14 '11 at 20:11
  • alright i did what you suggested and i still have the same outcome, i get a "SIGABRT" message on this line of code: cell.textLabel.text = [[cells objectAtIndex:indexPath.row] valueForKey:@"name"]; – kurt moyer Jul 14 '11 at 20:17
  • i dont know if you know, but my tableView is populated by a plist and i stored the plist's info into the cells mutable array – kurt moyer Jul 14 '11 at 20:19
  • Shouldn't it be `cell.textLabel.text = [cells objectAtIndex:indexPath.row];` as you are adding a string i.e. `textField.text` in your code above. – Deepak Danduprolu Jul 14 '11 at 20:20
  • yeah but i have a checklist on this table and i use that to populate the table from the plist if i do it the way you posted, i get a crashed program. – kurt moyer Jul 14 '11 at 20:27
  • @Legolas Thanks. @kurt moyer I have updated my answer to state my understanding of your problem. – Deepak Danduprolu Jul 14 '11 at 20:31
  • mmkay i added the code posted above which stores the textfields text in a dictionary, now what should i do to fix the problem of the tableview not adding the data? – kurt moyer Jul 14 '11 at 20:37
  • @Deepak : Any help with http://stackoverflow.com/questions/6700184/sliding-uitabbaritems-in-uitabbarcontroller ? – Legolas Jul 14 '11 at 22:43
0

You are each time the button is pressed you alloc/init a new obj, try adding the new data into your actual container.

  • hmm ok i just tried it and nothing different happened :( any other suggestions?? Thanks – kurt moyer Jul 14 '11 at 18:49
  • no, you **re**instanciate the container each time. Since it is a fresh new instance, you in fact don't add the data to the container which is your actual container for cells and table view. –  Jul 14 '11 at 18:56
  • ok if my actual container is a plist. so should i try to add it there? – kurt moyer Jul 14 '11 at 18:58
  • no, add the data to the container in which the table view data source is picking values. –  Jul 14 '11 at 19:00
  • ok i think i know what you mean... and what do you mean my "picking values" ?? – kurt moyer Jul 14 '11 at 19:02
  • the table view data source is the object responsible for giving the cells and other informations to the table view.That's what I meant, but just to know where is this action method defined actually ? –  Jul 14 '11 at 19:07
  • umm im really sorry about all the questions, im really new to programming im only 16 and ive been doing it for about 1 year so if i aggrivate you with all the questions, im sorry, and which action method? – kurt moyer Jul 14 '11 at 19:12
0

You are creating another instance of CheckListPracticeViewController. I assume thats where your table is. If you are going back to CheckListPracticeViewController (I assume you do since you use [self dismissModalViewControllerAnimated:YES]) you will have to pass your view controller as a week reference or use NSNotification or use NSUserDefaults to store and retrieve this object in the CheckListPracticeViewController.

EDIT

Pass the CheckListPracticeViewController by weak reference to the view that has UITextField.

example:

in the .h of your UITextField class create a controller reference.

@property(nonatomi,assign)CheckListPracticeViewController *controller;

then when you create your new UITextField controller class pass the reference to its creator throught controller instance.

//in CheckListPracticeViewController.m file

myEdiotr.controller = self;

Later use controller instance to save the text from the UITextField.

- (IBAction)saveButton:(id)sender {
  [controller.cells insertObject:textField.text atIndex:0];
  [controller.myTableView reloadData];
  [self dismissModalViewControllerAnimated:YES];
}
Cyprian
  • 9,423
  • 4
  • 39
  • 73
  • ok so i should store the textfield's text in NSUSerDefaults, then retrieve it and store it in the array that is populating my table?? – kurt moyer Jul 14 '11 at 18:51
  • You could or you can pass the controller by week reference. That's the best solution for it. Example posted in an answer. – Cyprian Jul 14 '11 at 18:54
  • @kurt it has to work, maybe you did something wrong, paste the code where you create the view that has the UITextfield. – Cyprian Jul 14 '11 at 19:07
  • wait create the view? what do you mean? and im sorry im a noob :D haha – kurt moyer Jul 14 '11 at 19:09
  • @paste the code where show up the UITextFiewld view containter, if you whether you do it with UIViewController or just plain UIView – Cyprian Jul 14 '11 at 19:17
  • ok i used: -(void)viewDidLoad{ string = textField.text; [super viewDidLoad]; } to store the text into a string and then i used: - (IBAction)saveButton:(id)sender { [controller.cells insertObject:textField.text atIndex:0]; [controller.myTableView reloadData]; [self dismissModalViewControllerAnimated:YES]; } to try to save the text to my cells array!! – kurt moyer Jul 14 '11 at 19:22
  • There is couple of problems here, on viewDidLoad there should not be anything in the textField, since the view has not yet been presented. Second I still don't know how you created this UIViewController in the code of your super ViewController. Third you can't call `dismissModalViewControllerAnimated:YES` to close itself. – Cyprian Jul 14 '11 at 19:31
  • ok so i erased the string = textField.text; code, and im not sure what you mean about how i created my UIViewController? and why cant i use dismissModalViewControllerAnimated:YES; ? Thanks – kurt moyer Jul 14 '11 at 19:41
0

It's definitely one of these.

  1. Check if array cells is allocated. Most likely it is not. That is why it is not being added to the tableView. You can add it to the init method so that you initialize the array before adding to the array.

  2. Try NSlogging the contents of the array at various points. This will let you know if its exactly what's going on. NSLog the array in IBAction of Add-method, viewWillAppear

  3. You need to make sure that your array is allocated before adding data to it.

  4. NSLog the content of textField.text before adding it to the array. I am guessing that it is not a property, and it is simply null.

  5. Make sure you had assigned self.myTableView = tableView at the end of your cellForRowAtIndexPath

Legolas
  • 12,145
  • 12
  • 79
  • 132
  • umm im pretty sure the array is allocated, i wrote cells [[NSMutableArray alloc]initWithContentsOfFile:myFile]; and my file is a NSString that is written like : NSString * myFile = [[NSBundle mainBundle]pathForResource:funds ofType:plist]; – kurt moyer Jul 14 '11 at 19:01
  • Okay. Where is it allocated ? – Legolas Jul 14 '11 at 19:02
  • And is are you adding the modal view controller in that method ? and are you adding the modal view controller before or after allocating array ? – Legolas Jul 14 '11 at 19:14
  • i have no idea what you mean my adding the mvc? – kurt moyer Jul 14 '11 at 19:15
  • Hm. You should have used `presentModalViewController` : someViewController. Because you have posted `dismissModalViewControllerAnimated` in your code above. I am asking about that. – Legolas Jul 14 '11 at 19:17
  • oh yeah i used that in my firstViewController! i used this in my add: method which sends me to the second view! – kurt moyer Jul 14 '11 at 19:20
  • Try NSLogging array contents in `IBACtion of add method`, `viewWillAppear` of firstviewcontroller and paste it. – Legolas Jul 14 '11 at 19:22
  • ok so do you mean something like : - (void)add { MyDetailController * detail = [[MyDetailController alloc]init]; detail.modalTransitionStyle = UIModalTransitionStyleCoverVertical; [self presentModalViewController:detail animated:YES]; [detail.textField becomeFirstResponder]; NSLog(@"%@ %@", [cells objectAtIndex:0], [cells objectAtIndex:1]); [detail release]; } i added the NSLog statement just now and this button just takes us to the second view controller – kurt moyer Jul 14 '11 at 19:25
  • Yeah. What do you see in the NSLogs. Just do `NSLog (@"%@", cells);` in the places I had suggested. – Legolas Jul 14 '11 at 19:28
  • i also added a NSLog in the save: method now it looks like this : - (IBAction)saveButton:(id)sender { [controller.cells insertObject:textField.text atIndex:0]; [controller.myTableView reloadData]; NSLog(@"%@", textField.text); [self dismissModalViewControllerAnimated:YES]; } and the text is displayed in the console, so i dont know why its not being added to the table view ? – kurt moyer Jul 14 '11 at 19:28
  • Okay what do you see in other NSlogs ? Just do NSLog (@"%@", cells); in the places I had suggested. Lets find out if it is allocated. – Legolas Jul 14 '11 at 19:33
  • ok i did NSLog in the add: method, the save: method and they are both printing out exactly what i want them to! So i beleive that means the array is allocated! – kurt moyer Jul 14 '11 at 19:39
  • It printing out the contents of the array ? What about in `viewWillAppear` ? Also write down `[self.tableView reloadData];` in viewWillAppear – Legolas Jul 14 '11 at 19:41
  • yes it is! ok let me try that! and i have [self.myTableView reloadData]; in both the viewWillAppear and in viewDidLoad! – kurt moyer Jul 14 '11 at 19:42
  • If it is a tableViewController, just do `[self.tableView reloadData]`. And make sure that you had set `[self.myTableView = tableview];` – Legolas Jul 14 '11 at 19:44
  • i also have a NSLog in my saveData: method, and its not printing out when the method is called so what does that mean? – kurt moyer Jul 14 '11 at 19:44
  • Post the NSLog in your question and explain it there – Legolas Jul 14 '11 at 19:46
  • kk i did that! now what do you think i should do? do you have any more ideas? – kurt moyer Jul 14 '11 at 19:49