简体   繁体   中英

Passing data to parent viewcontroller from tableviewcontroller

so currently I have a tableviewcontroller that gets presented modally above the loginviewcontroller if a certain case occurs, then once the user presses on a cell, the title should be passed to the parent view controller.

So picture (A) being the parent - the mainview, (B) being the login, and (C) being the tableviewcontroller...

C sits on top of B which sits on top of A, NOT THROUGH A NAVIGATION STACK BUT THROUGH A MODAL STACK. So C doesn't have any reference to A other than this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self Login];
    [self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}

When a user presses on a cell, the view then goes straight to (A), so from (C) to (A) skipping (B). This is what I wanted... But the issue is how do I pass data to (A) without having to instantiate (A) again? I mean instantiate again by, it's already instantiated in the beginning of the app launch and (B) is presented over (A) if the user isn't already logged in. So (A) is ALWAYS loaded, but how do I pass data to it?

If there is more information that should be provided, please inform me and i'll edit ASAP.

EDIT I looked at a few more questions that related to this one, would I use NSNotification? Or delegation? I attempted to use both but none really worked... I refered to my previously answered question on delegation to implement the delegation solution but I have to instantiate (C) in (A). But I already instantiate (C) in (B). Heres the delgation solution I was gonna use but didn't: Delegation . Here is the Link for the NSNotification solution i attempted to use:

Put this in your parent controller in viewDidLoad

- (void) viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];

    [self makeCallbacks];
    // get register to fetch notification
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(yourNotificationHandler:)
                                                 name:@"MODELVIEW DISMISS" object:nil];
}

//Now create yourNotificationHandler: like this in parent class
-(void)yourNotificationHandler:(NSNotification *)notice{
    str = [notice object];

}

Put following to your child class where

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSLog(@"%@", cell.textLabel.text);
    // cell.textLabel.text logs this -> EDF ENRS
    [[NSNotificationCenter defaultCenter] postNotificationName:@"MODELVIEW DISMISS" object:cell.textLabel.text];
    [self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}

When I log 'str' (null) is printed

Now the issue is when I log str outside of the notification later in viewWillAppear, it keeps returning null.... It only DOESNT return null when I log str within the notification method... I'll try to figure this out later but the primary issue is i guess solved using NSNotificationCenter.

The problem behind the NSNotificationCenter not working, is with it being declared in viewDidAppear. Make sure, its appearing and written somewhere where the changes are made , everytime the screen is displayed, like viewWillAppear .

And then, make the necessary changes to the parentView, in this viewWillAppear method.

Also, be sure to remove the notifications, inside the methods that are called, when those notifications are posted.

Your notification code should work, unless you're removing the observer at some point in time.

Delegation will also work, you just need to pass the delegate down through the instances (A pass to B, B pass on to C).

Blocks are another option, similar to delegation but with a block type instead of a protocol. Again, passed down through the instances.


Blocks are probably your best option. Notifications should be used where multiple items are interested in the event and those items aren't necessarily associated with the trigger of the event. That said, notifications are the 'least code option'.

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