Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用SegmentController使TableView消失并显示UIContainerView_Ios_Swift_Xcode_Uisegmentedcontrol - Fatal编程技术网

Ios 使用SegmentController使TableView消失并显示UIContainerView

Ios 使用SegmentController使TableView消失并显示UIContainerView,ios,swift,xcode,uisegmentedcontrol,Ios,Swift,Xcode,Uisegmentedcontrol,我尝试使用段控制器在tableView和容器视图之间切换,但当我尝试在它们之间切换时,只起到一半的作用。TableView出现并消失,但容器视图从未出现 这是我的密码: @IBAction func switchAction(_ sender: UISegmentedControl) { if sender.selectedSegmentIndex == 0 { profileTableView.isHidden = false modelsContain

我尝试使用段控制器在tableView和容器视图之间切换,但当我尝试在它们之间切换时,只起到一半的作用。TableView出现并消失,但容器视图从未出现

这是我的密码:

@IBAction func switchAction(_ sender: UISegmentedControl) {
    if sender.selectedSegmentIndex == 0 {
        profileTableView.isHidden = false
        modelsContainerView.isHidden = true
    } else {
        profileTableView.isHidden = true
        modelsContainerView.isHidden = false
    }
}
更新

如果我使用这段代码,模拟类的工作。容器视图出现,但它不像tableview那样填充屏幕

    @IBAction func switchAction(_ sender: UISegmentedControl) {
    if sender.selectedSegmentIndex == 0 {
        UIView.animate(withDuration: 0.5, animations: {
            self.profileTableView.alpha = 1
            self.modelsContainerView.alpha = 0
        })
    } else {
        UIView.animate(withDuration: 0.5, animations: {
            self.profileTableView.alpha = 0
            self.modelsContainerView.alpha = 1
        })
    }
}

我可以看出它不起作用,因为我已将容器视图的背景色设置为粉红色。这就是我尝试从TableView(可行)切换到container视图时的样子:

所有插座似乎都已连接。我的UI设置是段控制器后面的绿色视图,下面有一个tableView,同一个位置有一个containerView

非常感谢您在advanced中的帮助

试试这个方法

Seg背景视图的高度为45分,固定的顶部、前导和尾随都等于
0

配置文件容器固定在前导、尾随和底部,全部等于
0
,顶部固定在Seg背景的底部

但您无法看到配置文件容器(红色背景),因为模型容器(橙色背景)位于其顶部,并且

“模型”容器的宽度和高度相等,水平和垂直居中,所有这些都与“轮廓”容器相同

配置文件容器中嵌入了配置文件表VC

模型容器中嵌入了模型VC

这个想法是:

选择
Seg 0
时,配置文件容器为alpha 1且未隐藏,而模型容器为alpha 0且隐藏

class SegContainerViewController: UIViewController {

    @IBOutlet weak var profileContainerView: UIView!
    @IBOutlet weak var modelsContainerView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // start with Profile visible
        // so hide Models and set its alphs to 0
        self.modelsContainerView.alpha = 0
        self.modelsContainerView.isHidden = true

    }

    @IBAction func switchAction(_ sender: UISegmentedControl) {

        // on segment select, the "other" container will be
        // transparent and hidden, so
        // un-hide it, then animate the alpha for both (for cross-fade)
        // on animation completion, hide the now transparent container

        if sender.selectedSegmentIndex == 0 {

            self.profileContainerView.isHidden = false
            UIView.animate(withDuration: 0.5, animations: {

                self.profileContainerView.alpha = 1
                self.modelsContainerView.alpha = 0

            }, completion: { (finished: Bool) in

                self.modelsContainerView.isHidden = true

            })

        } else {

            self.modelsContainerView.isHidden = false
            UIView.animate(withDuration: 0.5, animations: {

                self.profileContainerView.alpha = 0
                self.modelsContainerView.alpha = 1

            }, completion: { (finished: Bool) in

                self.profileContainerView.isHidden = true

            })

        }

    }
}
选择
seg1
时,配置文件容器为alpha 0且隐藏,而模型容器为alpha 1且未隐藏

class SegContainerViewController: UIViewController {

    @IBOutlet weak var profileContainerView: UIView!
    @IBOutlet weak var modelsContainerView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // start with Profile visible
        // so hide Models and set its alphs to 0
        self.modelsContainerView.alpha = 0
        self.modelsContainerView.isHidden = true

    }

    @IBAction func switchAction(_ sender: UISegmentedControl) {

        // on segment select, the "other" container will be
        // transparent and hidden, so
        // un-hide it, then animate the alpha for both (for cross-fade)
        // on animation completion, hide the now transparent container

        if sender.selectedSegmentIndex == 0 {

            self.profileContainerView.isHidden = false
            UIView.animate(withDuration: 0.5, animations: {

                self.profileContainerView.alpha = 1
                self.modelsContainerView.alpha = 0

            }, completion: { (finished: Bool) in

                self.modelsContainerView.isHidden = true

            })

        } else {

            self.modelsContainerView.isHidden = false
            UIView.animate(withDuration: 0.5, animations: {

                self.profileContainerView.alpha = 0
                self.modelsContainerView.alpha = 1

            }, completion: { (finished: Bool) in

                self.profileContainerView.isHidden = true

            })

        }

    }
}

