1

I know this question is asked once every two days. I can not see what I am doing wrong though. I have a storyboard navigation controller based app.

My notification and pop / push segues works well, only thing is I can not add string to parents view NSmutablearray.

I want to add a string object to parent view's nsmutablearray. My decent code does not pass any data.

parent.h

@interface CreaatePlistTableViewController : UITableViewController<UITableViewDelegate, UITableViewDataSource>{

    NSMutableArray *presenterList;

}
@property (nonatomic, strong) NSMutableArray *presenterList;

parent.m

NSString * const NOTIF_CreatePlist_UpdateTableview= @"CreatePlist/UpdateTableview";
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * Private interface definitions for update tableview
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
@interface CreaatePlistTableViewController (private)
- (void)CreatePlistUpdateTableview:(NSNotification *)notif;
@end

@implementation CreaatePlistTableViewController
@synthesize presenterList=_presenterList;

- (void)viewDidLoad
{
    [super viewDidLoad];

    _presenterList=[[NSMutableArray alloc] init];

    // Register observer to be called when logging out
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(CreatePlistUpdateTableview:)
                                                 name:NOTIF_CreatePlist_UpdateTableview object:nil];

    NSLog(@"Presenter List: %@", _presenterList);

}


- (void)CreatePlistUpdateTableview:(NSNotification *)notif{
    NSLog(@"Notification recieved");
    NSLog(@"Presenter List: %@", _presenterList);
    [_createPlistTableview reloadData];
}

child.h

@interface AddPresenterViewController : UITableViewController<UITextFieldDelegate,UIAlertViewDelegate>{
    CreaatePlistTableViewController *crereaatePlistTableViewController;
}
@property(nonatomic,strong) CreaatePlistTableViewController *crereaatePlistTableViewController;

child.m

@synthesize crereaatePlistTableViewController=_crereaatePlistTableViewController;
//finished adding presenter
-(IBAction)finishedAddingPresenter:(id)sender{

    //some xml string here
    NSLog(@"final result XML:\n%@", writer.XMLString);


    _crereaatePlistTableViewController=[[CreaatePlistTableViewController alloc]init];
    //add object to parents view data source
    [_crereaatePlistTableViewController.presenterList addObject:writer.XMLString];

    //dismiss the view
     [self.navigationController popViewControllerAnimated:YES];

    //notify the parent view to update its tableview

    [[NSNotificationCenter defaultCenter] postNotificationName:@"CreatePlist/UpdateTableview" object:nil];

}

Output

Notification recieved
Presenter List: (
)

So notification works when I click the button. But it does not pass object to nsmutablearray.

What I am doing wrong here ? How can I add an object to parent view's nsmutablearray?

Mord Fustang
  • 1,523
  • 4
  • 40
  • 71
  • How do you pass the parent controller to the child controller ? (i.e. does the child's crereaatePlistTableViewController points to the same parent instance ? ) – giorashc Nov 06 '12 at 14:53
  • I dont pass any data to child view but I push child view with push segue. Besides my switching and notification methods seems working only problem is it does not add string to parents nsmutablearray – Mord Fustang Nov 06 '12 at 14:55
  • Stop using notification and start using blocks : http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/Blocks/Articles/bxUsing.html – TheRonin Nov 06 '12 at 15:13

2 Answers2

0

You wrote this.

[_crereaatePlistTableViewController.presenterList addObject:writer.XMLString];

Do you ever initialize the array? No. Use the debugger and you will see that at this line the presenterList is nil.

Now as a point of style. Avoid using NSNotificationCenter to pass data or signaling other objects. @TheRonin gave a handy link. You should also look into some tutorials on Segues, because these are solved problems.

This is another related post that you might find interesting.

Community
  • 1
  • 1
Cameron Lowell Palmer
  • 21,528
  • 7
  • 125
  • 126
0

It seems everything is good except your alloc of parent view object I am not that familiar with storyboard but You said you are using navigation navigation controller

so change this

 _crereaatePlistTableViewController=[[CreaatePlistTableViewController alloc]init];

to

 _crereaatePlistTableViewController= [self.navigationController.viewControllers objectAtIndex:0];

It may work I am not sure

SpaceDust__
  • 4,844
  • 4
  • 43
  • 82