开发者社区> 问答> 正文

使用制表符栏从另一个视图返回时不会调用视图控制器

我有一个简单的应用程序two controllers,两者之间的导航通过UITabBarController执行。当应用程序启动时,当它第一次被初始化时,一切都按其应有的方式工作,但是当我在第一个VC中更改一些东西并切换到第二个VC时,然后再次尝试返回到第一个VC--它不会出现。

ViewWillAppear()加法在我的例子中不起作用,因为调试器甚至没有显示ViewDidLoad()或ViewWillAppear()功能在第一个VC中工作。

这就是它的外观(CloudApp记录)

MainTabBarController代码:

class MainTabBarController: UITabBarController {

var photosVC: PhotosCollectionViewController! var likesVC: LikesCollectionViewController!

override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white }

override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated)

photosVC = PhotosCollectionViewController(collectionViewLayout: WaterfallLayout())
likesVC = LikesCollectionViewController(collectionViewLayout: UICollectionViewFlowLayout())

viewControllers = [
  generateNavigationController(rootViewController: photosVC, title: "Photos", image: #imageLiteral(resourceName: "photos")),
  generateNavigationController(rootViewController: likesVC, title: "Favourites", image: #imageLiteral(resourceName: "heart"))
]

}

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) { if (self.selectedViewController?.isKind(of: UINavigationController.self))! { let nav = self.selectedViewController as! UINavigationController nav.popToRootViewController(animated: false) } }

private func generateNavigationController(rootViewController: UIViewController, title: String, image: UIImage) -> UIViewController { let navigationVC = UINavigationController(rootViewController: rootViewController) navigationVC.tabBarItem.title = title navigationVC.tabBarItem.image = image return navigationVC } } 光电收集器代码:

import UIKit

class PhotosCollectionViewController: UICollectionViewController {

var networkDataFetcher = NetworkDataFetcher() private var timer: Timer?

private var photos = UnsplashPhoto

private var selectedImages = UIImage

// private let itemsPerRow: CGFloat = 2 // относится к UICollectionViewDelegateFlowLayout // private let sectionInserts = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20) // относится к UICollectionViewDelegateFlowLayout

private lazy var addBarButtonItem: UIBarButtonItem = { return UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addBarButtonTapped)) }()

private lazy var actionBarButtonItem: UIBarButtonItem = { return UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(actionBarButtonTapped)) }()

private var numberOfSelectedPhotos: Int { return collectionView.indexPathsForSelectedItems?.count ?? 0 }

private let enterSearchTermLabel: UILabel = { let label = UILabel() label.text = "Please enter search term above..."

label.textAlignment = .center
label.font = UIFont.boldSystemFont(ofSize: 20)
label.translatesAutoresizingMaskIntoConstraints = false
return label

}()

private let spinner: UIActivityIndicatorView = { let spinner = UIActivityIndicatorView(style: .gray) spinner.translatesAutoresizingMaskIntoConstraints = false return spinner }()

override func viewDidLoad() { super.viewDidLoad() }

override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated)

setupNavigationBar()
setupCollectionView()
setupSearchBar()
setupEnterLabel()
setupSpinner()

}

private func undateNavButtonsState() { addBarButtonItem.isEnabled = numberOfSelectedPhotos > 0 actionBarButtonItem.isEnabled = numberOfSelectedPhotos > 0 }

func refresh() { self.selectedImages.removeAll() self.collectionView.selectItem(at: nil, animated: true, scrollPosition: []) undateNavButtonsState() }

// MARK: - NavigationItems action

@objc private func addBarButtonTapped() { print(#function) let selectedPhotos = collectionView.indexPathsForSelectedItems?.reduce([], { (photosss, indexPath) -> [UnsplashPhoto] in var mutablePhotos = photosss let photo = photos[indexPath.item] mutablePhotos.append(photo) return mutablePhotos })

let alertController = UIAlertController(title: "", message: "\(selectedPhotos!.count) фото будут добавлены в альбом", preferredStyle: .alert)
let add = UIAlertAction(title: "Добавить", style: .default) { (action) in
  let tabbar = self.tabBarController as! MainTabBarController
  let navVC = tabbar.viewControllers?[1] as! UINavigationController
  let likesVC = navVC.topViewController as! LikesCollectionViewController

  likesVC.photos.append(contentsOf: selectedPhotos ?? [])
  likesVC.collectionView.reloadData()

  self.refresh()
}
let cancel = UIAlertAction(title: "Отменить", style: .cancel) { (action) in
}
alertController.addAction(add)
alertController.addAction(cancel)
present(alertController, animated: true)

}

@objc private func actionBarButtonTapped(sender: UIBarButtonItem) { print(#function)

let shareController = UIActivityViewController(activityItems: selectedImages, applicationActivities: nil)


shareController.completionWithItemsHandler = { _, bool, _, _ in
  if bool {
    self.refresh()
  }
}

shareController.popoverPresentationController?.barButtonItem = sender
shareController.popoverPresentationController?.permittedArrowDirections = .any
present(shareController, animated: true, completion: nil)

}

// MARK: - Setup UI Elements

private func setupCollectionView() { collectionView.backgroundColor = .white

collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "CellId")
collectionView.register(PhotosCell.self, forCellWithReuseIdentifier: PhotosCell.reuseId)

collectionView.layoutMargins = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
collectionView.contentInsetAdjustmentBehavior = .automatic
collectionView.allowsMultipleSelection = true

if let waterfallLayout = collectionViewLayout as? WaterfallLayout {
  waterfallLayout.delegate = self
}

}

private func setupEnterLabel() { collectionView.addSubview(enterSearchTermLabel) enterSearchTermLabel.centerXAnchor.constraint(equalTo: collectionView.centerXAnchor).isActive = true enterSearchTermLabel.topAnchor.constraint(equalTo: collectionView.topAnchor, constant: 50).isActive = true }

private func setupSpinner() { view.addSubview(spinner) spinner.centerXAnchor.constraint(equalTo: collectionView.centerXAnchor).isActive = true spinner.centerYAnchor.constraint(equalTo: collectionView.centerYAnchor).isActive = true }

private func setupNavigationBar() { let titleLabel = UILabel(text: "PHOTOS", font: .systemFont(ofSize: 15, weight: .medium), textColor: #colorLiteral(red: 0.5019607843, green: 0.4980392157, blue: 0.4980392157, alpha: 1)) navigationItem.leftBarButtonItem = UIBarButtonItem.init(customView: titleLabel) navigationItem.rightBarButtonItems = [actionBarButtonItem, addBarButtonItem] navigationController?.hidesBarsOnSwipe = true actionBarButtonItem.isEnabled = false addBarButtonItem.isEnabled = false }

private func setupSearchBar() { let seacrhController = UISearchController(searchResultsController: nil) navigationItem.searchController = seacrhController navigationItem.hidesSearchBarWhenScrolling = false seacrhController.hidesNavigationBarDuringPresentation = false seacrhController.obscuresBackgroundDuringPresentation = false seacrhController.searchBar.delegate = self }

// MARK: - UICollecionViewDataSource, UICollecionViewDelegate

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { enterSearchTermLabel.isHidden = photos.count != 0 return photos.count }

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PhotosCell.reuseId, for: indexPath) as! PhotosCell let unspashPhoto = photos[indexPath.item] cell.unsplashPhoto = unspashPhoto return cell }

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { undateNavButtonsState() let cell = collectionView.cellForItem(at: indexPath) as! PhotosCell guard let image = cell.photoImageView.image else { return } selectedImages.append(image)

}

override func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) { undateNavButtonsState() let cell = collectionView.cellForItem(at: indexPath) as! PhotosCell guard let image = cell.photoImageView.image else { return } if let index = selectedImages.firstIndex(of: image) { selectedImages.remove(at: index) } } }

