-1

I have segmented control, and container view, now how can i make 2 views and segmented control needs to switch that 2 views in container view?

I can't find any tutorial for swift or obj c.

3
  • How far did you try ? redartisan.com/2010/6/27/… stackoverflow.com/questions/1047114/… Commented Feb 19, 2015 at 10:43
  • I saw this, but i need with container view.. i think its better solution? Commented Feb 19, 2015 at 10:47
  • see my second link it's another solution.Or you can do anything you want to switch 2 view in segmentSwitch event Commented Feb 19, 2015 at 10:54

1 Answer 1

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.

In your viewDidLoad() write (swift):

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.

The IBAction func should look like this:

@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.

Sign up to request clarification or add additional context in comments.

6 Comments

Can't make that adInfoViewController.hidden = false to work does not have a member named 'hidden', and on top of file i put var adInfoViewController = AdInfoViewController() didn't do anything from storyboard. Do i need something else to do?
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.
This seems to be what you need: stackoverflow.com/questions/13279105/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.