0

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.

Community
  • 1
  • 1
jsetting32
  • 1,632
  • 2
  • 20
  • 45
  • Why did NSNotificationCenter not work? – random Jul 24 '13 at 22:36
  • Your best bet is most likely to use **Unwind Segues**. Here's a very in-depth [Q&A on StackOverflow regarding Unwind Segues](http://stackoverflow.com/questions/12561735/what-are-unwind-segues-for-and-how-to-use-them) – Sam Spencer Jul 24 '13 at 22:37
  • I am not using storyboards though... Can i use segues without storyboards? Sorry thats probably a dumb Q if it is allowed :/ – jsetting32 Jul 24 '13 at 22:47

2 Answers2

2

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.

Hitesh
  • 542
  • 3
  • 12
1

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'.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • Is there any reason why the notification code is returning to me null? I feel like it should be working... :( And it seems that delegation takes a whole lot more code to implement than NSNotificationCenter... I feel like NSNotificationCenter is the best bet for this but apparently it's not working :( – jsetting32 Jul 24 '13 at 23:03
  • When you post the notification, what is `cell`? – Wain Jul 24 '13 at 23:06
  • cell.textLabel.text logs the statement EDF ENRS which is a title of a cell I press, then the tableview is dismissed and the main view is presented – jsetting32 Jul 24 '13 at 23:11
  • The notification method is called before the dismiss happens. Have you logged the other information passed with the notification? – Wain Jul 24 '13 at 23:16
  • SO I got it to work. For some reason, the method implemented wasnt being called. I moved it from ViewDidAppear to ViewWillAppear and It worked... wierd... – jsetting32 Jul 24 '13 at 23:24
  • You should probably just add the observer once in `viewDidLoad`. – Wain Jul 24 '13 at 23:29