Swift: my destinationVC segue is showing nil - swift

I'm having users fill out some profile information via texts fields (name, email, etc.), which is used to set my ProfileContoller.shared.profile values. When I get to my navigation segue to pass the data over, my destinationVC.profile will not set its value to the sending profile object and I get nil instead.
My sendingVC is embedded in a navigation controller, while my destinationVC is embedded in Tab Bar Controller.
Segue SendingVC
Attributes Inspector SendingVC
DestinationVC
// Sending View Controller:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let profile = ProfileController.shared.profile else { return }
if segue.identifier == "signUpMemicTBC" {
let destinationVC = segue.destination as? ProfileViewController
destinationVC?.profile = profile
// Receiving ProfileViewController:
class ProfileViewController: UIViewController {
// MARK: - IBOutlets
#IBOutlet weak var fullNameLabel: UILabel!
#IBOutlet weak var usernameLabel: UILabel!
#IBOutlet weak var emailLabel: UILabel!
// MARK: - Landing Pad
var profile : Profile? {
didSet {
updateViews()
}
}
override func viewDidLoad() {
super.viewDidLoad()
updateViews()
}
func updateViews () {
guard let profile = profile else { return }
fullNameLabel.text = profile.firstName + " " + profile.lastName
usernameLabel.text = profile.username
emailLabel.text = profile.email
}
}
// ProfileController:
class ProfileController {
// MARK: - Properties
var profile : Profile?
// MARK: - Singleton
static let shared = ProfileController()
}
My sending object has data:
(lldb) po profile
Profile: 0x600000c873c0
The destination object is unexpectedly nil:
(lldb) po destinationVC?.profile
nil

didSet triggers while your vc in segue isn't loaded yet so all outlets are nil
you need to put updateViews() inside viewDidLoad
Plus your destination is a tabBar
let tab = segue.destination as! UITabBarController
let destinationVC = tab.viewControllers![2] as! ProfileViewController

I think all you have to do is call the updateViews() in the main thread.
didSet {
print("did set profile called")
DispatchQueue.main.async{[weak self] in
guard let self = self else {
return
}
self.updateViews()
}
}
Another option would be to update the views in viewDidLoad()
override func viewDidLoad() {
if let prof = self.profile {
self.updateViews()
}
}

You can use below code to safely pass data to the ProfileViewController.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let profile = ProfileController.shared.profile else { return }
if segue.identifier == "signUpMemicTBC" {
guard let tabBarController = segue.destination as? UITabBarController else { return }
guard let profileController = tabBarController.viewControllers?.index(at: 2) as? ProfileViewController else {
return
}
profileController.profile = profile
}
}

Related

Can't pass value from FirstVC to SecondVC using segue

