سؤال

I am new to this. I have an application with a Tableview where when I pressed on a cell, it will navigate me to a DetailView. Now the problem is that I have a textField within the Detailview and I want to load the data that the user has keyed back to the detailTextLabel segment of the original cell after i press a save button.

How do I go about doing so? Below is what I have:

DetailViewController.m

//save button
-(IBAction) SendTextFieldtoViewController:(id)sender { 

    NSString *grabKeyedData = self.inputField.text;
    ViewController *mainController =[self.storyboard instantiateViewControllerWithIndentfier:@"mainController"];

    [mainController GrabbedKeyData: grabKeyedData];
}

ViewController.m

-(void) GrabbedKeyData:(NSString *)text{

    grabData = text;

    NSLog(@" data: %@",text);
    [tableView reloadData];
}


-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath {

.....

  if(grabData == nil){
      [[cell detailTextLabel] setText:@"no data"];
  }else{
      [[cell detailTextLabel] setText:grabData];
  }

I have managed to pass the data to -(void) GrabbedKeyData and i can read the passed data from NSLog. But the table does not seems to update from it when i return back to the ViewController.

هل كانت مفيدة؟

المحلول

I think the problem could be one of two things. In your question, you say "return back to the ViewController", but you are not going back to the one you started with, you're creating a new one. If you got to the detail controller by pushing from ViewController in a storyboard, then this is not the way to do it. You need to get a reference to the original instance of ViewController, or use a protocol (which is the standard way Apple recommends for sending data back to a controller). There are several ways to get that reference, you could have a property in the detail controller called mainController, say, and ViewController could set itself as the value of that property when it pushes the detail controller.

Another possible problem is a simple one -- the table is using a plain cell rather than the one that has the detailTextLabel. Make sure you've set the right kind of cell in IB.

نصائح أخرى

-(IBAction) SendTextFieldtoViewController:(id)sender 
{ 
    NSString *grabKeyedData = self.inputField.text;
    [mainController GrabbedKeyData: grabKeyedData];
    ViewController *mainController =[self.storyboard instantiateViewControllerWithIndentfier:@"mainController"];
}


-(void) GrabbedKeyData:(NSString *)text
{
  self.keyDataArray = [[NSMutableArray alloc] initWithObjects:text, nil];
  NSLog(@" data: %@",text);
  [tableView reloadData];
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath {

.....

  NSString *grabbedName = [keyDataArray objectAtIndex:0];
  NSLog(@" cell text is :%@",grabbedName);

  [[cell detailTextLabel] setText:grabbedName];
}

You are storing only NSString object in to the array.so,its stored in array at 0th index.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top