Search code examples
uitableviewsegueuistoryboardsegue

Unwanted segue occuring while using a UITableView


I have a UItableView and I perform a segue to a UIView. The information varies on the presented UIView depending on what row is selected in the UITableView. The UIView gets presented perfectly. The problem is that when the new UIView is presented, if I click in the same place on the screen where the initial row was, I then segue back to the UITableView.

I made a segue on the storyboard called dayx. I then used the following code to present the new UIView. Here is the code I used to perform the segue:

  //when row is pressed what happens
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let destinationVC = segue.destination as! foodinfo
    destinationVC.counter = rowselected
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    rowselected = indexPath.row
    self.performSegue(withIdentifier: "dayx", sender: self)

    //making desleected row not highlighted
    tableView.deselectRow(at: indexPath, animated: true)
}

the presented UIView code is as follows:

class foodinfo: UIViewController {

var counter = Int()
let playerController = AVPlayerViewController()

@IBOutlet var tabs: UISegmentedControl!
@IBOutlet var theTitleLable: UILabel!
@IBOutlet var stack: UIStackView!
@IBOutlet var ScrollView: UIScrollView!
@IBOutlet var foodImageview: UIImageView!
@IBOutlet var navBar: UINavigationBar!


var simpleViewX: UIView!
var simpleViewY: UIView!

var theTitleArray = ["title1","title2","title3","title4","title5","title6","title7"]
var theImageArray = ["image1", "image2", "image3","image4","image5",image6","image7"]


//video links
var calledVideo: String?

let vids = ["https://vimeo.com/123456789","https://vimeo.com/123456789", "https://vimeo.com/123456789", "https://vimeo.com/123456789","https://vimeo.com/123456789","https://vimeo.com/123456789","https://vimeo.com/123456789"]


override func viewDidLoad() {

    NotificationCenter.default.addObserver(self, selector: #selector(self.deviceOrientationDidChange), name: UIDevice.orientationDidChangeNotification, object: nil)
    getTitle()
    getImage()
    getNavbar()

    //Different subViews for ingredients and steps
    if counter == 0 {

        simpleViewX = SimpleVC0().view
        simpleViewY = SimpleVC1().view

        stack.addArrangedSubview(simpleViewX)
        stack.addArrangedSubview(simpleViewY)

        simpleViewY.isHidden = true

    }

    if counter == 1 {
        simpleViewX = SimpleVC2().view
        simpleViewY = SimpleVC3().view

        stack.addArrangedSubview(simpleViewX)
        stack.addArrangedSubview(simpleViewY)

        simpleViewY.isHidden = true

    }

    if counter == 2 {
        simpleViewX = SimpleVC4().view
        simpleViewY = SimpleVC5().view

        stack.addArrangedSubview(simpleViewX)
        stack.addArrangedSubview(simpleViewY)

        simpleViewY.isHidden = true
    }

    if counter == 3 {
        simpleViewX = SimpleVC6().view
        simpleViewY = SimpleVC7().view

        stack.addArrangedSubview(simpleViewX)
        stack.addArrangedSubview(simpleViewY)

        simpleViewY.isHidden = true
    }

    if counter == 4 {
        simpleViewX = SimpleVC8().view
        simpleViewY = SimpleVC9().view

        stack.addArrangedSubview(simpleViewX)
        stack.addArrangedSubview(simpleViewY)

        simpleViewY.isHidden = true
    }

    if counter == 5 {
        simpleViewX = SimpleVC10().view
        simpleViewY = SimpleVC11().view

        stack.addArrangedSubview(simpleViewX)
        stack.addArrangedSubview(simpleViewY)

        simpleViewY.isHidden = true
    }

    if counter == 6 {
        simpleViewX = SimpleVC12().view
        simpleViewY = SimpleVC13().view

        stack.addArrangedSubview(simpleViewX)
        stack.addArrangedSubview(simpleViewY)

        simpleViewY.isHidden = true
    }

}


func getTitle() {
    theTitleLable.text = theTitleArray[counter]
}

func getImage() {
    foodImageview.image = UIImage(named: theImageArray[counter] + ".jpg")
}

func getNavbar () {
    self.navBar.setBackgroundImage(UIImage(), for: .default)
    self.navBar.shadowImage = UIImage()
}



@IBAction func tabselected(_ sender: Any) {
    switch (sender as AnyObject).selectedSegmentIndex {
    case 0:
        //shiftView.bringSubviewToFront(simpleViewX)
        simpleViewY.isHidden = true
        simpleViewX.isHidden = false

        break
    case 1:
        //shiftView.bringSubviewToFront(simpleViewY)

        simpleViewX.isHidden = true
        simpleViewY.isHidden = false

        break
    case 2:
        //calledvideo in array is the value of the counter.
        calledVideo = vids[counter]
        geturl()


        break
    default:
        break
    }
}

//Getting url info for video by using exractor
func geturl() {
    if let url = URL(string: calledVideo!) {
        HCVimeoVideoExtractor.fetchVideoURLFrom(url: url, completion: { ( video:HCVimeoVideo?, error:Error?) -> Void in

            if let err = error {
                DispatchQueue.main.async() {
                    let alert = UIAlertController(title: "Error", message: err.localizedDescription, preferredStyle: .alert)
                    alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
                    self.present(alert, animated: true, completion: nil)
                }
                return
            }

            guard let vid = video else {
                print("Invalid video object")
                return
            }


            //play video
            let player = AVPlayer(url: vid.videoURL[.Quality540p]!)
            self.playerController.player = player
            self.present(self.playerController, animated: true) {
                player.play()
            }
        })

    }
}

//ROTATION OF VIDEO BUT NOT ALLOWING VC TO ROTATE---this is the guy you got-----
@objc func deviceOrientationDidChange() {
    switch UIDevice.current.orientation {
    case .faceUp, .faceDown, .portrait, .unknown, .portraitUpsideDown:
        // default the player to original rotation
        self.playerController.view.transform = .identity
        self.playerController.view.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)

    /*case .landscapeLeft:

       print("Landscape left")
       self.playerController.view.transform = CGAffineTransform(rotationAngle: CGFloat((90 * Double.pi)/180))
        self.playerController.view.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)*/

    case .landscapeRight, .landscapeLeft:

                  print("Landscape right")
                  self.playerController.view.transform = CGAffineTransform(rotationAngle: CGFloat((-90 * Double.pi)/180))
        self.playerController.view.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
    }
}
//---------------------------------------

}

I have no idea why it segues back to the UITableView. Anyone know what im doing wrong?


Solution

  • You mention that you're using a Storyboard and that you've connected your two views UITableView and destination UIView with a segue with identifier "dayx".

    Is there some specific reason you're overriding tableView(didSelectRowAt:)?

    Have you considered or attempted to use code similar to this (and removing tableView(didSelectRowAt:))...?

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    
        if segue.identifier = "dayx" {
    
            if let rowSelected = tableView.indexPathForSelectedRow {
    
                let destinationVC = segue.destination as! foodinfo
                destinationVC.counter = rowSelected
            }
        }
    }
    

    Also set the following default property (in viewWillAppear(animated))to suit your UI... clearsSelectionOnViewWillAppear = true (or false).