// MARK: - UISearchBarDelegate

extension PhotosCollectionViewController: UISearchBarDelegate {

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { print(searchText) self.spinner.startAnimating() timer?.invalidate() timer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: false, block: { (_) in self.networkDataFetcher.fetchImages(searchTerm: searchText) { [weak self] (searchResults) in guard let fetchedPhotos = searchResults else { return } self?.spinner.stopAnimating() self?.photos = fetchedPhotos.results self?.collectionView.reloadData() self?.refresh() } }) } }

// MARK: - WaterfallLayoutDelegate extension PhotosCollectionViewController: WaterfallLayoutDelegate { func waterfallLayout(_ layout: WaterfallLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

let photo = photos[indexPath.item]
return CGSize(width: photo.width, height: photo.height)

} } LikesCollection VC代码:

import UIKit

class LikesCollectionViewController: UICollectionViewController {

var photos = UnsplashPhoto

private lazy var trashBarButtonItem: UIBarButtonItem = { return UIBarButtonItem(barButtonSystemItem: .trash, target: self, action: nil) }()

private let enterSearchTermLabel: UILabel = { let label = UILabel() label.text = "You haven't add a photos yet" label.textAlignment = .center label.font = UIFont.boldSystemFont(ofSize: 20) label.translatesAutoresizingMaskIntoConstraints = false return label }()

override func viewDidLoad() { super.viewDidLoad()

collectionView.backgroundColor = .white
collectionView.register(LikesCollectionViewCell.self, forCellWithReuseIdentifier: LikesCollectionViewCell.reuseId)
collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)

let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
layout.minimumInteritemSpacing = 1
layout.minimumLineSpacing = 1

setupEnterLabel()
setupNavigationBar()

}

// MARK: - Setup UI Elements

private func setupEnterLabel() { collectionView.addSubview(enterSearchTermLabel) enterSearchTermLabel.centerXAnchor.constraint(equalTo: collectionView.centerXAnchor).isActive = true enterSearchTermLabel.topAnchor.constraint(equalTo: collectionView.topAnchor, constant: 50).isActive = true }

private func setupNavigationBar() { let titleLabel = UILabel(text: "FAVOURITES", font: .systemFont(ofSize: 15, weight: .medium), textColor: #colorLiteral(red: 0.5, green: 0.5, blue: 0.5, alpha: 1)) navigationItem.leftBarButtonItem = UIBarButtonItem.init(customView: titleLabel) navigationItem.rightBarButtonItem = trashBarButtonItem trashBarButtonItem.isEnabled = false }

// MARK: - UICollectionViewDataSource

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { enterSearchTermLabel.isHidden = photos.count != 0 return photos.count }

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: LikesCollectionViewCell.reuseId, for: indexPath) as! LikesCollectionViewCell let unsplashPhoto = photos[indexPath.item] cell.unsplashPhoto = unsplashPhoto return cell } }

// MARK: - UICollectionViewDelegateFlowLayout extension LikesCollectionViewController: UICollectionViewDelegateFlowLayout {

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let width = collectionView.frame.width return CGSize(width: width/3 - 1, height: width/3 - 1) } }

展开
收起
游客5akardh5cojhg 2019-12-04 23:10:50 475 0
1 条回答
写回答
取消 提交回答
  • 不,这不是你要做的。选项卡条控制器不会在两个视图控制器之间导航。它在两个导航控制器之间导航。

    这就是问题所在。您无法在viewDidAjar中检测到任何内容,因为您的视图控制器不是选项卡控制器切换时出现的内容。出现的是导航控制器。

    2019-12-04 23:11:18
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载