集合 View/Tab 条形控制器崩溃
Collection View/Tab Bar Controller Crashes
在我的应用程序中,我使用标签栏控制器在视图控制器之间切换,并且在每个视图控制器上我使用集合视图然后转到不同的页面。当我 运行 电脑上的应用程序时,一切正常。问题是当我 运行 我的 phone 上的应用程序时。每当我单击选项卡栏控制器中的选项卡,该选项卡转到上面带有集合视图控制器的页面时,应用程序就会冻结然后崩溃。我认为我已经将问题缩小到集合视图控制器,因为当没有集合视图控制器时,我 phone 上的应用程序不会崩溃。它仅在使用集合视图控制器时崩溃。这可能是什么原因造成的?我已经看过了,但没有发现任何类似的问题。
这是其中一个集合视图控制器的代码:
import UIKit
class HighSchoolController: UIViewController, \UICollectionViewDataSource, UICollectionViewDelegate {
var identities = [String]()
let hsImage: [UIImage] = [
UIImage(named: "dates")!,
UIImage(named: "athletics_icon")!,
UIImage(named: "interm-1")!,
UIImage(named: "lunch_icon")!,
UIImage(named: "pioneerpress")!,
UIImage(named: "PIONEERNEWS")!,
UIImage(named: "clubs_icon")!,
UIImage(named: "contact_icon")!
]
override func viewDidLoad() {
super.viewDidLoad()
identities = ["events", "athletics", "da", "lunch", "pioneer press", "pioneer news", "clubs", "contact"]
//Navigationbar Shadow
self.navigationController?.navigationBar.layer.shadowColor = UIColor.black.cgColor
self.navigationController?.navigationBar.layer.shadowOffset = CGSize(width: 0.0, height: 4.0)
self.navigationController?.navigationBar.layer.shadowRadius = 9.0
self.navigationController?.navigationBar.layer.shadowOpacity = 0.9
self.navigationController?.navigationBar.layer.masksToBounds = false
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return hsImage.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let hsCell = collectionView.dequeueReusableCell(withReuseIdentifier: "hsCell", for: indexPath) as! HighSchoolCell
hsCell.hsImage.image = hsImage[indexPath.item]
hsCell.layer.cornerRadius = (hsCell.layer.frame.height) / 2.0;
hsCell.layer.masksToBounds = true
return hsCell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vcName = identities[indexPath.row]
let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
self.navigationController?.pushViewController(viewController!, animated: true)
}
}
在我看来,它一定是在您的 self.navigationController?...
线路上崩溃了。我的猜测是您将 CollectionViewController
用作 TabBarController
中选项卡的 rootViewController
。
如果您想使用 navigationController
在标签内导航,您需要附加 navigationController
作为标签的根,然后将 CollectionViewController
设置为rootViewController
的 navigationController
.
您需要像 TabBarController > NavigationViewController > CollectionViewController
这样的层次结构,而不是 TabBarController > CollectionViewController
我通过在我的标签栏控制器中这样做来解决这个问题
让 layout1 = UICollectionViewFlowLayout() 让 cv1 =
CollectionView1(collectionViewLayout: layout1)
让layout2 = UICollectionViewFlowLayout()
让 cv2 = CollectionView1(collectionViewLayout: layout2)
让layout3 = UICollectionViewFlowLayout()
让 cv3 = CollectionView1(collectionViewLayout: layout3)
让layout4 = UICollectionViewFlowLayout()
让 cv4 = CollectionView1(collectionViewLayout: layout4)
在我的应用程序中,我使用标签栏控制器在视图控制器之间切换,并且在每个视图控制器上我使用集合视图然后转到不同的页面。当我 运行 电脑上的应用程序时,一切正常。问题是当我 运行 我的 phone 上的应用程序时。每当我单击选项卡栏控制器中的选项卡,该选项卡转到上面带有集合视图控制器的页面时,应用程序就会冻结然后崩溃。我认为我已经将问题缩小到集合视图控制器,因为当没有集合视图控制器时,我 phone 上的应用程序不会崩溃。它仅在使用集合视图控制器时崩溃。这可能是什么原因造成的?我已经看过了,但没有发现任何类似的问题。
这是其中一个集合视图控制器的代码:
import UIKit
class HighSchoolController: UIViewController, \UICollectionViewDataSource, UICollectionViewDelegate {
var identities = [String]()
let hsImage: [UIImage] = [
UIImage(named: "dates")!,
UIImage(named: "athletics_icon")!,
UIImage(named: "interm-1")!,
UIImage(named: "lunch_icon")!,
UIImage(named: "pioneerpress")!,
UIImage(named: "PIONEERNEWS")!,
UIImage(named: "clubs_icon")!,
UIImage(named: "contact_icon")!
]
override func viewDidLoad() {
super.viewDidLoad()
identities = ["events", "athletics", "da", "lunch", "pioneer press", "pioneer news", "clubs", "contact"]
//Navigationbar Shadow
self.navigationController?.navigationBar.layer.shadowColor = UIColor.black.cgColor
self.navigationController?.navigationBar.layer.shadowOffset = CGSize(width: 0.0, height: 4.0)
self.navigationController?.navigationBar.layer.shadowRadius = 9.0
self.navigationController?.navigationBar.layer.shadowOpacity = 0.9
self.navigationController?.navigationBar.layer.masksToBounds = false
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return hsImage.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let hsCell = collectionView.dequeueReusableCell(withReuseIdentifier: "hsCell", for: indexPath) as! HighSchoolCell
hsCell.hsImage.image = hsImage[indexPath.item]
hsCell.layer.cornerRadius = (hsCell.layer.frame.height) / 2.0;
hsCell.layer.masksToBounds = true
return hsCell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vcName = identities[indexPath.row]
let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
self.navigationController?.pushViewController(viewController!, animated: true)
}
}
在我看来,它一定是在您的 self.navigationController?...
线路上崩溃了。我的猜测是您将 CollectionViewController
用作 TabBarController
中选项卡的 rootViewController
。
如果您想使用 navigationController
在标签内导航,您需要附加 navigationController
作为标签的根,然后将 CollectionViewController
设置为rootViewController
的 navigationController
.
您需要像 TabBarController > NavigationViewController > CollectionViewController
TabBarController > CollectionViewController
我通过在我的标签栏控制器中这样做来解决这个问题
让 layout1 = UICollectionViewFlowLayout() 让 cv1 = CollectionView1(collectionViewLayout: layout1)
让layout2 = UICollectionViewFlowLayout() 让 cv2 = CollectionView1(collectionViewLayout: layout2)
让layout3 = UICollectionViewFlowLayout() 让 cv3 = CollectionView1(collectionViewLayout: layout3)
让layout4 = UICollectionViewFlowLayout() 让 cv4 = CollectionView1(collectionViewLayout: layout4)