Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7794211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:42:58+00:00 2026-06-01T22:42:58+00:00

Up in viewDidLoad I have: UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)]; For

  • 0

Up in viewDidLoad I have:

UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];

For the insertNewObjectMethod I have:

- (void)insertNewObject:(id)sender
{
    NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
    NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
    CommodityTypes *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];

    self.detailViewController.detailItem = newManagedObject;
    self.detailViewController.context = context;

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

}

There is also:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showCommodityTypesDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        CommodityTypes *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
        NSManagedObjectContext *context = self.fetchedResultsController.managedObjectContext;
        [[segue destinationViewController] setDetailItem:object];
        [[segue destinationViewController] setContext:context];
    }
}

When I build & run the app and select the + button it does the segue to the detail page, but does not load the newManagedObject information.

My guess is that performSegueWithIdentifier triggers a call to prepareForSeque, and because there is no selected row nothing gets loaded, but this method overrides the earlier self.detailViewController.detailItem = newManagedObject;
self.detailViewController.context = context;

How do I work around this?
First guess, insertNewObject creates newManagedObject. How do I select the row that it appears on in the table?

Beginning iOS programmer here.

Eureka
self.detailViewController is the detail view controller instance used by the iPad split view controller not the detail view controller instance used by the iPhone interface.

performSegueWithIdentifier does send a message to prepareForSegue. To fix my problem I had to do two things.

Create a second segue from the master view controller to the detail view controller.
The original seque was linked from the table view row to the detail view controller. The second segue I linked from the master view controller to the detail view controller. (Use the view controller icon in the footer of the view controller in Interface Builder.)

Move the object creation lines from insertNewObject to prepareForSeque in an if statement that selects for the unique name of the second seque.

- (void)insertNewObject:(id)sender
{

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

}

and

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"ctMasterRowToDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        CommodityTypes *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
        NSManagedObjectContext *context = self.fetchedResultsController.managedObjectContext;
        [[segue destinationViewController] setDetailItem:object];
        [[segue destinationViewController] setContext:context];
    }

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

        NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
        NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
        CommodityTypes *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];

        [[segue destinationViewController] setDetailItem:newManagedObject];
        [[segue destinationViewController] setContext:context];

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T22:43:00+00:00Added an answer on June 1, 2026 at 10:43 pm

    In your insertNewObject: method, you have these two lines –

    self.detailViewController.detailItem = newManagedObject;
    self.detailViewController.context = context;
    

    …which suggest that you’ve already tried to initialise the detailViewController which you’re going to segue to. You don’t need to do that, it’s initialised automatically because you’ve linked up in the storyboard, and you pass it any information you want to in the prepareForSegue:sender: method – in the way you’re doing it in fact.

    As to getting the right data, maybe you could do that in your tableView’s delegate method, tableView:didSelectRowAtIndexPath:, and just set an ivar to the row number of the selected row so you can get the info in the prepareForSegue: method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UITableView with the following code: - (void)viewDidLoad { [super viewDidLoad]; parser
I have a UITableViewController. In viewDidLoad I set the rowHeight: self.tableView.rowHeight = 43; But
I have a RESTful API serving JSON. I'm calling it like this: - (void)viewDidLoad
Consider the following code in my TTTableViewController , I have - (void) viewDidLoad {
I have the following: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath(NSIndexPath*)indexPath { audiChassisInputViewController = [[myAudiChassisInputViewController alloc] init];
I have the following loop in my viewDidLoad: for(int i=1; i<[eventsArray count]; i++) {
I have a simple mapview that has the following viewdidload method and didupdate to
I have created several UI elements in the viewDidLoad method. I want to change
I have code that dynamically load 10 different textfields in viewdidload method now if
- (void)viewDidLoad { [super viewDidLoad]; NSURL *url = [NSURL fileURLWithPath: [NSString stringWithFormat:@%@/Intro2.mp3, [[NSBundle mainBundle]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.