简体   繁体   中英

Updating a NSMutableArray in new viewController that's pushed onto the stack

I'm new to programming and some OOP concepts and am not sure what is the "right" way to do something. Basically what I have is an NSMutableArray in my mainViewController.

MainViewController: UITableViewController 

@property (nonatomic, retain) NSMutableArray *data;

A plus button can be pressed to add an item to the Table, or if an already existing row is selected, the current item can be edited. I'm not sure what the best way to structure my DetailViewController since the DetailViewController can be presented from either the plus button or a table row selection. Do I set a flag in DetailViewController that says which method presented the DetailViewController so in my Save method, I either insert the object (if the user pressed plus), or replace the object at the current index (if the user selected a row and I passed the index to the DetailViewController)? It seems kind of clunky to do it that way and was wondering what a better way to do something simple like that would be. Thanks!

Your current MainViewController setup is fine. What you want to do for your DetailViewController is define its own view. Given the description of your app it would be best if you stored each row in your table as an object:

MainViewController: UITableViewController

@property (nonatomic, retain) NSMutableArray * data ; // an entry for each row

DetailViewController: UIViewController

@property (nonatomic, retain) SomeObject * object; // Represents a single row

When presenting your DetailViewController either:

  1. Populate its managed object with an existing row, in case user selects an existing item
  2. Populate it with a new managed object, that will be added to the master list once detail editing is done

When your detail view controller is done, you refresh the table view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM