我有一个这样的容器 View :
主要的称为 ConfigViewContainer,子称为 ConfigDistrictViewController,上面有一个 UIPicker。 如果我不在 Storyboard 中将 ConfigDistrictViewController 设置为隐藏,那么它就会显示。
现在我想在 ConfigViewController 文本字段控件中执行触地事件时通过代码显示它。
var configDistrictViewController: ConfigDistrictViewController?
var uiView : UIView?
@IBAction func selectDistrictTouchDown(sender: AnyObject) {
self.performSegueWithIdentifier("ConfigDistrictSelectionSegue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "ConfigDistrictViewController")
{
configDistrictViewController = segue.destinationViewController as? ConfigDistrictViewController
configDistrictViewController?.showContainer()
// Also tried the following
uiView = configDistrictViewController?.view
uiView?.hidden = false
}
}
这是 ConfigDistrictViewController 中的代码:
func showContainer()
{
println("showContainer")
self.view.hidden = true
}
但我得到的只是这样的错误:
失败发生在这里:
configDistrictViewController = segue.destinationViewController as? ConfigDistrictViewController
所以我想我可能正在使用:
self.performSegueWithIdentifier("ConfigDistrictSelectionSegue", sender: self)
不正确
最佳答案
当 ConfigViewContainer
被实例化时,它的 child ConfigDistrictViewController
也被实例化,因此 segue 已经被执行。您可以通过实现 shouldPerformSegueWithIdentifier:
来防止执行 segue(当 ConfigViewContainer
被实例化时),而不是隐藏 View Controller 。并返回错误。当您在代码中调用 performSegueWithIdentifier
时,不会调用该方法,因此您应该可以这样做,
-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
if ([identifier isEqualToString:@"Embed"]) { // The embed segue in IB was given this identifier. This method is not called when calling performSegueWithIdentifier:sender: in code (as in the button method below)
return NO;
}else{
return YES;
}
}
- (IBAction)showEmbed:(UIButton *)sender {
[self performSegueWithIdentifier:@"Embed" sender:self];
}
我从以前的项目中获取了这段代码。如果您需要我将它翻译成 Swift,我可以。
关于ios - 快速显示隐藏的容器 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29981510/