使用 UITableView 时出现不需要的 segue
Unwanted segue occuring while using a UITableView
我有一个 UItableView
并且我对 UIView
执行了一个 segue。根据在 UITableView
中选择的行,显示的 UIView
中的信息会有所不同。 UIView
完美呈现。
问题是,当出现新的 UIView
时,如果我在屏幕上初始行所在的相同位置单击,然后我会返回到 UITableView
。
我在名为 dayx
的故事板上做了一个 segue。
然后我使用以下代码来呈现新的 UIView
。
这是我用来执行 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)
}
呈现的UIView
代码如下:
class 食物信息: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)
}
}
//---------------------------------------
}
我不知道为什么它会回到 UITableView
。任何人都知道我做错了什么?
您提到您正在使用 Storyboard,并且您已将两个视图 UITableView
和目标 UIView
与 identifier
"dayx" 的 segue 连接起来。
您是否有某些特定原因要覆盖 tableView(didSelectRowAt:)
?
您是否考虑过或尝试使用与此类似的代码(并删除 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
}
}
}
同时设置以下默认值 属性(在 viewWillAppear(animated)
中)以适合您的 UI...
clearsSelectionOnViewWillAppear = true
(或假)。
我有一个 UItableView
并且我对 UIView
执行了一个 segue。根据在 UITableView
中选择的行,显示的 UIView
中的信息会有所不同。 UIView
完美呈现。
问题是,当出现新的 UIView
时,如果我在屏幕上初始行所在的相同位置单击,然后我会返回到 UITableView
。
我在名为 dayx
的故事板上做了一个 segue。
然后我使用以下代码来呈现新的 UIView
。
这是我用来执行 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)
}
呈现的UIView
代码如下:
class 食物信息: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)
}
}
//---------------------------------------
}
我不知道为什么它会回到 UITableView
。任何人都知道我做错了什么?
您提到您正在使用 Storyboard,并且您已将两个视图 UITableView
和目标 UIView
与 identifier
"dayx" 的 segue 连接起来。
您是否有某些特定原因要覆盖 tableView(didSelectRowAt:)
?
您是否考虑过或尝试使用与此类似的代码(并删除 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
}
}
}
同时设置以下默认值 属性(在 viewWillAppear(animated)
中)以适合您的 UI...
clearsSelectionOnViewWillAppear = true
(或假)。