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

Mirza Delic
  • 4,119
  • 12
  • 55
  • 86
  • How far did you try ? http://redartisan.com/2010/6/27/uisegmented-control-view-switching-revisited http://stackoverflow.com/questions/1047114/how-do-i-use-a-uisegmentedcontrol-to-switch-views – Huy Nghia Feb 19 '15 at 10:43
  • I saw this, but i need with container view.. i think its better solution? – Mirza Delic Feb 19 '15 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 – Huy Nghia Feb 19 '15 at 10:54

1 Answers1

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.

nanothread
  • 908
  • 3
  • 10
  • 25
  • 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? – Mirza Delic Feb 19 '15 at 11:22
  • 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. – nanothread Feb 19 '15 at 11:49
  • 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? – Mirza Delic Feb 19 '15 at 11:52
  • 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. – nanothread Feb 19 '15 at 12:42
  • Thanks.. this works :D just one more question, how can i pass data to containers controller? I have variable `id` in contianer controller view, so how to set that variable from parent? Then i will check if didSet.. – Mirza Delic Feb 19 '15 at 12:51
  • 1
    This seems to be what you need: http://stackoverflow.com/questions/13279105/access-container-view-controller-from-parent-ios – nanothread Feb 19 '15 at 12:53