I have two ViewControllers connected via Show segue. I need to pass NSSlider's value from ViewController to SecondViewCotroller.
So, moving slider in ViewController a variable updates in SecondViewController.
How to update a value of imagesQty variable?
// FIRST VIEW CONTROLLER
import Cocoa
class ViewController: NSViewController {
#IBOutlet weak var slider: NSSlider!
#IBOutlet weak var photosLabel: NSTextField!
#IBAction func segueData(_ sender: NSSlider) {
photosLabel.stringValue = String(slider.intValue) + " photos"
self.performSegue(withIdentifier: NSStoryboardSegue.Identifier(rawValue: "SegueIdentifierForSecondVC"), sender: slider)
}
func prepare(for segue: NSStoryboardSegue, sender: NSSlider?) {
if segue.identifier!.rawValue == "SegueIdentifierForSecondVC" {
if let secondViewController =
segue.destinationController as? SecondViewController {
secondViewController.imagesQty = slider.integerValue
}
}
}
}
and
// SECOND VIEW CONTROLLER
import Cocoa
class SecondViewController: NSViewController {
var imagesQty = 30
override func viewWillAppear() {
super.viewWillAppear()
self.view.wantsLayer = true
print("viewWillAppear – Qty:\(imagesQty)")
//let arrayOfViews: [NSImageView] = [view01...view12]
let url = URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Desktop/ArrayOfElements")
do {
let fileURLs = try FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil, options: [.skipsHiddenFiles]).reversed()
let photos = fileURLs.filter { $0.pathExtension == "jpg" }
for view in arrayOfViews {
//"imagesQty" is here
let i = Int(arc4random_uniform(UInt32(imagesQty-1)))
let image = NSImage(data: try Data(contentsOf: photos[i]))
view.image = image
view.imageScaling = .scaleNone
}
} catch {
print(error)
}
}
First of all the purpose and benefit of NSStoryboardSegue.Identifier is to create an extension to be able to avoid literals.
extension NSStoryboardSegue.Identifier {
static let secondVC = NSStoryboardSegue.Identifier("SegueIdentifierForSecondVC")
}
Then you can write
self.performSegue(withIdentifier: .secondVC, sender: slider)
and
if segue.identifier! == .secondVC { ...
This error occurs because imagesQty is declared in viewWillAppear rather than on the top level of the class.
Change it to
class SecondViewController: NSViewController {
var imagesQty = 30 // Int is inferred
// override func viewWillAppear() {
// super.viewWillAppear()
// }
}
There is another mistake: The signature of prepare(for segue is wrong. It must be
func prepare(for segue: NSStoryboardSegue, sender: Any?) {
You can‘t change the value because the var is defined in the function and not in the class.
Make your var a class property and it should work.
class SecondViewController: UIViewController {
var imagesQty: Int = 30
...
}

iOS - How to set imageview of a parent view controller through child container?

I am currently using a container view and I want to change the value of parent view controller imageView through child view controller using delegates, but it always returns nil.
import UIKit
protocol updateImage {
func userIsDone(image:UIImage)
}
class ViewController: UIViewController, updateImage{
#IBOutlet weak var imageView:UIImageView!
var image = UIImage(named: "hello.png")
override func viewDidLoad() {
super.viewDidLoad()
self.imageView.image=self.image
}
func userIsDone(image: UIImage) {
self.image=image
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "containerChild"{
let nextView = segue.destinationViewController as! ControllerChild
nextView.image=self.image
nextView.delegate=self
}
}
}
class ControllerChild:UIViewController{
var image=UIImage(named: "newhello.png")
var delegate: updateImage? = nil
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func changeImage(sender:UIButton){
if(delegate != nil){
self.delegate!.userIsDone(self.image!)
print("I am Called!")
}
}
}
Remove this line:
nextView.image = image
And change the userIsDone function to:
func userIsDone(image: UIImage) {
imageView.image = image
}
As side notes:
In Swift you don't need to use self outside of blocks/closures. For example, you don't need to self.imageView.image = self.image -- just imageView.image = image will do.
Protocols should be upper-cased, like UpdateImage (instead of updateImage).
You don't need that extra .png to reference images, just the title, E.g.: "hello".
Apple describes Swift code conventions in their awesome Swift book.
Here is the code, refactored (Swift 3):
import UIKit
protocol UpdateImageProtocol {
func userIsDone(image: UIImage)
}
class ViewController: UIViewController, UpdateImageProtocol {
#IBOutlet weak var imageView: UIImageView!
var image = UIImage(named: "hello")
override func viewDidLoad() {
super.viewDidLoad()
userIsDone(image: image!)
}
func userIsDone(image: UIImage) {
imageView.image = image
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "containerChild" {
let nextView = segue.destination as! ChildViewController
nextView.delegate = self
}
}
}
class ChildViewController: UIViewController {
var image = UIImage(named: "newHello")
var delegate: UpdateImageProtocol?
#IBAction func changeImage(sender: UIButton) {
delegate?.userIsDone(image: image!)
}
}

How do I call a function from a different view controller?

I have what is basically a notes app, and I am trying to call a function that is in one view controller from another. How do I do this, or is there a better way I should do it?
Here's where I am trying to call the function from
class NoteDetailViewController: UIViewController
{
#IBOutlet weak var funcButton: UIButton!
#IBAction func funcButTouched(sender: UIButton)
{
// where i want to call the function
}
}
And where the function is
class ListTableViewController: UITableViewController
{
// the function
func wordCount()
{
var contentArr = Project.sharedInstance.content.componentsSeparatedByString(" ")
for (index, element) in contentArr.enumerate()
{
let location = conciseArr.indexOf(element)
if (location != nil)
{
contentArr[index] = inflatedArr[location!]
afterStr = contentArr.joinWithSeparator(" ")
Project.sharedInstance.after = afterStr
}
}
}
}
I've tried just creating an instance of ListTableViewController and just calling the function that way but I get an error.
You could use delegation.
First, declare a delegate protocol. This should be in your NoteDetailViewController.swift file but outside of the class declaration:
protocol NoteDetailViewControllerDelegate: class {
func noteDetailViewControllerButtonTouched(controller: NoteDetailViewController)
}
Next, add a delegate property to your NoteDetailViewController:
weak var delegate: NoteDetailViewControllerDelegate?
Now, we use the #IBAction to tell the delegate, which will be the ListTableViewController:
#IBAction func funcButTouched(sender: UIButton)
{
delegate?.noteDetailViewControllerButtonTouched(self)
}
Finally, back in ListTableViewController (assuming this controller is the one directly before a NoteDetailViewController is shown), conform to the protocol and use prepareForSegue to set the delegate to itself:
class ListTableViewController: UITableViewController, NoteDetailViewControllerDelegate {
// ... more stuff ...
// Implement the delegate protocol
func noteDetailViewControllerButtonTouched(controller: NoteDetailViewController) {
// Do something! The button was pressed!
wordCount()
}
// Set ourselves as delegate when we are about to show the other view controller
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let yourVC = segue.destinationViewController as? NoteDetailViewController {
yourVC.delegate = self
}
}
}
You should finish with something like this:
NoteDetailViewController.swift:
class NoteDetailViewController: UIViewController
{
#IBOutlet weak var funcButton: UIButton!
weak var delegate: NoteDetailViewControllerDelegate?
#IBAction func funcButTouched(sender: UIButton)
{
delegate?.noteDetailViewControllerButtonTouched(self)
}
}
protocol NoteDetailViewControllerDelegate: class {
func noteDetailViewControllerButtonTouched(controller: NoteDetailViewController)
}
ListTableViewController.swift:
class ListTableViewController: UITableViewController, NoteDetailViewControllerDelegate
{
// the function
func wordCount()
{
var contentArr = Project.sharedInstance.content.componentsSeparatedByString(" ")
for (index, element) in contentArr.enumerate()
{
let location = conciseArr.indexOf(element)
if (location != nil)
{
contentArr[index] = inflatedArr[location!]
afterStr = contentArr.joinWithSeparator(" ")
Project.sharedInstance.after = afterStr
}
}
}
// Implement the delegate protocol
func noteDetailViewControllerButtonTouched(controller: NoteDetailViewController) {
// Do something! The button was pressed!
wordCount()
}
// Set ourselves as delegate when we are about to show the other view controller
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let yourVC = segue.destinationViewController as? NoteDetailViewController {
yourVC.delegate = self
}
}
}
Just follow bellow code its very simple :-
NoteDetailViewController.swift:
class NoteDetailViewController: UIViewController
{
#IBOutlet weak var funcButton: UIButton!
var parentListTableView : ListTableViewController!
#IBAction func funcButTouched(sender: UIButton)
{
parentListTableView.wordCount()
}
}
ListTableViewController.swift:
class ListTableViewController: UITableViewController
{
// the function
func wordCount()
{
var contentArr = Project.sharedInstance.content.componentsSeparatedByString(" ")
for (index, element) in contentArr.enumerate()
{
let location = conciseArr.indexOf(element)
if (location != nil)
{
contentArr[index] = inflatedArr[location!]
afterStr = contentArr.joinWithSeparator(" ")
Project.sharedInstance.after = afterStr
}
}
}
// Set ourselves as delegate when we are about to show the other view controller
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let yourVC = segue.destinationViewController as? NoteDetailViewController {
yourVC.parentListTableView = self
}
}
}

