简体   繁体   English

从模式视图或推送视图调用父方法到presentingViewController

[英]Calling parent method from a modal or push view to presentingViewController

I have a UINavigationGroup with a root view controller called MainViewController . 我有一个UINavigationGroup带有一个名为MainViewController的根视图控制器。 Inside this MainViewController I'm calling another UINavigationController as a modal as following: 在这个MainViewController我将另一个UINavigationController称为模态,如下所示:

- (IBAction)didTapButton:(id)sender {
    UINavigationController * someViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"someNavigationController"];
    [self.navigationController presentViewController:someViewController animated:YES completion:nil];
}

Inside this someNavigationController , the user is going through some process so the nav controller is being pushed with some UIViewControllers . someNavigationController内部,用户正在经历一些过程,因此导航控制器被一些UIViewControllers推入。 After the user completes the process, in the last UIViewController called finalStepViewController , I'm closing the modal as follow: 用户完成此过程后,在最后一个名为finalStepViewController UIViewController ,我将关闭模态,如下所示:

[self dismissViewControllerAnimated:YES completion:nil];

The modal is indeed dismissed and the user is back to the initial MainViewController . 模态确实被取消了,用户又回到了最初的MainViewController However, I'd like to push another UIViewController to MainViewController 's NavigationController (for example: a view saying that the user completed the process successfully). 但是,我想将另一个UIViewController推送到MainViewController的NavigationController中(例如:一个视图,表明用户成功完成了该过程)。 Preferably before the modal is dismissed. 优选地,在模态被消除之前。

I have tried the following things: 我尝试了以下操作:

1. Using presentingViewController 1.使用presentingViewController

UIViewController * successViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"successViewController"];
[self.presentingViewController.navigationController successViewController animated:YES];

Result: no error, but nothing happening either. 结果:没有错误,但是也没有任何反应。

2. Delegate/protocol 2.代表/协议

  • Imported finalStepViewController.h inside MainViewController.h and appended <finalStepViewControllerDelegate> MainViewController.h导入了finalStepViewController.h ,并附加了<finalStepViewControllerDelegate>
  • Inside MainViewController.m added a method called parentMethodThatChildCanCall to be called from finalStepViewController.m MainViewController.m内部添加了一个名为parentMethodThatChildCanCall的方法,该方法将从finalStepViewController.m
  • Added the following to finalStepViewController.h : finalStepViewController.h添加了以下finalStepViewController.h

    @protocol finalStepViewControllerDelegate <NSObject> -(void)parentMethodThatChildCanCall; @end @protocol finalStepViewControllerDelegate <NSObject> -(void)parentMethodThatChildCanCall; @end @property (assign) id <finalStepViewControllerDelegate> delegate; @protocol finalStepViewControllerDelegate <NSObject> -(void)parentMethodThatChildCanCall; @end @property (assign) id <finalStepViewControllerDelegate> delegate; and @synthesize delegate; @synthesize delegate; in the model 在模型中

  • Set the delegate property to someViewController in the above mentioned didTapButton IBAction to self. 在上述didTapButton IBAction中将委托属性设置为someViewController ,将其设置为self。 This showed a notice error saying: Assigning to id<UINavigationControllerDelegate>' from incompatible type UIViewController *const __strong' 这显示了一个通知错误消息: Assigning to id<UINavigationControllerDelegate>' from incompatible type UIViewController *const __strong'
  • Finally called [self.delegate parentMethodThatChildCanCall] just before closing the modal. 最后在关闭模式之前调用了[self.delegate parentMethodThatChildCanCall]

Result: except for the notice error, no fail but nothing happens as parentMethodThatChildCanCall is not called. 结果:除通知错误外,没有失败,但未发生任何事情,因为未调用parentMethodThatChildCanCall

Any idea what I'm doing wrong/what I should be doing? 知道我做错了/应该做什么吗? It's my second week doing Objective-C, and most of the time I don't know what I'm doing so any help/code would be appreciated! 这是我第二周在做Objective-C,大部分时间我都不知道我在做什么,因此任何帮助/代码都将不胜感激!

Thanks. 谢谢。

You can achieve this a lot easier using NSNotificationCenter . 您可以使用NSNotificationCenter轻松实现这一点。

In your MainViewController 's -viewDidLoad add the following code 在您的MainViewController-viewDidLoad添加以下代码

  typeof(self) __weak wself = self;
  [[NSNotificationCenter defaultCenter] addObserverForName:@"successfullActionName"
                                                    object:nil
                                                     queue:[NSOperationQueue mainQueue]
                                                usingBlock:^(NSNotification *note) {
                                                  SuccessViewController *viewController; // instantiate it properly
                                                  [wself.navigationController pushViewController:viewController animated:NO];
                                                }];

Remove your controller from NSNotificationCenter upon dealloc 取消分配时从NSNotificationCenter删除控制器

- (void)dealloc {
  [[NSNotificationCenter defaultCenter] removeObserver:self];
}

In FinalStepViewController on action that dismisses the view controller before dismiss post the notification FinalStepViewController上执行的操作,在关闭视图之前关闭视图控制器

- (IBAction)buttonTapped:(id)sender {
  [[NSNotificationCenter defaultCenter] postNotificationName:@"successfullActionName" object:nil];
  [self dismissViewControllerAnimated:YES completion:nil];
}

This example is very crude and is not ideal, you should use constants for your notification names and in some cases store the observers returned by NSNotificationCenter to remove specific ones. 此示例非常粗糙,也不理想,您应使用常量作为通知名称,并在某些情况下存储NSNotificationCenter返回的观察者以除去特定的观察者。

-- EDIT I'd like to also mention that the method addObserverForName:object:queue:usingBlock: actually returns the observer as an id type object. -编辑我还要提到方法addObserverForName:object:queue:usingBlock:实际上将观察者作为id类型的对象返回。 You need to store a reference to it as an iVar in your class and remove it from the NSNotificationCenter when dealloc method is called otherwise that observer will never get deallocated. 您需要在类中将对它的引用存储为iVar,并在调用dealloc方法时将其从NSNotificationCenter删除,否则观察者将永远不会被释放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 presentingViewController返回实际呈现视图控制器的父级 - presentingViewController returns the parent of the actual presenting view controller UINavigationItem模态如何从presentingViewController继承颜色 - UINavigationItem modal how to inherit color from presentingviewcontroller 使用Storyboard Segues将视图控制器从模态推送到模态父级使用的导航控制器 - Push view controller from modal into navigation controller used by parent of the modal using Storyboard segues 模态视图关闭时如何获取精确的PresentingViewController - How to get exact PresentingViewController when Modal View dismissed 在解除模态视图控制器的动画时访问presentViewController - Accessing presentingViewController while dismissal animation of a Modal View Controller 从模式视图中推送视图,然后从popToRootView中推送 - Push view from modal view and then popToRootView 从故事板中的模态视图推送视图控制器 - Push View Controllers from Modal View in Storyboard SwiftUI 从模态视图使用 NavigationLink 推送视图 - SwiftUI Push View with NavigationLink from modal view 使用委托从模态视图控制器中调用另一个视图控制器中的方法 - Calling method in another view controller from modal view controller using a delegate 将视图从另一个选项卡的模式推送到导航 - Push view to navigation from modal of another tab
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM