[英]How to push to view in tabbar with some data in a variable?
我有一个tabbar,其中包括:TabBar - HomeView - > UserProfileView - > Gallery - LeaderBoardView - SettingView - ........在这里,用户可以通过userProfileView选择各种选项(相机,图库,头像)在主视图中更改用户配置文件。 让我们说,用户选择图库,他/她去图库,选择图像,代码将推回到主视图,图像。 用户配置文件图像将更改,但标签栏控制器消失。 选择头像也是一样的情况。
由于我们需要将图像放在homeview中,因此无法推送到tabbar视图,因为图像的变量位于homeview中。 我们可以在homeview中推送到tabbar视图和图像数据吗? 或者我们可以推送到homeview并显示标签栏控制器。
我是xcode / Swift的新手。 我正在尝试将测验应用作为练习。 我用过
NavigationController.pushviewController
以下是一些引用问题的代码。
fileprivate func presentPhotoPickerController() {
let myPickerController = UIImagePickerController()
myPickerController.allowsEditing = true
myPickerController.delegate = self
myPickerController.sourceType = .photoLibrary
self.present(myPickerController, animated: true)
}
@IBAction func goToGalleryBtn(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
PHPhotoLibrary.requestAuthorization { (status) in
switch status {
case .authorized:
self.presentPhotoPickerController()
break
case .notDetermined:
if status == PHAuthorizationStatus.authorized{
self.presentPhotoPickerController()
}
case .restricted:
let alert = UIAlertController(title: "Photo Library Restricted", message: "Photo Library access is restricted and cannot be accessed.", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default)
alert.addAction(okAction)
self.present(alert, animated: true)
case .denied:
let alert = UIAlertController(title: "Photo Library Denied", message: "Photo Library access was previously denied. Please update your Settings if you wish to change this.", preferredStyle: .alert)
let goToSettingsAction = UIAlertAction(title: "Go to Settings", style: .default){(action) in
DispatchQueue.main.async {
let url = URL(string: UIApplication.openSettingsURLString)!
UIApplication.shared.open(url, options: [:])
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
alert.addAction(goToSettingsAction)
alert.addAction(cancelAction)
self.present(alert, animated: true)
}
}
}
}
@IBAction func cancelBtn(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
}
extension UserAvatarViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate{
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[.editedImage] as? UIImage{
imageGallery = image
}else if let image = info[.originalImage] as? UIImage{
imageGallery = image
}
dismiss(animated: true, completion: nil)
let storyBoard: UIStoryboard = UIStoryboard(name: "HomeViewController", bundle: nil)
let homeVC = storyBoard.instantiateViewController(withIdentifier: "HomeVC") as! HomeViewController
homeVC.imageView = imageGallery
// self.navigationController?.pushViewController(homeVC, animated: true)
self.navigationController?.popViewController(animated: true)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
}
顺便说一句,我对每个视图控制器都使用了不同的故事板。
我想,你的问题是使用didFinishPickingMediaWithInfo
方法。 您正在创建HomeViewController
的新对象,而不是从tabbar中选择控制器。我假设您已将tabbarcontroller
设置为窗口上的rootviewcontroller
。
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[.editedImage] as? UIImage{
imageGallery = image
}else if let image = info[.originalImage] as? UIImage{
imageGallery = image
}
dismiss(animated: true, completion: nil)
let appDel = UIApplication.shared.delegate as! AppDelegate
let tabbar = appDel.window.rootViewController as TabbarViewController
// if you know the index of the controller on tabbar, you can directly access it
let homeVC = tabbar.viewControllers[0] as! HomeViewController
homeVC.imageView = imageGallery
self.navigationController?.popViewController(animated: true)
}
如果不是窗口的rootViewController
请告诉我你是如何设置tabbar
的
像这样更新你的imagePickerController(picker: didFinishPickingMediaWithInfo)
方法
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[.editedImage] as? UIImage{
imageGallery = image
}else if let image = info[.originalImage] as? UIImage{
imageGallery = image
}
dismiss(animated: true, completion: nil)
//Assuming your viewcontroller hierarchy is
// TabbarVC -> HomeVC -> GalleryVC -> ImagePickerVC
if let homeVC = self.parent as? HomeViewController {
homeVC.imageView = imageGallery
}
self.navigationController?.popViewController(animated: true)
}
并在HomeViewController
viewWillAppear
中更新图像。 希望它能满足您的需求。
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[.editedImage] as? UIImage{
imageGallery = image
}else if let image = info[.originalImage] as? UIImage{
imageGallery = image
}
dismiss(animated: true, completion: nil)
let storyBoard:UIStoryboard = UIStoryboard(name: "TabBarViewController", bundle: nil)
let tabBarVC = storyBoard.instantiateViewController(withIdentifier: "TabBarVC") as! UITabBarController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = tabBarVC
let navBar = tabBarVC.viewControllers![0] as! UINavigationController
let homeVC = navBar.topViewController as! HomeViewController
homeVC.imageView = imageGallery
self.navigationController?.popViewController(animated: true)
}
这终于奏效了。 感谢Hisenberg和Najeeb ur Rehman向我提供了使用Tab bar视图控制器及其子项的提示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.