编辑:

要访问嵌入式视图控制器,请覆盖prepareForSegue:

var theProfileVC: ProfileTableViewController?
var theModelsVC: ModelsViewControler?

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if let vc = segue.destination as? ProfileTableViewController {

        // do something here if desired, like setting a property of the VC

        // save a reference so we can use it later
        theProfileVC = vc
    }

    if let vc = segue.destination as? ModelsViewControler {

        // do something here if desired, like setting a property of the VC

        // save a reference so we can use it later
        theModelsVC = vc

    }

}
我还用一个例子更新了GitHub回购协议


我把它作为一个示例项目,如果你想深入研究的话:

试试这个方法

Seg背景视图的高度为45分,固定的顶部、前导和尾随都等于
0

配置文件容器固定在前导、尾随和底部,全部等于
0
,顶部固定在Seg背景的底部

但您无法看到配置文件容器(红色背景),因为模型容器(橙色背景)位于其顶部,并且

“模型”容器的宽度和高度相等,水平和垂直居中,所有这些都与“轮廓”容器相同

配置文件容器中嵌入了配置文件表VC

模型容器中嵌入了模型VC

这个想法是:

选择
Seg 0
时,配置文件容器为alpha 1且未隐藏,而模型容器为alpha 0且隐藏

class SegContainerViewController: UIViewController {

    @IBOutlet weak var profileContainerView: UIView!
    @IBOutlet weak var modelsContainerView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // start with Profile visible
        // so hide Models and set its alphs to 0
        self.modelsContainerView.alpha = 0
        self.modelsContainerView.isHidden = true

    }

    @IBAction func switchAction(_ sender: UISegmentedControl) {

        // on segment select, the "other" container will be
        // transparent and hidden, so
        // un-hide it, then animate the alpha for both (for cross-fade)
        // on animation completion, hide the now transparent container

        if sender.selectedSegmentIndex == 0 {

            self.profileContainerView.isHidden = false
            UIView.animate(withDuration: 0.5, animations: {

                self.profileContainerView.alpha = 1
                self.modelsContainerView.alpha = 0

            }, completion: { (finished: Bool) in

                self.modelsContainerView.isHidden = true

            })

        } else {

            self.modelsContainerView.isHidden = false
            UIView.animate(withDuration: 0.5, animations: {

                self.profileContainerView.alpha = 0
                self.modelsContainerView.alpha = 1

            }, completion: { (finished: Bool) in

                self.profileContainerView.isHidden = true

            })

        }

    }
}
选择
seg1
时,配置文件容器为alpha 0且隐藏,而模型容器为alpha 1且未隐藏