Pass data between ViewController and ContainerViewController

I'm working on an app, and need to pass data between view and containerView. I need to send data and receive data from both Views.
Let me explain better:
I can change the Label Master (Touch the Container Button) by protocol, but I can not change the Label Container (Touch the Master button). What happens is the Master connects with the container by a following. But do not have a follow Container linking to the Master.
I tried to add but segue to, but it worked.
The Master View Controller:
import UIKit
protocol MasterToContainer {
func changeLabel(text:String)
}
class Master: UIViewController, ContainerToMaster {
#IBOutlet var containerView: UIView!
var masterToContainer:MasterToContainer?
#IBOutlet var labelMaster: UILabel!
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "containerViewSegue" {
let view = segue.destinationViewController as? Container
view!.containerToMaster = self
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func button_Container(sender: AnyObject) {
masterToContainer?.changeLabel("Nice! It's work!")
}
func changeLabel(text: String) {
labelMaster.text = text
}
}
The Container View Controller:
import UIKit
protocol ContainerToMaster {
func changeLabel(text:String)
}
class Container: UIViewController, MasterToContainer {
var containerToMaster:ContainerToMaster?
#IBOutlet var labelContainer: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func button_Master(sender: AnyObject) {
containerToMaster?.changeLabel("Amazing! It's work!")
}
func changeLabel(text: String) {
labelContainer.text = text
}
}
Can someone help me?
All you need to do is keep a reference to Container in your master view controller.
That is, you should add an instance variable to Master that will hold a reference to the view controller, not just the view. You'll need to set it in prepareForSegue.
So the beginning of Master View Controller would look something like this:
class Master: UIViewController, ContainerToMaster {
#IBOutlet var containerView: UIView!
var containerViewController: Container?
#IBOutlet var labelMaster: UILabel!
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "containerViewSegue" {
containerViewController = segue.destinationViewController as? Container
containerViewController!.containerToMaster = self
}
}
And then in your button function, simply change the label using the variable you just added.
Example:
#IBAction func button_Container(sender: AnyObject) {
containerViewController?.changeLabel("Nice! It's work!")
}
This means you can get rid of your MasterToContainer protocol too.
I tested this code, so I know it works, but unfortunately I am an Objective-C dev, and know nothing about best practices in Swift. So I don't know if this is the best way to go about it, but it certainly works.
Edit:
Here's the exact code I've tested:
Master.swift:
import UIKit
class Master: UIViewController, ContainerToMaster {
#IBOutlet var containerView: UIView!
#IBOutlet var labelMaster: UILabel!
var containerViewController: Container?
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "containerViewSegue" {
containerViewController = segue.destinationViewController as? Container
containerViewController!.containerToMaster = self
}
}
#IBAction func button_Container(sender: AnyObject) {
containerViewController?.changeLabel("Nice! It's work!")
}
func changeLabel(text: String) {
labelMaster.text = text
}
}
Container.swift:
import UIKit
protocol ContainerToMaster {
func changeLabel(text:String)
}
class Container: UIViewController {
#IBOutlet var labelContainer: UILabel!
var containerToMaster:ContainerToMaster?
#IBAction func button_Master(sender: AnyObject) {
containerToMaster?.changeLabel("Amazing! It's work!")
}
func changeLabel(text: String) {
labelContainer.text = text
}
}
I solved it with this code
To send data from ViewController -> ContainerViewController
Class ViewController : UIViewController {
func sendData(MyStringToSend : String) {
let CVC = childViewControllers.last as! ContainerViewController
CVC.ChangeLabel( MyStringToSend)
}
}
in your ContainerViewController
Class ContainerViewController : UIViewController {
#IBOutlet weak var myLabel: UILabel!
func ChangeLabel(labelToChange : String){
myLabel.text = labelToChange
}
}
To send data from ContainerViewController -> ViewController
Class ContainerViewController : UIViewController {
func sendDataToVc(myString : String) {
let Vc = parentViewController as! ViewController
Vc.dataFromContainer(myString)
}
}
and in ViewController
Class ViewController : UIViewController {
func dataFromContainer(containerData : String){
print(containerData)
}
}
I hope this will help someone.
you can use this extension to access the container child
extension UIViewController {
func getContainerChild<vc:UIViewController>(_ viewController : vc,_ hasNavigation : Bool = true) -> (vc) {
guard let vc = self.children[0] as? UINavigationController else {return viewController}
if hasNavigation {
guard let childVC = vc.children[0] as? PurchasedHistoryListVC else {
return viewController}
return childVC as! vc
} else {
return vc as! vc
}
}
}
so you can do some thing like this in your view Controller
let vc = self.getContainerChild(yourChildViewControllerClass())
vc.functionName()

