[英]React on Modal ViewController being dismissed
I have three ViewControllers and one navigation controller. 我有三个ViewController和一个导航控制器。 The Navigation Stack is:
导航堆栈为:
NavigationController-push->VC1-push->VC2. NavigationController-推> VC1,推> VC2。 VC1 can modally present VC3 in code.
VC1可以以代码形式形式呈现VC3。 Its not connected via segues.
它没有通过segues连接。
VC1 - "Your current projects" VC1-“您当前的项目”
VC2 - "Details of your project" VC2-“您的项目的详细信息”
VC3 - "Create new project" VC3-“创建新项目”
When user desires to create a new project, I put a VC3 using: 当用户希望创建一个新项目时,我使用以下命令放置了VC3:
- (IBAction)newProjectButton:(id)sender {
NewProjectViewController *newProject = [[NewProjectViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:newProject];
[self presentViewController:navController animated:YES completion:nil];
}
I user presses "Cancel" button, I use this code: 我用户按下“取消”按钮,我使用此代码:
- (IBAction)cancelButton:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
When user presses "Done" button I want VC1 to make segue to VC2 and show new project's properties. 当用户按下“完成”按钮时,我希望VC1切换到VC2并显示新项目的属性。 I would like this segue to be invisible for user, so, he only sees this chain of events:
我希望用户无法看到此序列,因此,他只看到以下事件链:
Presses the button "add" -> modal VC appears -> presses "Done" -> Modal VC disappears and VC3 is already shown. 按下按钮“添加”->模态VC出现->按下“完成”->模态VC消失并且VC3已经显示。
What I am asking is how to tell VC1 that user pressed button "Done"? 我要问的是如何告诉VC1用户按下了“完成”按钮? Is delegation possible here?
这里可以委托吗? How to implement it?
如何执行呢? Thank you.
谢谢。
You should create a delegate protocol which allows the modal view controller to send notifications to its creator. 您应该创建一个委托协议,该协议允许模态视图控制器向其创建者发送通知。
@protocol ModalViewControllerDelegate
@optional
- (void)modalViewControllerDidCancel:(ModalViewController *)vc;
- (BOOL)modalViewControllerShouldSave:(ModalViewController *)vc;
@end
Then, in ModalViewController
you define a new property. 然后,在
ModalViewController
定义一个新属性。 The weak
is important, because you don't want to have any retain cycles. weak
很重要,因为您不想有任何保留周期。
@property (nonatomic, weak) id <ModalViewControllerDelegate> delegate;
Before dismissing or saving, just check if the delegate has implemented the methods (via -respondsToSelector:
) and send the appropriate callbacks. 在关闭或保存之前,只需检查委托是否已实现方法(通过
-respondsToSelector:
并发送适当的回调。 Don't forget setting the delegate property when creating your modal view controller. 创建模态视图控制器时,请不要忘记设置委托属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.