class SegContainerViewController: UIViewController {

    @IBOutlet weak var profileContainerView: UIView!
    @IBOutlet weak var modelsContainerView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // start with Profile visible
        // so hide Models and set its alphs to 0
        self.modelsContainerView.alpha = 0
        self.modelsContainerView.isHidden = true

    }

    @IBAction func switchAction(_ sender: UISegmentedControl) {

        // on segment select, the "other" container will be
        // transparent and hidden, so
        // un-hide it, then animate the alpha for both (for cross-fade)
        // on animation completion, hide the now transparent container

        if sender.selectedSegmentIndex == 0 {

            self.profileContainerView.isHidden = false
            UIView.animate(withDuration: 0.5, animations: {

                self.profileContainerView.alpha = 1
                self.modelsContainerView.alpha = 0

            }, completion: { (finished: Bool) in

                self.modelsContainerView.isHidden = true

            })

        } else {

            self.modelsContainerView.isHidden = false
            UIView.animate(withDuration: 0.5, animations: {

                self.profileContainerView.alpha = 0
                self.modelsContainerView.alpha = 1

            }, completion: { (finished: Bool) in

                self.profileContainerView.isHidden = true

            })

        }

    }
}

编辑:

要访问嵌入式视图控制器,请覆盖prepareForSegue:

var theProfileVC: ProfileTableViewController?
var theModelsVC: ModelsViewControler?

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if let vc = segue.destination as? ProfileTableViewController {

        // do something here if desired, like setting a property of the VC

        // save a reference so we can use it later
        theProfileVC = vc
    }

    if let vc = segue.destination as? ModelsViewControler {

        // do something here if desired, like setting a property of the VC

        // save a reference so we can use it later
        theModelsVC = vc

    }

}
我还用一个例子更新了GitHub回购协议




我把它作为一个示例项目,如果你想深入研究的话:

你能在故事板上做一个视图层次结构的屏幕截图吗?它已经上传了@Loryhuz哪个视图的背景是绿色的?你的邮件里不清楚,而且。。。您以这种方式使用容器视图有什么原因吗?通常,您会将分段控件放在视图的顶部,然后在其下方放一个ContainerView。。。您可以在容器视图中交换表视图和模型视图。没有真正的理由使用堆栈视图。。。将线段背景视图约束到顶部,将容器视图约束到线段背景的底部和视图的底部。将“包含的视图”约束到容器视图的4个侧面。您可以在故事板中制作视图层次结构的屏幕截图吗?它已上载@Loryhuz哪个视图的背景是绿色的?你的邮件里不清楚,而且。。。您以这种方式使用容器视图有什么原因吗?通常,您会将分段控件放在视图的顶部,然后在其下方放一个ContainerView。。。您可以在容器视图中交换表视图和模型视图。没有真正的理由使用堆栈视图。。。将线段背景视图约束到顶部,将容器视图约束到线段背景的底部和视图的底部。将“包含的视图”约束到容器视图的4个侧面。Hi@DonMag我正在尝试从ViewController访问一个属性,该属性包含支持其中一个容器视图的ViewController中的容器视图,但我很难做到这一点。你有什么建议吗?容器视图似乎在包含容器视图的ViewController加载之前加载,因此我可以访问该属性,但该属性尚未设置。hmmm。。。你有一个“MainVC”来保存你的开关和两个容器视图。。。您希望MainVC中的代码在容器视图中嵌入的一个(或两个)VC中设置属性?仅在容器视图中嵌入的一个VC中设置属性,但MainVC在容器视图中的VC之后加载,因此属性在MainVC中不可用,请为要“与之通信”的VC创建一个变量(我暂时假设它是ModelsVC)。当容器视图加载其嵌入式VC时,它会触发prepare for segue。在MainVC中,
覆盖func prepare(对于segue:UIStoryboardSegue,sender:Any?
。您可以检查它是否是ModelsVC……如果是,然后将其分配给