Tyring to set UIImage from different fiew via delegation - ios

I have a view controller with a UIImage that will be sent to a service. I'm trying to load this image from another view so I'm using delegation. However I cant seem to understand why it will not work. I set some messages to print in the console and I see them right before the call to the method but its not setting the variables and I'm not sure why.
The view are linked via a navigation controller and to return I use popViewController and to go to the other view controller I use a simple show segue from the button. The "confirmation" of the selected image is done with a button that is supposed to send the image to the previous view controller.
This is the main controller with the UIImage I want to set from another view:
import UIKit
class MainNewPost: UIViewController{
override var preferredStatusBarStyle: UIStatusBarStyle{
return.lightContent
}
var imageForUpload: UIImage?
var isImageSelected: Bool = false
#IBOutlet weak var newPostBox: TVUIView!
#IBOutlet weak var newPost: UIButton!
#IBAction func sendNewPost(_ sender: Any) {
var imageId = ""
if isImageSelected {
imageId = UUID().uuidString
ImageWS.sendIamge(imageId: imageId, image: imageForUpload!, {()}, error: {(errorMessate) in
print(errorMessate)
})
}
if (newPostBox.text!.isEmpty) {
Util.showMessage(controller: self, message: "Ingrese un texto", seconds: 5)
} else {
PostWS.newPost({() in
Util.showMessage(controller: self, message: "Enviaste un mensaje!", seconds: 3.0)
}, img: imageId, postBody: newPostBox.text!, personId: PersonBE.shared!.personId, posterName: PersonBE.shared!.displayName, error: {(errorMessage) in print(errorMessage)})
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
extension MainNewPost: NewPostDelegate {
// This is not executing. I'm calling it since I see the console message by the flag and image dont get set
func selectedImage(_ image: UIImage, _ isImageSelected: Bool) {
self.imageForUpload = image
self.isImageSelected = isImageSelected
}
}
This is the view controller where I select the image:
import UIKit
protocol NewPostDelegate {
func selectedImage(_ image: UIImage, _ isImageSelected: Bool)
}
class imageSelectViewController: UIViewController {
var postDelegate: NewPostDelegate?
#IBOutlet weak var imageView: UIImageView!
var imagePicker: ImagePicker!
override func viewDidLoad() {
super.viewDidLoad()
self.imagePicker = ImagePicker(presentationController: self, delegate: self)
}
#IBAction func showImagePicker(_ sender: UIButton) {
self.imagePicker.present(from: sender)
}
override var preferredStatusBarStyle: UIStatusBarStyle{
return.lightContent
}
#IBAction func clickBtnBack(_ sender: Any){
self.navigationController?.popViewController(animated: true)
}
#IBAction func selectImage(_ sender: Any) {
// I see this message printed in the console
// This button is suposed to send the selected image to the previous view controller
print("In selectImage button")
self.postDelegate?.selectedImage(imageView.image!, true)
self.navigationController?.popViewController(animated: true)
}
}
extension imageSelectViewController: ImagePickerDelegate {
func didSelect(image: UIImage?) {
// I see this message printed in the console
print("In didSelect delegate method")
self.imageView.image = image
self.postDelegate?.selectedImage(image!, true)
}
}
Thanks!

It seems that var postDelegate: NewPostDelegate? in your imageSelectViewController instance remains nil. Which means that even if you're calling a delegate method, there is no delegate specified itself.
In order to fix it you should assign your MainNewPost instance reference to var postDelegate: NewPostDelegate? of your destination imageSelectViewController while preparing for segue.

Related

How to Change an image in 2nd View Controller using a Button in 1st View Controller

I have stetted up switch in my First View Controller and assigned the following action to it :-
class ThirdViewController: UIViewController {
#IBOutlet weak var Image: UIImageView!
#IBOutlet weak var playerNum1Button: UIButton!
#IBOutlet weak var toggleSwitch: UISwitch!
override func viewDidLoad() {
super.viewDidLoad()
self.toggleSwitch.setOn(UserDefaults.standard.bool(forKey: "toggleState"), animated: true)
}
#IBAction func numPlayers1(_ sender: Any) {
performSegue(withIdentifier: "3to8segue", sender: self)
}
IBAction func toggleSwitch(_ sender: UISwitch) {
if (toggleSwitch.isOn == true) {
Image.image = UIImage(named: "Night")
}
else {
Image.image = UIImage(named: "background")
}
UserDefaults.standard.set(sender.isOn, forKey: "toggleState")
}
It is Able to change the image in the First View Controller. But, how can I get that switch to change the image in 2nd View controller as well when it is turned on? Thanks for the help!
Using Notification Center
Example:
In FirstViewController:
#IBAction func onPressButton(_ sender: UIButton) {
UserDefaults.standard.set(sender.isSelected, forKey: "pressedButton")
NotificationCenter.default.post(name: "changeImageBackground", object: nil)
}
In SecondViewController:
NotificationCenter.default.addObserver(target: self, selector: #selector(didChangeImageBackground), name: "changeImageBackground", object: nil)
#objc func didChangeImageBackground() {
let changed = UserDefaults.standard.bool(forKey: "pressedButton")
if changed {
// image when pressed
} else {
// image when haven't pressed
}
}

Swift delegate doesn't get called

