swift显示隐藏的容器视图

时间:2015-05-01 04:15:00

标签: ios iphone swift

我有一个像这样的容器视图:

enter image description here

主要名为ConfigViewContainer,子名称为ConfigDistrictViewController,上面有UIPicker。 如果我没有在故事板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)

错误地

1 个答案:

答案 0 :(得分:1)

ConfigViewContainer被实例化时,其子ConfigDistrictViewController也是,因此segue已经被执行。您可以通过实现ConfigViewContainer来阻止执行segue(在shouldPerformSegueWithIdentifier:实例化时),而不是隐藏视图控制器。并返回false。在代码中调用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,我可以。