adviner
adviner

Reputation: 3557

swift showing a hidden container view

I have a container view like this:

enter image description here

The main is called ConfigViewContainer and the child is called ConfigDistrictViewController which has a UIPicker on it. If I dont set in the storyboard ConfigDistrictViewController as hidden then it shows.

enter image description here

Now I want to show it via code when i do a touchdown event in the ConfigViewController textfield control.

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


    }
}

Here is the code in the ConfigDistrictViewController:

func showContainer()
{
    println("showContainer")
    self.view.hidden = true
}

but all i get is error like this:

enter image description here

The failure happens here:

        configDistrictViewController = segue.destinationViewController as? ConfigDistrictViewController

so I'm thinking I may be using:

self.performSegueWithIdentifier("ConfigDistrictSelectionSegue", sender: self)

incorrectly

Upvotes: 1

Views: 1510

Answers (1)

rdelmar
rdelmar

Reputation: 104082

When the ConfigViewContainer is instantiated, its child, ConfigDistrictViewController is also, so that segue has already been executed. Instead of hiding the view controller, you can prevent the segue from being executed (when ConfigViewContainer is instantiated) by implementing shouldPerformSegueWithIdentifier:. and returning false. That method is not called when you call performSegueWithIdentifier in code, so you should be able to do something like this,

-(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];
}

I took this code from a previous project. If you need me to translate it to Swift, I can.

Upvotes: 1

Related Questions