[英]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.代表/协议
finalStepViewController.h
inside MainViewController.h
and appended <finalStepViewControllerDelegate>
在MainViewController.h
导入了finalStepViewController.h
,并附加了<finalStepViewControllerDelegate>
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 在模型中
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'
[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.