I'm using PanelKit for an iOS app that I'm developing right now.
I have set up a panel, which is a ViewController, and have set up a button in that ViewController. When pressed, I want a picture to be displayed in another ViewController (the main VC that's instantiated when the app is launched). I'm using a delegate for this. However, my delegate function never gets executed. Here's my code:
Delegate:
protocol IMGPickerDelegate: class {
func didFinishPicking(_ imageData: UIImage)
}
Panel ViewController:
class IMGLibraryViewController: UIViewController, PanelContentDelegate {
weak var delegate: IMGPickerDelegate?
#IBAction func fireButton(_ sender: Any) {
let imageSample = UIImage(named: "sample")!
didPickImage(imageDData: imageSample)
}
func didPickImage(imageDData: UIImage) {
delegate?.didFinishPicking(imageDData)
}
Main ViewController:
class ImageDisplayViewController: UIViewController, IMGPickerDelegate {
#IBOutlet weak var imageDisplayViewOutlet: UIImageView!
var imgLibrary = IMGLibraryViewController()
func didFinishPicking(_ imageData: UIImage) {
imageDisplayViewOutlet.image = imageData
}
override func viewDidLoad() {
super.viewDidLoad()
imgLibrary.delegate = self
}

Passing an object with data from one view to another view in Swift using Segue

I am trying to pass an object with data from one view to another view using Swift4. However, I get this problem when trying to print.
Here is the message that I get:
2018-03-21 21:47:03.542578-0400 Sudoku[64427:1070575] <UIView:
0x7fd28bc0b4c0; frame = (0 0; 414 736); autoresize = W+H; layer =
<CALayer: 0x604000038500>>'s window is not equal to
<Sudoku.setupSudoku: 0x7fd28bf256c0>'s view's window!
Here is the picture of my View. Identifier for segue: setupScreen
Here is the code in setPlayer:
class setPlayer: UIViewController {
var singleUser:playerInfor=playerInfor()
var randomGame:Int = -1
var playerName:String=""
#IBOutlet weak var txtPlayerName: UITextField!
var sizeOfSudoku:Int = 9 //for project 1, let take one size of sudoku
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "Communication-Network-Vector-Illustration.jpg")!)
self.txtPlayerName.layer.borderWidth=2.0
self.txtPlayerName.layer.borderColor=UIColor.blue.cgColor
// Do any additional setup after loading the view.
}
func randomNumber(){
self.randomGame=Int(arc4random_uniform(3)+1)
}
func initialize(){
//if(size)
if txtPlayerName.text?.isEmpty ?? true {
self.playerName="Player1"
} else {
self.playerName=self.txtPlayerName.text!
}
self.singleUser=playerInfor(playerName:self.playerName,size:self.sizeOfSudoku,time:0, randomSudoku:self.randomGame)
//print (self.singleUser.size)
}
#IBAction func startButton(_ sender: Any) {
initialize()
performSegue(withIdentifier: "setupScreen", sender: self.singleUser)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "setupScreen"{
if let destination=segue.destination as?setupSudoku{
destination.objectPlayer=sender as?playerInfor
print(singleUser.playerName)
}
}
}
#IBAction func size9(_ sender: Any) {
self.sizeOfSudoku=9
}
#IBAction func Size6(_ sender: Any) {
self.sizeOfSudoku=9
}
Here is playerInfor
import Foundation
import UIKit
class playerInfor{
var playerName:String=""
var size:Int=0
var time:Int=var randomSudoku:Int=0
init(playerName:String,size:Int,time:Int,randomSudoku:Int) {
self.playerName=playerName
self.size=size
self.time=time
self.randomSudoku=randomSudoku
}
}
Here is setupSudoku
class setupSudoku: UIViewController , UICollectionViewDelegate, UICollectionViewDataSource{
#IBOutlet weak var stackViewButtons: UIStackView!
#IBOutlet weak var myCollectionView: UICollectionView!
var newNumber=0
var objectPlayer:playerInfor!
override func viewDidLoad() {
super.viewDidLoad()
print(self.objectPlayer?.time)
}
}
You have setup your segue incorrectly in your storyboard, and it is attempting to segue twice. To confirm this is the case, select your segue in your storyboard and check if your button highlights, like this:
To resolve, delete this segue and create a new one, by control-dragging from the view controller (not the button) to the destination, like so:
Make sure to reset the new segues identifier back to setupScreen. Then you should be good to go!

how to pass a UI ImageView from one view controller to another? swift 3

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.

Button function can only work once

I am making an app with a main view controller and a menu button in it. By clicking the button, there is another view controller as the side-menu appearing and then users can choose to go back to the previous main view controller. However, then the menu button doesn't work anymore.
The code for the main view controller is here (not the container view controller):
#objc
protocol CenterViewControllerDelegate {
optional func toggleLeftPanel()
optional func collapseSidePanels()
}
class CenterViewController: UIViewController {
#IBOutlet weak var titleLabel: UILabel!
var delegate: CenterViewControllerDelegate?
#IBAction func MenuTapped(sender: AnyObject) {
delegate?.toggleLeftPanel?()
}
// MARK: Button actions
}
You have to take a bool flag to track open/collapse of menu, something like this:
#objc
protocol CenterViewControllerDelegate {
optional func toggleLeftPanel()
optional func collapseSidePanels()
}
class CenterViewController: UIViewController {
var _isMenuOpen = false
#IBOutlet weak var titleLabel: UILabel!
var delegate: CenterViewControllerDelegate?
#IBAction func MenuTapped(sender: AnyObject) {
if _isMenuOpen{
delegate?.toggleLeftPanel?()
_isMenuOpen = true
}
else{
delegate?.collapseSidePanels?()
_isMenuOpen = false
}
}
// MARK: Button actions
}
Hope this will help you.

Resources