ios - 快速显示隐藏的容器 View

标签 ios iphone swift

我有一个这样的容器 View :

enter image description here

主要的称为 ConfigViewContainer,子称为 ConfigDistrictViewController,上面有一个 UIPicker。 如果我不在 Storyboard 中将 ConfigDistrictViewController 设置为隐藏,那么它就会显示。

enter image description here

现在我想在 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
}

但我得到的只是这样的错误:

enter image description here

失败发生在这里:

        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/

相关文章:

objective-c - 有没有办法在UITableView(分组)中隐藏单元格?

iphone - slrequest 中的应用程序 ID

iphone - 过度释放 CFNetwork 中的数组?

swift - 调整 Swift 代码以与 Swift 3 配合使用

ios - GMSCameraPosition 不准确

iphone - 有人可以向我解释 iPhone 的数据保护和加密吗?

ios - 避免向 Realm 添加重复对象

ios - 从 UITableViewCell 将 UIImageView 扩展到全屏

iphone - 在 iTunesConnect 上添加应用程序购买只允许免费订阅

ios - 如何键入自动完成中显示的方法名称的下一部分?