Rectangle 27 2

Firstly, go into the container view's View Controller and make sure your two views are variables, either via Interface Builder or Code. Let's say you called them view1 and view2.

viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "segmentedControlTapped:", name: "SCTapped", object: nil)

Then, make a new function like this:

func segmentedControlTapped(notif: NSNotification){
    let index = notif.userInfo["index"] as Int

    if index == 0{
        view1.hidden = false
        view2.hidden = true
    }
    else if index == 1{
        view1.hidden = true
        view2.hidden = false
    }
}

Then, in the View Controller housing your Segmented Control, hook up an IBAction (if using IB) to the Control's ValueChanged action or use code.

@IBAction func tapped(sender: UISegmentedControl){
    NSNotificationCenter.defaultCenter().postNotificationName("SCTapped", object: nil, userInfo: ["index": sender.selectedSegmentIndex])
}

What this should do, is when the SC is tapped, it will call the tapped function, which tells the NSNotificationCenter to post a message. This should be received by the VC with the views in it and segmentedControlTapped() should be called, and it will switch your views.

adInfoViewController.hidden = false
does not have a member named 'hidden'
var adInfoViewController = AdInfoViewController()

Yes, you can't hide a UIViewController because it is not the same as a UIView. You need to make the two UIViews you want to hide first. Sorry, I assumed you already had them.

But can i make this with 2 viewcontrollers, so it can be separated, for better control? Maybe with instantiateViewControllerWithIdentifier? If you have code for that?

Ok, with 2 view controllers instead of two UIViews, it is actually much easier. All you need is 2 container views in the same place, and when you tap the segmented control, you hide one and show the other, and vice versa.

objective c - Ios swift container view and segmented control switch vi...

ios objective-c swift uisegmentedcontrol uicontainerview
Rectangle 27 2

Firstly, go into the container view's View Controller and make sure your two views are variables, either via Interface Builder or Code. Let's say you called them view1 and view2.

viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "segmentedControlTapped:", name: "SCTapped", object: nil)

Then, make a new function like this:

func segmentedControlTapped(notif: NSNotification){
    let index = notif.userInfo["index"] as Int

    if index == 0{
        view1.hidden = false
        view2.hidden = true
    }
    else if index == 1{
        view1.hidden = true
        view2.hidden = false
    }
}

Then, in the View Controller housing your Segmented Control, hook up an IBAction (if using IB) to the Control's ValueChanged action or use code.

@IBAction func tapped(sender: UISegmentedControl){
    NSNotificationCenter.defaultCenter().postNotificationName("SCTapped", object: nil, userInfo: ["index": sender.selectedSegmentIndex])
}

What this should do, is when the SC is tapped, it will call the tapped function, which tells the NSNotificationCenter to post a message. This should be received by the VC with the views in it and segmentedControlTapped() should be called, and it will switch your views.

adInfoViewController.hidden = false
does not have a member named 'hidden'
var adInfoViewController = AdInfoViewController()

Yes, you can't hide a UIViewController because it is not the same as a UIView. You need to make the two UIViews you want to hide first. Sorry, I assumed you already had them.

But can i make this with 2 viewcontrollers, so it can be separated, for better control? Maybe with instantiateViewControllerWithIdentifier? If you have code for that?

Ok, with 2 view controllers instead of two UIViews, it is actually much easier. All you need is 2 container views in the same place, and when you tap the segmented control, you hide one and show the other, and vice versa.

objective c - Ios swift container view and segmented control switch vi...

ios objective-c swift uisegmentedcontrol uicontainerview
Rectangle 27 0

Firstly, go into the container view's View Controller and make sure your two views are variables, either via Interface Builder or Code. Let's say you called them view1 and view2.

viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "segmentedControlTapped:", name: "SCTapped", object: nil)

Then, make a new function like this:

func segmentedControlTapped(notif: NSNotification){
    let index = notif.userInfo["index"] as Int

    if index == 0{
        view1.hidden = false
        view2.hidden = true
    }
    else if index == 1{
        view1.hidden = true
        view2.hidden = false
    }
}

Then, in the View Controller housing your Segmented Control, hook up an IBAction (if using IB) to the Control's ValueChanged action or use code.

@IBAction func tapped(sender: UISegmentedControl){
    NSNotificationCenter.defaultCenter().postNotificationName("SCTapped", object: nil, userInfo: ["index": sender.selectedSegmentIndex])
}

What this should do, is when the SC is tapped, it will call the tapped function, which tells the NSNotificationCenter to post a message. This should be received by the VC with the views in it and segmentedControlTapped() should be called, and it will switch your views.

adInfoViewController.hidden = false
does not have a member named 'hidden'
var adInfoViewController = AdInfoViewController()

Yes, you can't hide a UIViewController because it is not the same as a UIView. You need to make the two UIViews you want to hide first. Sorry, I assumed you already had them.

But can i make this with 2 viewcontrollers, so it can be separated, for better control? Maybe with instantiateViewControllerWithIdentifier? If you have code for that?

Ok, with 2 view controllers instead of two UIViews, it is actually much easier. All you need is 2 container views in the same place, and when you tap the segmented control, you hide one and show the other, and vice versa.

objective c - Ios swift container view and segmented control switch vi...

ios objective-c swift uisegmentedcontrol uicontainerview