Why can't I assign value on didSet?

I have two view. On my first view i create some data and send to other view lets name view2. So my view1 has code:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showUserDetail" {
(segue.destinationViewController as! UserDetailTableViewController).detailContact = detailCoversationContact
}
}
In my detailCoversationContact i have detail about this one user, this detail like number, nick etc. Now as i expect this data are put to variable detailContact in my view2
And this is my cod in view2 this code is work:
class UserDetailTableViewController: UITableViewController {
#IBOutlet weak var nickNameLabel: UILabel!
var nickMy: String = ""
var detailContact: AnyObject? {
didSet {
// Update the user interface for the detail item.
if let detail = self.detailContact {
self.nickMy = (detail.valueForKey("nickName") as? String)!
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
nickNameLabel.text = nickMy
}
}
When i send data to detailContact' i check is didSet if yes i set my nickMy and this is work. But what is first ? setting my var or viewDidLoad?
this is my code and not working and can someone explain why ?
class UserDetailTableViewController: UITableViewController {
#IBOutlet weak var nickNameLabel: UILabel!
var detailContact: AnyObject? {
didSet {
// Update the user interface for the detail item.
if let detail = self.detailContact {
self.title = detail.valueForKey("nickName") as? String
self.nickNameLabel?.text = (detail.valueForKey("nickName") as? String)!
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
Ok this is self.title and work i see the name of user but title is from tableView. But my nickNameLabel is not change ? why ?
At the time of prepareForSegue, the IBOutlets have not been set up yet. So, self.nickNameLabel is still nil, and optional chaining makes self.nickNameLabel?.text = ... safely do nothing.
Set the value in viewDidLoad() when the IBOutlets have been set up:
override func viewDidLoad() {
super.viewDidLoad()
self.nickNameLabel?.text = (detail.valueForKey("nickName") as? String)!
}
Alternatively, you can trigger didSet by setting the detailContact to itself in viewDidLoad(). You have to trick Swift in order to do this because it thinks assigning detailContact to itself does nothing:
override func viewDidLoad() {
super.viewDidLoad()
detailContact = (detailContact) // trigger didSet to set up the label
}