ios Swift - 分段控制 - 切换多个视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27956353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:21:39  来源:igfitidea点击:

Swift - Segmented control - Switch multiple views

iosswiftxcode6

提问by Mohammad Nurdin

Until now I still can't figure how to switch multiple views in one view controller. My storyboard is like this one.

直到现在我仍然无法弄清楚如何在一个视图控制器中切换多个视图。我的故事板就是这样的。

enter image description here

在此处输入图片说明

Right now I want to embed two views inside my view controller.

现在我想在我的视图控制器中嵌入两个视图。

enter image description here

在此处输入图片说明

My code for segmented control to switch two views in one view controller so far.

到目前为止,我的分段控制代码可以在一个视图控制器中切换两个视图。

import UIKit

class PopularHistoryViewController: UIViewController {

    @IBOutlet weak var segmentedControl: UISegmentedControl!

    @IBAction func indexChanged(sender: UISegmentedControl) {
        switch segmentedControl.selectedSegmentIndex
        {
        case 0:
            NSLog("Popular selected")
            //show popular view
        case 1:
            NSLog("History selected")
            //show history view
        default:
            break; 
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }


}

Another thing, If I put two views inside my controller, what is best practice to differentiate it?

另一件事,如果我在控制器中放置两个视图,区分它的最佳实践是什么?

采纳答案by rakeshbs

You can use the isHiddenproperty of the UIViewto show/hide your required views. First you have to link both views to IBOutletsthrough the Interface builder

您可以使用 的isHidden属性UIView来显示/隐藏所需的视图。首先,您必须IBOutlets通过界面构建​​器将两个视图链接到

@IBOutlet weak var historyView: UIView!
@IBOutlet weak var popularView: UIView!

@IBAction func indexChanged(_ sender: UISegmentedControl) {
    switch segmentedControl.selectedSegmentIndex {
    case 0:
        historyView.isHidden = true
        popularView.isHidden = false
    case 1:
        historyView.isHidden = false
        popularView.isHidden = true
    default:
        break;
    }
}

Note: it was named hiddenin Swift 1 and 2.

注意:它是hidden在 Swift 1 和 2 中命名的。

回答by Xiaojun

If you want to do UI layout in Xcode for the two overlapping subviews, a better solution is to use two UIContainerViewController, and use the same way of setting the hidden property as suggested in the above answer.

如果要在 Xcode 中为两个重叠的子视图进行 UI 布局,更好的解决方案是使用两个 UIContainerViewController,并使用与上述答案中建议的相同的设置隐藏属性的方法。

enter image description here

在此处输入图片说明

回答by Midhun MP

First of all create two outlets and connect hose to the views in your ViewController.

首先创建两个出口并将软管连接到ViewController.

@IBOutlet weak var firstView: UIView!
@IBOutlet weak var secondView: UIView!

And Change the code like:

并更改代码,如:

@IBAction func indexChanged(sender: UISegmentedControl)
{
    switch segmentedControl.selectedSegmentIndex
    {
    case 0:
        firstView.hidden = false
        secondView.hidden = true
    case 1:
        firstView.hidden = true
        secondView.hidden = false
    default:
        break; 
    }
}

If you don't want to create Outlets, assign the views individual tags (Say 101and 102) and you can do it like:

如果您不想创建 Outlets,请为视图分配单独的标签(Say101102),您可以这样做:

@IBAction func indexChanged(sender: UISegmentedControl)
{
    switch segmentedControl.selectedSegmentIndex
    {
    case 0:
        self.view.viewWithTag(101)?.hidden = false
        self.view.viewWithTag(102)?.hidden = true
    case 1:
        self.view.viewWithTag(101)?.hidden = true
        self.view.viewWithTag(102)?.hidden = false
    default:
        break; 
    }
}

回答by Pete42

Add both views to the view controller in the story board and set one of them to be hidden = yes or alpha = 0. When your index changed function gets called set the current view on screen to hidden = yes/alpha of 0 and set the previously hidden view to hidden = no/alpha = 1. This should achieve what you want.

将两个视图添加到故事板中的视图控制器,并将其中一个设置为 hidden = yes 或 alpha = 0。当您的索引更改函数被调用时,将屏幕上的当前视图设置为 hidden = yes/alpha 0 并设置以前隐藏的视图隐藏 = no/alpha = 1。这应该实现你想要的。

回答by Sai kumar Reddy

@IBAction func acSegmentAction(_ sender: Any) {

    switch acSegmentedControl.selectedSegmentIndex {
        case 0:
           // print("addressview selected")
            addressView.isHidden = false
            contactProviderView.isHidden = true
        case 1:
            //print("contact provider selected")
            addressView.isHidden = true
            contactProviderView.isHidden = false
        default:
            break;
    }
}

回答by RealNmae

If it is a simple view, not a part of the screen, you can indeed use isHidden property of two subviews of your view controller view. But I don't like this approach because it's hard to understand latter what is going on with your nib when all of the subviews are in one pile. I would add and remove those two views as child view controllers programmatically. It's the cleanest way there is, in my opinion. But even if you decided to go with just views, don't put them directly on view controller's view. Use nibs, preferably with owner class. And in many cases consider adding and constraint them programmatically. It's more code, but also cleaner and conserves resources.

如果它是一个简单的视图,而不是屏幕的一部分,您确实可以使用视图控制器视图的两个子视图的 isHidden 属性。但是我不喜欢这种方法,因为当所有子视图都在一堆时,很难理解后者的笔尖发生了什么。我会以编程方式添加和删除这两个视图作为子视图控制器。在我看来,这是最干净的方式。但即使您决定只使用视图,也不要将它们直接放在视图控制器的视图上。使用笔尖,最好使用所有者类。在许多情况下,考虑以编程方式添加和约束它们。这是更多的代码,但也更清洁和节省资源。

回答by garytechy

So what is written above does not work for me, so I figured out myself in Xcode 11 with Swift 5. (view1 = historyView, view2 = popularView)

所以上面写的东西对我不起作用,所以我在Xcode 11 和 Swift 5 中找到了自己。(视图 1 = 历史视图,视图 2 = 流行视图)

@IBOutlet weak var view1: UITableView!
@IBOutlet weak var view2: UITableView!

@IBAction func segmentedControlChanged(_ sender: Any) {

    func showView1() {
        view1.isHidden = false
        view2.isHidden = true
    }

    func showView2() {
        view1.isHidden = true
        view2.isHidden = false
    }

    guard let segmentedControl = sender as?
        UISegmentedControl else { return }
    if segmentedControl.selectedSegmentIndex == 0 {
        showView1()

    }
        else {
            showView2()
        }
    }

Maybe this helps anyone.

也许这对任何人都有帮助。