0

I have a table. When you click on row of table you get detail using prepare for segue. From detail page, I have an edit button that lets you open a view controller previously created in the storyboard modally.

The question is how can I pass the row of the detail item or otherwise give the edit controller the info of what item to display?

Here is code that launches view controller.

//create Edit navigation button:

 UIBarButtonItem *editButton = [[UIBarButtonItem alloc]
                                   initWithTitle:@"Edit"
                                   style:UIBarButtonItemStylePlain
                                   target:self
                                   action:
                                   //next line calls method editView
                                   @selector(editView:)];
    self.navigationItem.rightBarButtonItem = editButton;

//method that fires when you click button to launch edit view controller


- (void) editView:(id) sender
{
    NSLog(@"pressed");
    UIStoryboard *storyBoard = self.storyboard;
    NSString * storyboardName = [storyBoard valueForKey:@"name"];
    UIViewController *vc = [[UIStoryboard storyboardWithName:storyboardName bundle:nil] instantiateViewControllerWithIdentifier:@"editvc"];
    IDEditVC *secondViewController =
    [storyBoard instantiateViewControllerWithIdentifier:@"editvc"];
}

But how can I pass the information on the item to edit?

Thanks for any suggestions.

user1904273
  • 4,562
  • 11
  • 45
  • 96
  • What do you mean by "previously created in the storyboard modally"? You're creating the controller in your editView method. You have a pointer (secondViewController) to it there, so you can pass any information you need in that method. It's not at all clear why you're instantiating 2 controllers, vc and secondViewController in editView, and you don't do anything with either of them. – rdelmar Apr 05 '15 at 04:48
  • You can easily pass data to a view controller. Its upto you how you want data as an array or string. You define variables in the edit viewcontroller and pass the data from detail page. – Uttam Sinha Apr 05 '15 at 07:12

1 Answers1

0

Lets assume that in UITableViewController you are building the TableView an array with objects(eg: MyObject.m/h). You should detect which cell the user has select using the didSelectRowAtIndexPath and then retrieve MyObject from your array using that integer at prepare for segue. Eg:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

        cellPressed =(int)indexPath.row; //cellPressed is an integer variable(global variable in my VC

        [self performSegueWithIdentifier:@"toMyVC" sender:self];

}

Now on prepareForSegue:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if([[segue identifier] isEqualToString:@"toMyVc"]){

    MyVC *mVC = [segue destinationViewController];
    mVC.myObject = [myArrayWithMyObjects objectAtIndex:cellPressed];

 }
}

In your Edit View:

IDEditVC *secondViewController =
[storyBoard instantiateViewControllerWithIdentifier:@"editvc"];
secondViewController.myObj = myObjectRetrievedFromArray;

Note: You should declare a variable MyObject in .h file in order to be visible from other classes.

Declaring a "global" variable in a class:

@interface ViewController : UIViewController{

  int cellPressed; //This is accessible by any method in YourViewController.m
}
BlackM
  • 3,927
  • 8
  • 39
  • 69
  • Would this be the same using Core Data. In prepareforsegue I currently just have Items *item = [self.fetchedResultsController objectAtIndexPath:indexPath]; and then destViewController.item=item – user1904273 Apr 05 '15 at 17:44
  • Realized I do not know how to create a global variable. Do you use extern? – user1904273 Apr 05 '15 at 18:33
  • Do you create a new separate class for the global variable or do you just put it in some class that is imported into the view controllers I am working with such as the data model that holds the core data stuff? – user1904273 Apr 06 '15 at 14:50
  • In our case global variable for the class means that is a variable that it can be accessed by any method of the instance class. Not a global variable which can be accessed by any class. I declare it as I showed on the example in the .h of my class – BlackM Apr 06 '15 at 16:44
  • Not sure which class you put it in but I put it in model which is lazy loaded in all three VCs, tableview, detail and edit. However, I am having trouble storing Indexpath as integer. I created property to hold integer in edit vc with @property(assign,nonatomic) int *indexNum; Is this right syntax? secondViewController.indexNum = &(cellPressed); – user1904273 Apr 06 '15 at 18:49
  • Also, I can't seem to put int cellPressed; after interface in any of my objects. It says cannot put after interface. Does class have to be uiviewcontroller? – user1904273 Apr 06 '15 at 18:57
  • 1
    Okay, I was able to get it to work with answer from following question that is similar to yours...http://stackoverflow.com/questions/9016680/how-many-ways-to-pass-share-data-b-w-view-controller. Marking yours right cause it helped me out although I did it a somewhat different way based on link. Thanks. – user1904273 Apr 06 '15 at 19:30