1

I've been stuck with this problem for a while now, can't find any useful information on how to do this..

I have a base view (view 1), where I can select items in a tableview. While on the items "page" (view 2) I can choose to edit that item, triggering a modalview (view 3). In this modalview, I have the option to delete this item. If the user pressed that button and confirms they want to delete the item, I want to send the app back to view 1..

I've tried a number of different things (popToViewController, pushViewController, dismissViewController etc etc) but I can't get anything to work. If I get the modal to close, view 2 doesn't close. Sometimes even the modal doesn't disappear. The base view is a UITableViewController, the other two are UIViewControllers, and I'm using storyboard.

Carl Veazey
  • 18,392
  • 8
  • 66
  • 81

1 Answers1

1

You have several options you can either use NSNotificationCenter or use the delegate pattern. NSNotificationCenter is easy to use but also it is tricky.

To use notification center you need to add observers to your view controller classes.When you dismiss your modal view controller you notify your view 2 that view 3 is dismissed now, view2 can dismiss itself.....

So basically when you notify the center, whatever in notified it runs a method etc....

Lets say your at view 3, you want to dismiss your views.

in view3 .m

-(IBAction)yourMethodHere
{
        //dissmiss view
        [self.navigationController dismissModalViewControllerAnimated:YES];
         // or [self dismissModalViewControllerAnimated:YES]; whateever works for you 

        //send notification to parent controller and start chain reaction of poping views
        [[NSNotificationCenter defaultCenter] postNotificationName:@"goToView2" object:nil];
}

in view 2 . h

// For name of notification
extern NSString * const NOTIF_LoggingOut_Settings;

in view 2. m before @implementation after#imports

NSString * const NOTIF_LoggingOut_Settings = @"goToView2";

    @implementation
    -(void)viewDidAppear:(BOOL)animated{

        // Register observer to be called when logging out
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(loggingOutSettings:)
                                                     name:NOTIF_LoggingOut_Settings object:nil];
    }
    /*---------------------------------------------------------------------------
     * Notifications of 2 view
     *--------------------------------------------------------------------------*/
    - (void)loggingOutSettings:(NSNotification *)notif
    {
        NSLog(@"Received Notification - Settings Pop Over  popped");

        [self.navigationController popViewControllerAnimated:NO];// change this if you do not have navigation controller 

//call another notification to go to view 1 
        [[NSNotificationCenter defaultCenter] postNotificationName:@"goToFirstView" object:nil];
    }

add another observer to your first view in your view1.h extern NSString * const NOTIF_FirstView;

in view 1. m before @implementation after#imports

NSString * const NOTIF_FirstView = @"goToFirstView";

@implementation
-(void)viewDidAppear:(BOOL)animated{

    // Register observer to be called when logging out
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(doYourThing:)
                                                 name:NOTIF_FirstView object:nil];
}
/*---------------------------------------------------------------------------
 * Notifications of 1 view
 *--------------------------------------------------------------------------*/
- (void)ldoYourThing:(NSNotification *)notif
{


 // do your thing
}
Community
  • 1
  • 1
SpaceDust__
  • 4,844
  • 4
  • 43
  • 82
  • Great explanation, thank you! However - I can't get the second view to go out of the screen. The modal disappears, then the NSLog output from view is triggered and after that an NSLog I put in view1 viewDidAppear is triggered. So it goes down the line, the second screen just isn't removed.. – Christopher Carlsson Jan 10 '13 at 08:10
  • this `[self.navigationController popViewControllerAnimated:NO]` method works when you have navigation controlllers make sure to use correct method that suits your code , it seems notifications work coreectly you just need to change your `dismiss` method – SpaceDust__ Jan 10 '13 at 14:21
  • What other ways of dismissing a view is there? If popViewController doesn't work, what is the problem then? I'm a beginner developer.. – Christopher Carlsson Jan 11 '13 at 07:19
  • pop view controller only work if you push the view to stack earlier. I have no idea about the possible problem since you have no code sample or your story boards screen shot in your question – SpaceDust__ Jan 11 '13 at 14:46
  • I solved it by doing `[self.navigationController popToViewController:senderClass animated:NO];`, where senderClass is just a reference sent in from the parent class to itself. Thank you very much for your help! – Christopher Carlsson Jan 12 '13 at 15:01