【问题标题】:UISegmented Control Inner view should call API after view has been shownUISegmented Control 内部视图应在视图显示后调用 API
【发布时间】:2020-03-30 18:29:56
【问题描述】:

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

DispatchQueue.main.async {
   reloadCollectionView()
}

但这无济于事。 我想在显示子视图时调用 API(不在 SegmentControllForRides 加载上)。这也是必要的,因为第一个控制器 API 响应具有第二个控制器 API 调用的效果。所以请指导我在显示视图后如何调用 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)
        }
    }
}

【问题讨论】:

    标签: ios swift uitableview uicollectionview uisegmentedcontrol


    【解决方案1】:

    你需要在segmentControllChanged中配置你的api调用,有很多方法可以实现。最适合的一种方法是

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

    1 个例子

    代码 -

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

    【讨论】:

      猜你喜欢
      • 2013-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-05
      • 1970-01-01
      相关资源
      最近更新 更多