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

  • Home
  • SEARCH
  • 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 8438675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:46:51+00:00 2026-06-10T07:46:51+00:00

IT IS SOLVED BY @T-X. I’ve marked the changes with // SOLUTION in the

  • 0

IT IS SOLVED BY @T-X. I’ve marked the changes with “// SOLUTION” in the code!!!

I have the following issue, if I presentModalViewController, the NSMutableArray “projectsArray” gets reset.

Here is my code:

Here the .h file:

    // ViewController.h
#import <UIKit/UIKit.h>

@class AddProject;

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
    IBOutlet UITableView *projectsTableView;
    UIAlertView *reallyDelete;

    BOOL candelButtonClicked;
    NSIndexPath *actualIndexPath;

    NSMutableArray *projectsArray;
    NSInteger ID;
    NSString *NAME;

    AddProject *addProject;
    **ViewController *viewContr; // SOLUTION**
}
- (IBAction)addNewProject:(id)sender;

- (void)addToTableView:(NSString *)projectName;

@property (nonatomic, retain) UIAlertView *reallyDelete;
@property (nonatomic) NSInteger cancelButtonIndex;

@property IBOutlet UITableView *projectsTableView;

@property (nonatomic, retain) AddProject *addProject;
**@property (nonatomic, retain) ViewController *viewContr; // SOLUTION**

@end

Here the .m file:

    // ViewController.m
#import "ViewController.h"
#import "CustomTableCell.h"
#import "AddProject.h"

@interface ViewController ()

@end

@implementation ViewController

**@synthesize viewContr; // SOLUTION**

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    projectsArray = [[NSMutableArray alloc] init];
    ViewController *element = [[ViewController alloc] init];

    element->ID = 1;
    element->NAME = @"Demo project";
    NSLog(@"viewdidload");
    [projectsArray addObject:element];

    NSLog(@"1. Array length: %d", [projectsArray count]);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([projectsArray count] < 1) {
        NSLog(@"2. Array length: 0");
        return 0;
    } else {
        NSLog(@"2. Array length: %d", [projectsArray count]);
        return [projectsArray count];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 78;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *customTableCellIdentifier = @"CustomTableCell";

    CustomTableCell *cell = (CustomTableCell *)[tableView dequeueReusableCellWithIdentifier:customTableCellIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    ViewController *element = [[ViewController alloc] init];
    element = [projectsArray objectAtIndex:indexPath.row];
    cell.nameLabel.text = element->NAME;
    cell.prepTimeLabel.text = [NSString stringWithFormat:@"%i", element->ID];

    NSLog(@"3. Array length: %d", [projectsArray count]);

    return cell;
}

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

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source

        reallyDelete = [[UIAlertView alloc] initWithTitle:@"Deleting" message:@"Do your really want to delete that row?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
        [reallyDelete show];

        actualIndexPath = indexPath;
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    if (buttonIndex != [reallyDelete cancelButtonIndex])
    {
        //If "Yes" clicked delete
        [projectsArray removeObjectAtIndex:actualIndexPath.row];
        NSLog(@"delete row");

        [self.projectsTableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:actualIndexPath, nil] withRowAnimation:YES];
        [self.projectsTableView reloadData];
    }
}

#pragma mark Edit

- (IBAction)addNewProject:(id)sender {
    NSLog(@"4. Array length: %d", [projectsArray count]);

    AddProject *addProjectView = [[AddProject alloc] initWithNibName:@"AddProject" bundle:nil];
    NSLog(@"4.1 Array length: %d", [projectsArray count]);
    addProjectView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    NSLog(@"4.2 Array length: %d", [projectsArray count]);
    addProjectView.modalPresentationStyle = UIModalPresentationFormSheet;
    NSLog(@"4.3 Array length: %d", [projectsArray count]);
    **addProjectView.viewContr = self; // SOLUTION**
    [self presentModalViewController:addProjectView animated:YES];
    NSLog(@"4.4 Array length: %d", [projectsArray count]);
    addProjectView.view.superview.frame = CGRectMake(addProjectView.view.center.x/2.5, addProjectView.view.center.y/3, 800, 600);
    NSLog(@"4.5 Array length: %d", [projectsArray count]);
}

