The image is not from one VC to another VC.
The problem is displaying it to the image view in the main UIViewController. I have everything hooked up correctly in the Storyboard.
Click here to view my storyboard layout.
Please note some of my unnecessary code has been removed from both ViewController classes.
Here is my first UIViewController:
import UIKit
class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
var imagePicker: UIImagePickerController!
#IBAction func editPhoto(_ sender: UIButton) {
let ViewController2 = storyboard?.instantiateViewController(withIdentifier: "AddNewEaterySegue") as! ViewController2
ViewController2.imageForEdit = imageView.image!
print(imageView)
print(imageView.image!)
navigationController?.pushViewController(ViewController2, animated: true)
}
#IBOutlet weak var imageView: UIImageView! {
didSet {
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
}
}
}
Here is my second UIViewController:
import UIKit
class ViewController2: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
var filter : CIFilter!
var imageForEdit = UIImage()
var imagePicker: UIImagePickerController!
#IBOutlet weak var select: UISegmentedControl!
#IBOutlet weak var imageLabel: UIImageView!
#IBOutlet weak var textField: UITextField!
#IBAction func saveButtonPressed(_ sender: UIImage) {
if textField.text == "" {
print("Не все поля заполнены")
} else {
}
performSegue(withIdentifier: "unwindSegueFromViewController", sender: sender)
}
override func viewDidLoad() {
super.viewDidLoad()
imageLabel.image = imageForEdit
print(imageLabel)
// Do any additional setup after loading the view.
}
#IBAction func tappedEnter(_ sender: Any) {
if textField.text?.isEmpty ?? true {
return
} else {
if let texttxt = textField.text {
let data = texttxt.data(using: .ascii, allowLossyConversion: false)
if select.selectedSegmentIndex == 0
{
filter = CIFilter(name: "CICode128BarcodeGenerator")
} else {
filter = CIFilter(name: "CIQRCodeGenerator")
}
filter.setValue(data, forKey: "inputMessage")
let transform = CGAffineTransform(scaleX: 5, y: 5)
let image = UIImage(ciImage: filter.outputImage!.transformed(by: transform))
imageLabel.image = image
}
}
}
}
When you are pushing to ViewController2 your imageView in ViewController is empty.
Where exactly are you setting the image for imageView?
In your storyboard, the imageView in ViewController is empty and also in the didSet method for imageView in ViewController you have not set any image to the imageView.
Try this in ViewController:
#IBOutlet weak var imageView: UIImageView! {
didSet {
//Add a test image to your project.
imageView.image = UIImage(named: "YOUR_TEST_IMAGE_NAME.jpeg")
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
}
}
EDIT: Ok I guess I was not clear on your question. So in order to send the image from "ViewController2" back to "ViewController", you can use many methods but the way I would do it is by using Protocols.
I have done it in a generic way but you can apply this to send any kind of data from one view controller back to the previous view controller.
Try this in ViewController:
import UIKit
protocol ImageViewProtocol{
func sendImageToViewController(theImage: UIImage)
}
class ViewController: UIViewController, ImageViewProtocol {
func sendImageToViewController(theImage: UIImage) {
imageView.image = theImage
}
#IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
let viewcontroller2 = storyboard?.instantiateViewController(withIdentifier: "viewcontroller2") as! ViewController2
viewcontroller2.delegate = self
self.navigationController?.pushViewController(viewcontroller2, animated: true)
}
}
Try this in ViewController2:
import UIKit
class ViewController2: UIViewController {
var delegate: ImageViewProtocol!
#IBOutlet weak var barcodeImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
barcodeImageView.image = UIImage(named: "test_image.jpg")
}
#IBAction func saveButtonAction(_ sender: Any) {
delegate.sendImageToViewController(theImage: barcodeImageView.image!)
self.navigationController?.popViewController(animated: true)
}
}
Related
I want to update the layout of a popover, I've already tried using the setNeedsLayout() and layoutIfNeeded() methods but couldn't find a solution.
I have a simple screen with 4 buttons that call a popover, within the popover there are 2 extra buttons:
Button -> hides button3, and moves button1 and button2 to the left.
reposition -> changes the sourceview of the popover from the button1/2/3/4 (sender) to button4.
Picture of the layout
The problem I have is that I'm not able to update/refresh the screen, and the popover keeps pointing to the former source view position.
Picture of the popover
Somehow I need a method that updates my view and which is stronger than layoutIfNeeded() because if the screen is changed from portrait/landscape the popover changes automatically. Here I changed portrait/landscape/portrait
GitHub of the project. GitHub
The code used is the following:
class ViewController: UIViewController, ButtonDidDisappearDelegate {
#IBOutlet weak var button1: UIButton!
#IBOutlet weak var button2: UIButton!
#IBOutlet weak var button3: UIButton!
#IBOutlet weak var button4: UIButton!
#IBOutlet weak var constrain2: NSLayoutConstraint!
var popVC: PopVC?
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func buttonPopover(_ sender: UIButton) {
configureAndPresentPopover(from: sender)
}
func buttonDisappear() {
self.constrain2.constant = 100
self.button3.isHidden = !self.button3.isHidden
if !self.button3.isHidden {
self.constrain2.constant = 10
}
}
func repositionPopover() {
DispatchQueue.main.async {
if let popVC = self.popVC {
self.self.popoverPresentationController(popVC.popoverPresentationController!, willRepositionPopoverTo: self.button4.bounds, in: self.button4)
}
self.view.setNeedsLayout()
self.view.layoutIfNeeded()
}
}
}
extension ViewController: UIPopoverPresentationControllerDelegate {
func configureAndPresentPopover(from sender: UIButton) {
popVC = storyboard?.instantiateViewController(withIdentifier: "popVC") as? PopVC
guard let popVC = popVC else {
return
}
popVC.popOverDelegate = self
popVC.modalPresentationStyle = .popover
let popOverVC = popVC.popoverPresentationController
popOverVC?.delegate = self
popOverVC?.sourceView = sender
popOverVC?.sourceRect = sender.bounds
popVC.preferredContentSize = CGSize(width: 300, height: 60)
popOverVC?.permittedArrowDirections = .down
self.present(popVC, animated: true)
}
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
func popoverPresentationController(_ popoverPresentationController: UIPopoverPresentationController,
willRepositionPopoverTo rect: CGRect,
in view: UIView) {
popoverPresentationController.sourceView = view
popoverPresentationController.sourceRect = rect
}
}
and the popover controller:
protocol ButtonDidDisappearDelegate: class {
func configureAndPresentPopover(from sender: UIButton)
func buttonDisappear()
func repositionPopover()
}
class PopVC: UIViewController {
weak var popOverDelegate: ButtonDidDisappearDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
#IBOutlet weak var popoverButton: UIButton!
#IBAction func popoverAction(_ sender: Any) {
popOverDelegate?.buttonDisappear()
DispatchQueue.main.async {
self.view.setNeedsLayout()
self.view.layoutIfNeeded()
}
}
#IBAction func reposition(_ sender: Any) {
popOverDelegate?.repositionPopover()
}
}
You need to specify the new position of the arrow and ask for the layout of the presentation controller's view, not only the presented view.
Try :
func repositionPopover() {
let popOverController = presentedViewController?.presentationController as? UIPopoverPresentationController
popOverController?.sourceView = button4
popOverController?.containerView?.setNeedsLayout()
popOverController?.containerView?.layoutIfNeeded()
}
You can try setting the sourceview and sourcerect once again. call this function when the sourcview bounds are changed.
I put a container inside my MainViewController. Then added FirstViewController inside that container
let containerView = UIView()
view.addSubview(containerView)
let controller = FirstViewController()
addChildViewController(controller)
containerView.addSubview(controller.view)
controller.didMove(toParentViewController: self)
There are a UITextField and a UIButton inside FirstViewController. There is a label inside SecondViewController and var passedText = "" to update the UILabel text. I just want to set the UILabel text according to the UITextField text from FirstViewController when I press UIButton and it should leads to SecondViewController as well. This is the UIButton action method from FirstViewController. I'm not using Storyboard
#IBAction func btnPressed(_ sender: Any) {
let secondVC = SecondViewController()
addChildViewController(secondVC)
view.addSubview(secondVC.view)
secondVC.view.frame = view.bounds
secondVC.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
secondVC.didMove(toParentViewController: self)
secondVC.passedText = inputTextField.text!
}
This is FirstViewController
class FirstViewController: UIViewController {
#IBOutlet weak var inputTextField: UITextField!
let secondVC = SecondViewController()
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func btnPressed(_ sender: Any) {
addChildViewController(secondVC)
view.addSubview(secondVC.view)
secondVC.view.frame = view.bounds
secondVC.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
secondVC.didMove(toParentViewController: self)
secondVC.passedText = inputTextField.text!
}
}
This is SecondViewController
class SecondViewController: UIViewController {
var passedText = ""
#IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
label.text = passedText
}
}
Closure
class ParentViewController: UIViewController {
var embeddedViewController: UIViewController?
override func viewDidLoad() {
super.viewDidLoad()
let firstViewController = FirstViewController()
embedViewController(firstViewController)
firstViewController.passDataClosure = { [weak self] text in
let secondViewController = SecondViewController()
self?.embedViewController(secondViewController)
secondViewController.passedText = text
}
}
func embedViewController(_ viewController: UIViewController) {
removeEmbedded()
viewController.view.autoresizingMask = [.flexibleHeight, .flexibleWidth]
addChildViewController(viewController)
view.addSubview(viewController.view)
viewController.didMove(toParentViewController: self)
embeddedViewController = viewController
}
func removeEmbedded() {
embeddedViewController?.view.removeFromSuperview()
embeddedViewController?.didMove(toParentViewController: nil)
embeddedViewController?.removeFromParentViewController()
}
}
-
class FirstViewController: UIViewController {
var passDataClosure: ((String?) -> Void)?
#IBOutlet weak var inputTextField: UITextField!
#IBAction func btnPressed(_ sender: Any) {
passDataClosure?(inputTextField.text)
}
}
-
class SecondViewController: UIViewController {
var passedText: String? {
didSet {
label.text = passedText
}
}
#IBOutlet weak var label: UILabel!
}
Delegate
class ParentViewController: UIViewController, FirstViewControllerDelegate {
var embeddedViewController: UIViewController?
override func viewDidLoad() {
super.viewDidLoad()
let firstViewController = FirstViewController()
embedViewController(firstViewController)
firstViewController.delegate = self
}
func embedViewController(_ viewController: UIViewController) {
removeEmbedded()
viewController.view.autoresizingMask = [.flexibleHeight, .flexibleWidth]
addChildViewController(viewController)
view.addSubview(viewController.view)
viewController.didMove(toParentViewController: self)
embeddedViewController = viewController
}
func removeEmbedded() {
embeddedViewController?.view.removeFromSuperview()
embeddedViewController?.didMove(toParentViewController: nil)
embeddedViewController?.removeFromParentViewController()
}
// MARK: FirstViewControllerDelegate
func firstViewController(_ firstViewController: FirstViewController, pass text: String?) {
let secondViewController = SecondViewController()
embedViewController(secondViewController)
secondViewController.passedText = text
}
}
-
class SecondViewController: UIViewController {
var passedText: String? {
didSet {
label.text = passedText
}
}
#IBOutlet weak var label: UILabel!
}
-
protocol FirstViewControllerDelegate: class {
func firstViewController(_ firstViewController: FirstViewController, pass text: String?)
}
class FirstViewController: UIViewController {
weak var delegate: FirstViewControllerDelegate?
#IBOutlet weak var inputTextField: UITextField!
#IBAction func btnPressed(_ sender: Any) {
delegate?.firstViewController(self, pass: inputTextField.text)
}
}
As far as I can see, this should work:
secondVC.label.text = inputTextField.text
You currently have secondVC.label = inputTextField.text which shouldn't even compile.
you can create a singleton class for your scenario. so that you can access all of your important data from anywhere either from your firstViewController or SecondViewController.
class YourDataClass : NSObject{
var textFieldData = String()
var lblSeconddata = String()
struct Static
{
public static var instance: YourDataClass?
}
class var singleton: YourDataClass
{
if Static.instance == nil
{
Static.instance = YourDataClass()
}
return Static.instance!
}
}
now somewhere in your controller
YourDataClass.Singleton.textFieldData = txtFld.Text
YourDataClass.Singleton. lblSeconddata = lblText.Text
in smame way you can get it anywhere
I am trying to pass this UI Image View from one view controller to another on the same storyboard. I have already passed a UI TextField to a UI Label and UI Button. Now I need to do it with a UI Image View.
Here are my two view controllers.
import UIKit
class PhotoShareViewController: UIViewController {
#IBOutlet weak var imageView: UIImageView!
#IBOutlet weak var contentTextView: UITextView!
#IBOutlet weak var thatTextField: UITextField!
#IBOutlet weak var thisTextField: UITextField!
var presenter: PhotoShareModuleInterface!
var image: UIImage!
#IBAction func thisUploadPhoto(_ sender: Any) {
if thisTextField.text != "" && thatTextField.text != ""
{
performSegue(withIdentifier: "segue", sender: nil)
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var photoShareLabelViewController = segue.destination as! PhotoShareLabelViewController
photoShareLabelViewController.thisString = thisTextField.text!
photoShareLabelViewController.thatString = thatTextField.text!
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
imageView.image = image
}
override var prefersStatusBarHidden: Bool {
return true
}
#IBAction func didTapCancel(_ sender: AnyObject) {
presenter.cancel()
presenter.pop()
}
/* #IBAction func didTapDone(_ sender: AnyObject) {
guard let message = thatTextField.text, !message.isEmpty else {
return
}
guard let messageOne = thisTextField.text, !messageOne.isEmpty else {
return
}
presenter.finish(with: image, content:message)
presenter.dismiss()
}
}
*/
}
extension PhotoShareViewController: PhotoShareViewInterface {
var controller: UIViewController? {
return self
}
}
if someone could tell me how to send the image from one view controller into another that would be awesome!
You didn't show PhotoShareLabelViewController, but presumably it has:
var thisString: String?
Add
var thisImage: UIImage?
And then pass it along the same way as thisString
photoShareLabelViewController.thisImage = imageView.image
And pick it up in the viewDidLoad of PhotoShareLabelViewController (which is probably where you pick up thisString). Something like:
self.imageView.image = self.thisImage
But, using whatever variable name you are using.
In short, just copy what you are doing for thisString, but use a UIImage as the type instead of String.
NOTE: You are not passing a UIImageView from one VC to another (that is impossible). You are passing the image from an imageview from one VC to another.
I have completed all the needed code for delegate to work. In my viewcontroller:
class ViewController: UIViewControllerCustomViewDelegate
I also have this:
override func viewDidLoad() {
super.viewDidLoad()
let myCustomView = Bundle.main.loadNibNamed("ImageHeaderView", owner: self, options: nil)?[0] as! ImageHeaderView
myCustomView.delegate = self
}
func goToNextScene() {
print("GOTOSCENE2")
}
And in my custom view I have this:
import UIKit
protocol CustomViewDelegate: class { // make this class protocol so you can create `weak` reference
func goToNextScene()
}
#available(iOS 10.0, *)
class ImageHeaderView : UIView {
#IBOutlet weak var followme: UISwitch!
#IBOutlet weak var profileImage : UIImageView!
#IBOutlet weak var backgroundImage : UIImageView!
weak var delegate: CustomViewDelegate?
override func awakeFromNib() {
super.awakeFromNib()
self.backgroundColor = UIColor(hex: "E0E0E0")
self.profileImage.layer.cornerRadius = self.profileImage.frame.size.height / 2
self.profileImage.clipsToBounds = true
self.profileImage.layer.borderWidth = 1
self.profileImage.layer.borderColor = UIColor.white.cgColor
//self.profileImage.setRandomDownloadImage(80, height: 80)
//self.backgroundImage.setRandomDownloadImage(Int(self.frame.size.width), height: 100)
}
#IBAction func followme(_ sender: AnyObject) {
UserDefaults.standard.set(followme.isOn, forKey: "followme")
}
#IBAction func logout(_ sender: AnyObject) {
delegate?.goToNextScene()
print("GOTOSCENE")
}
}
There is are no errors thrown but when I click/tap the button, nothing happens. It just prints "GOTOSCENE".
What I feel is
Your problem is right here
override func viewDidLoad() {
super.viewDidLoad()
let myCustomView = Bundle.main.loadNibNamed("ImageHeaderView", owner: self, options: nil)?[0] as! ImageHeaderView
myCustomView.delegate = self
}
I think you have added imageHeaderview from storyboard and in viewdidload you are creating new object of ImageHeaderView and assigning delegate to newly created object.
Try to outlet your ImageHeaderView and assign delegate to outleted object.
Hope this will fix your issue.
In my current project i have detail view that shows a specific record from my table view. I have the following labels
#IBOutlet weak var vacationImageView: UIImageView!
#IBOutlet weak var percentSaved: UILabel!
#IBOutlet weak var cost: UILabel!
#IBOutlet weak var saved: UILabel!
#IBOutlet weak var circleProgressView: CircularProgressView!
#IBOutlet weak var daysDepart: UILabel!
I call a popover that I want to send the current text value of saved to my popup, allow the user to edit it and send it back to the view. Here is my popover call.
#IBAction func addPopover(sender: UIView) {
let savingsInformationViewController = storyboard?.instantiateViewControllerWithIdentifier("SavingsAddPopover") as UIViewController
savingsInformationViewController.modalPresentationStyle = .Popover
savingsInformationViewController.preferredContentSize = CGSizeMake(200, 200)
let popoverController = savingsInformationViewController.popoverPresentationController
popoverController?.sourceView = sender
popoverController?.permittedArrowDirections = .Any
popoverController?.delegate = self
presentViewController(savingsInformationViewController, animated: true, completion: nil)
}
I would have thought I could reference the data object from the popover but can't..at least not the way I'm thinking.
class ViewController: UIViewController,SavingViewControllerDelegate,UIPopoverPresentationControllerDelegate{
#IBOutlet var labelText: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func buttonPopOverClick(sender: UIButton)
{
let savingsInformationViewController = storyboard?.instantiateViewControllerWithIdentifier("SavingsAddPopoverVC") as SavingViewController
savingsInformationViewController.delegate = self
savingsInformationViewController.strSaveText=labelText.text
savingsInformationViewController.modalPresentationStyle = .Popover
if let popoverController = savingsInformationViewController.popoverPresentationController {
popoverController.sourceView = sender
popoverController.sourceRect = sender.bounds
popoverController.permittedArrowDirections = .Any
popoverController.delegate = self
}
presentViewController(savingsInformationViewController, animated: true, completion: nil)
}
func saveText(strText: NSString) {
labelText.text=strText
}
// MARK: - UIPopoverPresentationControllerDelegate
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController!) -> UIModalPresentationStyle {
return .FullScreen
}
func presentationController(controller: UIPresentationController!, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController! {
return UINavigationController(rootViewController: controller.presentedViewController)
}
}
protocol SavingViewControllerDelegate
{
func saveText(var strText : NSString)
}
class SavingViewController: UIViewController {
#IBOutlet var textField: UITextField!
var delegate : SavingViewControllerDelegate?
var strSaveText : NSString!
override func viewDidLoad() {
super.viewDidLoad()
textField.text = strSaveText
// Do any additional setup after loading the view.
}
#IBAction func buttonDone(sender: UIButton)
{
if (self.delegate) != nil
{
delegate?.saveText(textField.text)
self.dismissViewControllerAnimated(true, nil)
}
}
}
just to point out
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController!) -> UIModalPresentationStyle {
return .none
}
not woking properly on ios 12 /xcode 11, at least for popover tableview controller
The call below works
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}