NSMutableArray addObject not working uitableview

I am a beginning iPhone developer.

I have a UITableView but I am having trouble making it work with an NSMutableArray.

I have allocated and initialized my array: dataArray I have also connected my tableview's dataSource and delegate.

This is my code:

SecondViewControllerModal.m

@implementation SecondViewControllerModal

-(IBAction)addArray:(id)sender
{
    NSArray *content = [[NSArray alloc] initWithObjects:subjectName.text, setLetter.text, setOrder.text , nil];
    SecondViewController *c=[[SecondViewController alloc] init];
    [c addToArray:content];
}

...

(addArray is called when the user presses a button)

SecondViewController.m

-(void)addToArray:(NSArray *)content {

    NSString *setSubject = [content objectAtIndex:0];
    NSString *setLetter = [content objectAtIndex:1];
    NSString *setOrder = [content objectAtIndex:2];

    [dataArray addObject:setSubject];

    [tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [dataArray count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self->tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];


    return cell;
}

NOTE: I am using storyboards and a tabbed view application and I only need to run this on an iPad if that makes any difference.

If I haven't included enough detail or you have any questions about the code please leave a comment.

EDIT: This is how my popover looks like in xcode:

Answers


Every time your press the 'round rect' button, you're creating a new second view controller, passing it some data, asking it to do something and then throwing it away.

You need to update your addArray: method to call addToArray: on your existing second view controller.

You should probably do this by passing a reference to second view controller to the modal when you show it as a result of the bar button being pressed.

In SecondViewControllerModal.h :

@property (weak, nonatomic) SecondViewController *owningSecondViewController;

In SecondViewController.m :

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"..."]) {
        SecondViewControllerModal *modalViewController = (SecondViewControllerModal *)segue.destinationViewController;
        modalViewController.owningSecondViewController = self;
    }
}

Finally:

- (IBAction)addArray:(id)sender
{
    NSArray *content = [[NSArray alloc] initWithObjects:subjectName.text, setLetter.text, setOrder.text , nil];

    [self.owningSecondViewController addToArray:content];
}

Need Your Help

How to set up a robot.txt which only allows the default page of a site

web-crawler bots robots.txt googlebot slurp

Say I have a site on http://example.com. I would really like allowing bots to see the home page, but any other page need to blocked as it is pointless to spider. In other words

Make sprite in GameScene.sks and access it in code

ios swift sprite-kit skscene

I was making an app in Sprite Kit and I didn't want to design my level in code so I used my GameScene.sks file and visually made my level and I named each of the nodes I made in the GameScene.sks. ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.