- (void)addToTableView:(NSString *)projectName
{
    NSLog(@"Project Array: %@", projectsArray);
    NSLog(@"addToTableView: %@", projectName);    
    NSLog(@"5. Array length: %d", [projectsArray count]);

    ViewController *element = [[ViewController alloc] init];

    element->ID = 2;
    element->NAME = projectName;

    [projectsArray addObject:element];

    NSLog(@"6. Array length: %d", [projectsArray count]);

    [self.projectsTableView reloadData];
    [self.projectsTableView setNeedsDisplay];
}

- (void)viewDidUnload
{
    [self setProjectsTableView:nil];
    self.projectsTableView = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

@end

Here the .h file of the modal presented view:

    // AddProject.h
#import <UIKit/UIKit.h>

@class ViewController;

@interface AddProject : UIViewController
{    
    IBOutlet UITextField *projectName;

    ViewController *viewContr;
}

- (IBAction)back:(id)sender;
- (IBAction)next:(id)sender;

@property (nonatomic, retain) ViewController *viewContr;

@end

The .m file of the modal presented file:

    // AddProject.m
#import "AddProject.h"
#import "ViewController.h"

@interface AddProject ()

@end

@implementation AddProject

**@synthesize viewContr; // SOLUTION I've mist to synthesize it**

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    **// SOLUTION no new allocation/init of ViewController**
}

- (IBAction)back:(id)sender
{
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)next:(id)sender
{
    [viewContr addToTableView:projectName.text];
    [viewContr.projectsTableView reloadData];
    [viewContr.projectsTableView setNeedsDisplay];
    [self dismissModalViewControllerAnimated:YES];
}

- (void)viewDidUnload
{
    projectName = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

@end

I’ve tested the code by using breakpoints and I could locate the issue when the AddProject class “viewDidLoad” starts loading. But I don’t know why it resets the array.

  • 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-10T07:46:52+00:00Added an answer on June 10, 2026 at 7:46 am

    You navigate from ViewController Screen to Add project Screen. In Add Project Screen, you add a new project and want to delegate back this information to the ViewController Screen where you came from.

    Now, you are trying to have an instance of ViewController in your AddProject, which you newly instantiate in viewDidLoad method and when project is added, you provide the information to it by calling - (void)addToTableView:(NSString *)projectName on it.
    This doesn’t work because – The instance of ViewController you allocated is a new instance and not the one you navigated from. When you do alloc + init you get a new object. Thus, although you get addToTableView: called on your ViewController, it doesn’t work because the ViewController instance that is receiving this message is a new instance and not the one you navigated from.

    Thus, all you needed to do is – have a ViewController property in your Add Project, which you would set before navigating. And then you can send message to this property – As explained in this transcript.

    I would like to say – However this thing works, it provides tight coupling between the two controllers.

    Ideally, This cane be done by making ViewController (where you go navigate from) as a delegate of Add Project (where you navigate to). And then Add Project can delegate the added project to it’s delegate by something like - (void) addProjectController:(AddProjectViewController *) controller didAddProject:(NSString *) projectName. This provides lose coupling between the two view controllers. To understand delegation concept, refer to Delegates and Datasources. Alternatively, search for other Stack overflow posts.

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

Sidebar

Related Questions

SOLVED. Code has been edited to reflect solution. Given the following GridView : <asp:GridView
SOLVED: See my solution below! using aspx with C# code behind. I have the
SOLVED BELOW See below for solution I have a ListView with a custom adapter
** Solved ** I have a bit of a problem with my code i
------ SOLVED ------ Hi everyone, I have now solved this issue and it was
[Solved] Writing parsing code is a trap. A line with 15 spaces will have
Solved I actually found out what is going on here. Turns out it was
SOLVED in the last answer im getting following error, dunno where and why? cause
@Solved The two subquestions I have created have been solved (yay for splitting this
SOLVED http://jsfiddle.net/ArtofLife/u9d9d/ I have multiple TEXTAREA and BUTTON out side.. For inserting string in

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.