How to call viewDidLoad after [self dismissModalVi

2019-05-30 06:22发布

Okay. If you have two viewControllers and you do a modal Segue from the first to the second, then you dismiss it with [self dismissModalViewControllerAnimated:YES]; it doesn't seem to recall viewDidLoad. I have a main page (viewController), then a options page of sorts and I want the main page to update when you change an option. This worked when I just did a two modal segues (one going forward, one going back), but that seemed unstructured and may lead to messy code in larger projects.

I have heard of push segues. Are they any better?

Thanks. I appreciate any help :).

1条回答
Luminary・发光体
2楼-- · 2019-05-30 07:20

That's because the UIViewController is already loaded in memory. You can however use viewDidAppear:.

Alternatively, you can make the pushing view controller a delegate of the pushed view controller, and notify it of the updates when the pushed controller is exiting the screen.

The latter method has the benefit of not needing to re-run the entire body of viewDidAppear:. If you're only updating a table row, for example, why re-render the whole thing?

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

#import <Foundation/Foundation.h>

// this would be in your ModalView Controller's .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's .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's .h
@interface ViewController : NSObject <ModalViewDelegate>

- (void)prepareForSegue;

@end;

// this would be your pushing View Controller's .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

Example was ran in CodeRunner for OS X, whom I have zero affiliation with.

查看更多
登录 后发表回答