这似乎是整个 Apple 应用程序中使用的模式;新记录的创建是通过模态视图完成的,需要保存或取消才能继续,编辑记录是通过推送到导航堆栈的 View 完成的。
基本上复制我的 ViewController 以进行“添加”和“编辑”似乎是不对的,但是推送和模态 ViewController 的工作方式存在一些差异,这使事情变得复杂。
我应该怎么做才能涵盖这两个基础?
-
差异包括。
当插入堆栈时,导航栏出现在 View 的顶部,并且可以配置为包含取消/保存按钮。当以模态方式呈现时,情况并非如此,因此要复制界面,需要单独创建工具栏并将关闭/保存按钮添加到其中。
当关闭推送 View 时,我们向导航 Controller 发送一条消息[self.navigationController popViewControllerAnimated:YES];
,当关闭模态视图时,我们向自身发送一条消息[self dismissModalViewControllerAnimated :YES];
最佳答案
您可以在 InterfaceBuilder 中添加 UIToolbar,然后在 self.navigationController 不为 nil 时将其隐藏在 viewDidLoad 中。
至于解雇,你可以有这样的东西:
- (void)didCancel {
[self.navigationController popViewControllerAnimated:YES] || [self dismissModalViewControllerAnimated:YES];
}
如果您的 View Controller 是导航控件的一部分,这将短路,否则使用 dismissModalViewControllerAnimated。
这应该适用于您的取消按钮。对于您的保存按钮,调用某种委托(delegate)方法很有用,例如:
- (void)didSave {
// do your saving juju here
if([self.delegate respondsToSelector:@selector(viewController:didSave:]) {
[self.delegate viewController:self didSave:whatJustGotSaved];
}
[self.navigationController popViewControllerAnimated:YES]; // noop if currently modal
}
然后在委托(delegate)的实现中,您可以放置:
- (void)viewController:(UIViewController*)viewController didSave:(NSObject*)whatJustGotSaved {
// do stuff with parameters
[self.modalViewController dismissModalViewControllerAnimated:YES]; // noop if not modal
}
关于objective-c - 我是否应该使用相同的细节 ViewController 在模态和推送时工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10348203/