ios - UISegmentedControl 内部 View 应在显示 View 后调用 API

标签 ios swift uitableview uicollectionview uisegmentedcontrol

我有一个“UISegmentedControl”,它以编程方式添加到名为 SegmentControllForRides 的 View Controller 中。 (我以编程方式添加了 UISegmentedControl,因为段项目将在某个时间 2 或 3 更改。UISegmentedControl 有三个项目。项目名称为 ["Request","Upcoming", "Past"]。每个段将显示一个 viewController.view 并隐藏其他的。每个 View Controller 都有一个 API 调用。我的问题是当 SegmentControllForRides 第一次显示它调用所有三个(子)viewcontroller API 时(我认为因为 View 是隐藏的但加载在 Controller 中,这就是 API 调用转到服务器的原因。每个 View Controller 都有一个 UITableView 并且在 UITableViewCell 中有一个 UICollectionView。我试图在显示 View 时(再次)调用 API,在这种情况下,API 调用进行了,但 UICollectionView 弄乱了。它显示了另一个单元格的数据。为此,我尝试

DispatchQueue.main.async {
   reloadCollectionView()
}

但这并没有帮助。 我想在显示 subview 时调用 API(不是在 SegmentControllForRides 加载时)。这也是必要的,因为第一个 Controller API 响应具有第二个 Controller API 调用的效果。所以请指导我如何在显示 View 后调用 API。 我会尝试提供任何进一步的细节。 这是我的课。

import UIKit

class SegmentControllForRides: UIViewController {
    // MARK: - Variables
    var controller: UIViewController!
    var requestedRideViewController: RequestedRideViewController!
    var upcomingRideViewController: UpcomingRideViewController!
    var myRideViewControllerUpcoming: UIViewController!
    var myRideViewControllerPast: UIViewController!
    var segmentControll: UISegmentedControl!
    var sideMenuOpen = false

    // MARK: - Outlets

    // MARK: - Life Cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        configure()
    }
    override func viewWillDisappear(_ animated: Bool) {
        if sideMenuOpen {
            openCloseSideMenu()
        }
    }

    // MARK: - Actions & Events
    @IBAction func segmentControllChanged(_ sender: UISegmentedControl) {
        if UserDefaults.standard.bool(forKey: User.isAppUsingAsPassenger) {
            switch segmentControll.selectedSegmentIndex {
            case 0:
                print("segment 1")
                myRideViewControllerUpcoming.view.isHidden = false
                myRideViewControllerUpcoming.didMove(toParent: self)
                myRideViewControllerPast.view.isHidden = true
            case 1:
                print("segment 2")
                myRideViewControllerUpcoming.view.isHidden = true
                myRideViewControllerPast.view.isHidden = false
                myRideViewControllerPast.didMove(toParent: self)
            default:
                break
            }
        } else {
            switch segmentControll.selectedSegmentIndex {
            case 0:
                print("segment 1")
                requestedRideViewController.view.isHidden = false
                requestedRideViewController.didMove(toParent: self)
                upcomingRideViewController.view.isHidden = true
                myRideViewControllerPast.view.isHidden = true
            case 1:
                print("segment 2")
                requestedRideViewController.view.isHidden = true
                upcomingRideViewController.view.isHidden = false
//                upcomingRideViewController.getUpcomingRide()
                upcomingRideViewController.didMove(toParent: self)
                myRideViewControllerPast.view.isHidden = true
//                let myClass : UpcomingRideViewController = self.children[1] as! UpcomingRideViewController
//                myClass.myRideTableView.reloadData()
//                myClass.getUpcomingRide()
//                myClass.viewWillAppear(false)
            case 2:
                print("Segment 3")
                requestedRideViewController.view.isHidden = true
                upcomingRideViewController.view.isHidden = true
                myRideViewControllerPast.view.isHidden = false
                myRideViewControllerPast.didMove(toParent: self)
            default:
                break
            }
        }
    }

    @IBAction func sideMenuClicked(_ sender: UIBarButtonItem) {
        openCloseSideMenu()
    }
    @objc func handleGesture(gesture: UISwipeGestureRecognizer) -> Void {
        if gesture.direction == .right {
            print("Swipe Right")
            if !sideMenuOpen {
                openCloseSideMenu()
            }
        } else if gesture.direction == .left {
            print("Swipe Left")
            if sideMenuOpen {
                openCloseSideMenu()
            }
        }
    }
    // MARK: - Helper Methods
    private func configure() {
        let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
        swipeLeft.direction = .left
        self.view.addGestureRecognizer(swipeLeft)

        let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
        swipeRight.direction = .right
        self.view.addGestureRecognizer(swipeRight)
        controller = storyboard!.instantiateViewController(withIdentifier: "SideMenuViewController")

        if UserDefaults.standard.bool(forKey: User.isAppUsingAsPassenger) {
            let segmentItems = ["Upcoming", "Past"]
            segmentControll = UISegmentedControl(items: segmentItems)
            segmentControll.frame = CGRect(x:0 ,y: 0, width: view.frame.width, height: 30)
            segmentControll.addTarget(self, action: #selector(segmentControllChanged), for: .valueChanged)
            segmentControll.selectedSegmentIndex = 0
            segmentControll.backgroundColor = .rideelyGray
            if #available(iOS 13.0, *) {
                segmentControll.selectedSegmentTintColor = .rideelyYellow
            } else {
                // Fallback on earlier versions
            }
            view.addSubview(segmentControll)
            myRideViewControllerUpcoming  = storyboard!.instantiateViewController(withIdentifier: "MyRideViewController")
            addChild(myRideViewControllerUpcoming)
            myRideViewControllerUpcoming.view.frame = CGRect(x: 0, y: 30, width: view.frame.width, height: view.frame.height - 30)  // or, better, turn off `translatesAutoresizingMaskIntoConstraints` and then define constraints for this subview
            view.addSubview(myRideViewControllerUpcoming.view)
            myRideViewControllerUpcoming.didMove(toParent: self)
            myRideViewControllerUpcoming.view.frame = CGRect(x: 0 - self.view.frame.width, y: 30, width: view.frame.width, height: view.frame.height - 30)
            UIView.animate(withDuration: 0.3, animations: { () -> Void in
                self.myRideViewControllerUpcoming.view.frame = CGRect(x: 0, y: 30, width: self.view.frame.width, height: self.view.frame.height - 30)
            }, completion:nil)
            myRideViewControllerUpcoming.view.isHidden = false

            myRideViewControllerPast  = storyboard!.instantiateViewController(withIdentifier: "MyRideViewController")
            addChild(myRideViewControllerPast)
            myRideViewControllerPast.view.frame = CGRect(x: 0, y: 30, width: view.frame.width, height: view.frame.height - 30)  // or, better, turn off `translatesAutoresizingMaskIntoConstraints` and then define constraints for this subview
            view.addSubview(myRideViewControllerPast.view)
            myRideViewControllerPast.didMove(toParent: self)
            myRideViewControllerPast.view.frame = CGRect(x: 0 - self.view.frame.width, y: 30, width: view.frame.width, height: view.frame.height - 30)
            UIView.animate(withDuration: 0.3, animations: { () -> Void in
                self.myRideViewControllerPast.view.frame = CGRect(x: 0, y: 30, width: self.view.frame.width, height: self.view.frame.height - 30)
            }, completion:nil)
            myRideViewControllerPast.view.isHidden = true
        } else {
            let segmentItems = ["Request","Upcoming", "Past"]
            segmentControll = UISegmentedControl(items: segmentItems)
            segmentControll.frame = CGRect(x:0 ,y: 0, width: view.frame.width, height: 30)
            segmentControll.addTarget(self, action: #selector(segmentControllChanged), for: .valueChanged)
            segmentControll.selectedSegmentIndex = 0
            segmentControll.backgroundColor = .rideelyGray
            if #available(iOS 13.0, *) {
                segmentControll.selectedSegmentTintColor = .rideelyYellow
            } else {
                // Fallback on earlier versions
            }
            view.addSubview(segmentControll)
            requestedRideViewController = (storyboard!.instantiateViewController(withIdentifier: "RequestedRideViewController") as! RequestedRideViewController)
            addChild(requestedRideViewController)
            requestedRideViewController.view.frame = CGRect(x: 0, y: 30, width: view.frame.width, height: view.frame.height)  // or, better, turn off `translatesAutoresizingMaskIntoConstraints` and then define constraints for this subview
            view.addSubview(requestedRideViewController.view)
            requestedRideViewController.didMove(toParent: self)
            requestedRideViewController.view.frame = CGRect(x: 0 - self.view.frame.width, y: 30, width: view.frame.width, height: view.frame.height)
            UIView.animate(withDuration: 0.3, animations: { () -> Void in
                self.requestedRideViewController.view.frame = CGRect(x: 0, y: 30, width: self.view.frame.width, height: self.view.frame.height - 30)
            }, completion:nil)
            requestedRideViewController.view.isHidden = false

            upcomingRideViewController  = (storyboard!.instantiateViewController(withIdentifier: "UpcomingRideViewController") as! UpcomingRideViewController)
            addChild(upcomingRideViewController)
            upcomingRideViewController.view.frame = CGRect(x: 0, y: 30, width: view.frame.width, height: view.frame.height - 30)  // or, better, turn off `translatesAutoresizingMaskIntoConstraints` and then define constraints for this subview
            view.addSubview(upcomingRideViewController.view)
            upcomingRideViewController.didMove(toParent: self)
            upcomingRideViewController.view.frame = CGRect(x: 0 - self.view.frame.width, y: 30, width: view.frame.width, height: view.frame.height)
            UIView.animate(withDuration: 0.3, animations: { () -> Void in
                self.upcomingRideViewController.view.frame = CGRect(x: 0, y: 30, width: self.view.frame.width, height: self.view.frame.height - 30)
            }, completion:nil)
            upcomingRideViewController.view.isHidden = true

            myRideViewControllerPast  = storyboard!.instantiateViewController(withIdentifier: "MyRideViewController")
            addChild(myRideViewControllerPast)
            myRideViewControllerPast.view.frame = CGRect(x: 0, y: 30, width: view.frame.width, height: view.frame.height - 30)  // or, better, turn off `translatesAutoresizingMaskIntoConstraints` and then define constraints for this subview
            view.addSubview(myRideViewControllerPast.view)
            myRideViewControllerPast.didMove(toParent: self)
            myRideViewControllerPast.view.frame = CGRect(x: 0 - self.view.frame.width, y: 30, width: view.frame.width, height: view.frame.height - 30)
            UIView.animate(withDuration: 0.3, animations: { () -> Void in
                self.myRideViewControllerPast.view.frame = CGRect(x: 0, y: 30, width: self.view.frame.width, height: self.view.frame.height - 30)
            }, completion:nil)
            myRideViewControllerPast.view.isHidden = true
        }
    }
    private func openCloseSideMenu() {
        if sideMenuOpen {
            sideMenuOpen = false
            controller.removeFromParent()
            controller.view.removeFromSuperview()
        } else {
            sideMenuOpen = true
            addChild(controller)
            controller.view.frame = CGRect(x: 0, y: 0, width: view.frame.width * 0.8, height: view.frame.height)  // or, better, turn off `translatesAutoresizingMaskIntoConstraints` and then define constraints for this subview
            view.addSubview(controller.view)
            controller.didMove(toParent: self)
            controller.view.frame = CGRect(x: 0 - self.view.frame.width, y: 0, width: view.frame.width * 0.8, height: view.frame.height)
            UIView.animate(withDuration: 0.3, animations: { () -> Void in
                self.controller.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width * 0.8, height: self.view.frame.height)
            }, completion:nil)
        }
    }
}

