How to call viewDidLoad after [self rejectModalViewControllerAnimated: YES];

Good. If you have two viewControllers and you make a modal Segue from first to second, you reject it with [self rejectModalViewControllerAnimated: YES]; it doesn't seem to call viewDidLoad. I have a master page (viewController), then a sorting options page, and I want the master page to refresh when the setting changes. This worked when I had just made two modal segments (one went forward, one came back), but it seemed unstructured and could lead to messy code on large projects.

I've heard about aftershocks. Are they better?

Thank. I appreciate any help :).

+1


source to share


1 answer


This is because the UIViewController is already loaded into memory. However, you can use viewDidAppear: .

Alternatively, you can make the pushing view controller a delegate to the pushed view controller and notify the update when the pushed controller exits the screen.

The latter method has the advantage of not having to rerun the entire body viewDidAppear:

. If you are only updating a table row, for example, why re-display the whole thing?

EDIT: Just for you, here's a quick example of using delegates:



#import <Foundation/Foundation.h>

// this would be in your ModalView Controller .h
@class ModalView;

@protocol ModalViewDelegate

- (void)modalViewSaveButtonWasTapped:(ModalView *)modalView;

@end

@interface ModalView : NSObject

@property (nonatomic, retain) id delegate;

@end

// this is in your ModalView Controller .m
@implementation ModalView

@synthesize delegate;

- (void)didTapSaveButton
{
    NSLog(@"Saving data, alerting delegate, maybe");

    if( self.delegate && [self.delegate respondsToSelector:@selector(modalViewSaveButtonWasTapped:)])
    {
        NSLog(@"Indeed alerting delegate");

        [self.delegate modalViewSaveButtonWasTapped:self];
    }
}

@end

// this would be your pushing View Controller .h
@interface ViewController : NSObject <ModalViewDelegate>

- (void)prepareForSegue;

@end;

// this would be your pushing View Controller .m
@implementation ViewController

- (void)prepareForSegue
{
    ModalView *v = [[ModalView alloc] init];

    // note we tell the pushed view that the pushing view is the delegate
    v.delegate = self;

    // push it

    // this would be called by the UI
    [v didTapSaveButton];
}

- (void)modalViewSaveButtonWasTapped:(ModalView *)modalView
{
    NSLog(@"In the delegate method");
}

@end

int main(int argc, char *argv[]) {
    @autoreleasepool {

        ViewController *v = [[ViewController alloc] init];

        [v prepareForSegue];
    }
}

      

Outputs:

2012-08-30 10:55:42.061 Untitled[2239:707] Saving data, alerting delegate, maybe
2012-08-30 10:55:42.064 Untitled[2239:707] Indeed alerting delegate
2012-08-30 10:55:42.064 Untitled[2239:707] In the delegate method

      

The example ran in CodeRunner for OS X, with which I have zero ownership.

+10


source







All Articles