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

心已入冬 提交于 2019-12-10 11:36:38

问题


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:


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.



来源:https://stackoverflow.com/questions/12200630/how-to-call-viewdidload-after-self-dismissmodalviewcontrolleranimatedyes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!