如何在 [self deniedModalViewControllerAnimated:YES] 之后调用 viewDidLoad; [英] How to call viewDidLoad after [self dismissModalViewControllerAnimated:YES];
问题描述
好的.如果你有两个 viewController 并且你从第一个到第二个执行模态 Segue,那么你可以用 [self deniedModalViewControllerAnimated:YES] 关闭它;它似乎不记得viewDidLoad.我有一个主页 (viewController),然后是一个选项页面,我希望在您更改选项时更新主页.当我只做了两个模态转场(一个前进,一个后退)时,这很有效,但这似乎是非结构化的,可能会导致大型项目中的代码混乱.
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.
我听说过push segues.他们好些了吗?
I have heard of push segues. Are they any better?
谢谢.我感谢任何帮助:)
Thanks. I appreciate any help :).
推荐答案
那是因为 UIViewController 已经加载到内存中了.但是,您可以使用 viewDidAppear:.
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.
后一种方法的好处是不需要重新运行 viewDidAppear:
的整个主体.例如,如果您只更新表格行,为什么要重新渲染整个内容?
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?
这里有一个使用委托的简单示例,仅供您参考:
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];
}
}
输出:
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
示例在 CodeRunner 中针对 OS X 运行,我与之零从属关系.
Example was ran in CodeRunner for OS X, whom I have zero affiliation with.
这篇关于如何在 [self deniedModalViewControllerAnimated:YES] 之后调用 viewDidLoad;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!