最佳答案

您需要在 segmentControllChanged 中配置您的 api 调用,您可以通过多种方式实现这一点。最适合的一种方式是

您可以为每个 Controller 使用标志来检查段何时滚动,然后只有您明确点击 api,否则不要点击它。

1 个示例

代码-

switch segmentControll.selectedSegmentIndex {
            case 1:
                print("segment 1")
                myRideViewControllerUpcoming.isVisible = true
                // call your myRideViewControllerUpcoming api method here 
            }

关于ios - UISegmentedControl 内部 View 应在显示 View 后调用 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59201684/

相关文章:

ios - cocos2d。对象类似于ccsprite但没有图片吗?

ios - Square应用是否具有自定义URL方案?

java - 使用约束布局针对不同的屏幕尺寸进行设计

android - 不同手机平台的插件不一样,那么phone gap怎么跨平台呢?

ios - 符合 MKAnnotation 协议(protocol)的类的自定义方法

ios - 在 iOS 8 自动高度的单元格内键入时,UITableView 在 endUpdates 上跳转到顶部

SwiftUI : Using NavigationView leads to image and text gone

ios - 来自 WKWebview 的 iCLoud 文档选择器关闭容器 View

ios - UITableView 滚动弹回顶部

ios - 使用 UIAppearance 设置 UITableView 的背景颜色