Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use the dialog with a custom view controller? #80

Closed
RyanHop3 opened this issue Dec 28, 2016 · 18 comments
Closed

How to use the dialog with a custom view controller? #80

RyanHop3 opened this issue Dec 28, 2016 · 18 comments
Labels

Comments

@RyanHop3
Copy link

Please fill out this template when filing an issue.
All ℹ symbols should be replaced with information on the issue.
Please remove this line and all above before submitting.

Report

Environment

Please provide information on your development environment, so we can build with the same scenario.

  • Mac OS version (e.g. 10.12): ℹ10.12.2
  • Xcode version (e.g. 8.0): ℹ8.0
  • PopupDialog version (e.g. 0.5.0): ℹ0.5.0
  • Minimum deployment target (e.g. 9.0): ℹ9.0
  • Language (Objective-C / Swift): ℹSwift
  • In case of Swift - Version (e.g. 3.0): ℹ3.0

Dependency management

If you are not using any dependency managers, you can remove this section.

Please note: If you are using CocoaPods with Xcode 8, CocoaPods 1.1.0 is required.

  • Dependency manager (e.g. CocoaPods): ℹCocoaPods
  • Version (e.g. 1.1.0): ℹ 1.1.0

Hi I was wondering if it is possible to pass information to the PopupViewController which can then be used in labels. I have three buttons in a view controller that all launch the same PopupViewController, but I want them to display different information in the labels etc. based on what button was selected, but I cannot seem to find a way to do this! e.g. if button 1 is selected, the label displays 'Button 1 was selected', if button 2 was selected then the label displays 'Button 2 was selected' etc.

@RyanHop3 RyanHop3 changed the title How to pass information to the PopupD? How to pass information to the Popup? Dec 28, 2016
@mwfire
Copy link
Member

mwfire commented Dec 31, 2016

Hey @RyanHop3,

I am not entirely sure if I understand the question. So title, body and image can be either set at initialization or modified at "runtime". You should be able to put this logic in the presenting view controller. If you have logic that exceeds the capability of the standard view, you can always use a custom view controller. More info on this can be found at #66, for example.

Cheers
Martin

@RyanHop3
Copy link
Author

Sorry If I was vague and thanks for the reply. I am trying to add a table view to the popup to displaying some information but can't see to do so successfully. I tried creating a custom view controller and xib file like in the cosmos demo but the table view will not appear and cellForRowAtIndexPath is not getting called. Do you know if it is possible to add a table view to the popUp?

Thanks, Ryan.

@mwfire mwfire changed the title How to pass information to the Popup? How to use the dialog with a custom view controller? Jan 1, 2017
@mwfire
Copy link
Member

mwfire commented Jan 1, 2017

Hey @RyanHop3,

I have published an example project with a custom view controller containing a table view. Check it out at https://github.com/mwfire/PopupDialogCustomExample.

Hope that helps :)
Martin

@RyanHop3
Copy link
Author

RyanHop3 commented Jan 3, 2017

Ah Excellent, thank you. One query about this; I have further added a label to the Popup to act as a title (called titleForLabel) but I can't seem to pass information to the PopupTableViewController to set the information for the title(the title is different depending on which button activated the Popup).

In the host view controller function I have the following code:

vc.cities = popupArray
vc.titleForLabel = popupTitle

but in PopupTableViewController when I try to print the titleForLabel in ViewDidLoad() it returns nil.

titleForLabel is declared as: public var titleForLabel: String? in PopupTableViewController.

Can you see why this is the case (I have probably overlooked something) and how to pass the information to titleForLabel?

Thanks for the help, Ryan.

@mwfire
Copy link
Member

mwfire commented Jan 3, 2017

Hey @RyanHop3,

hm, hard to tell. If possible, could you please share your demo project?

Cheers
Martin

@RyanHop3
Copy link
Author

RyanHop3 commented Jan 5, 2017

I'm having issues trying to upload the project to dropbox....

Only 3 small lines of code are needed to test my problem:

In PopupTableViewController:

public var titleForTable: String?

and in viewDidLoad():
print(titleForTable!)

then in ViewController:

within onSelectCityTapped():

vc.titleForTable = "Test Title"

When this code is added I would expect print(titleForTable!) to return "Test Title" but instead it is returning nil. How can this issue be resolved or is there another to pass information to the Popup?

Thanks, Ryan.

@mwfire
Copy link
Member

mwfire commented Jan 5, 2017

You are trying to print titleForTable in viewDidLoad. However, you are only setting it at onSelectCityTapped, which is way later in the process. Up to this point, titleForTable is nil.

Is that correct?

@RyanHop3
Copy link
Author

RyanHop3 commented Jan 5, 2017

How do I change the code to pass this information through to PopupTableViewController then (e.g. how is it done for the cities array)? The idea would be to add a UILabel to the Popup displaying the string within titleForLabel

@mwfire
Copy link
Member

mwfire commented Jan 5, 2017

Have a look at ViewController.swift. That's where the the dialog is created, the custom view controller is configured and the cities are set.

// Create our custom view controller programatically
let vc = PopupTableViewController(nibName: nil, bundle: nil)

// Create the PopupDialog with a completion handler,
// called whenever the dialog is dismissed
let popup = PopupDialog(viewController: vc, gestureDismissal: false) {
    guard let city = vc.selectedCity else { return }
    print("User selected city: \(city)")
}

// Create a cancel button for the dialog,
// including a button action
let cancel = DefaultButton(title: "Cancel") {
    print("User did not select a city")
}

// Add the cancel button we just created to the dialog
popup.addButton(cancel)

// Moreover, we set a list of cities on our custom view controller
vc.cities = ["Munich", "Budapest", "Krakow", "Rome", "Paris", "Nice", "Madrid", "New York", "Moscow", "Peking", "Tokyo"]

// We also pass a reference to our PopupDialog to our custom view controller
// This way, we can dismiss and manipulate it from there
vc.popup = popup

// Last but not least: present the PopupDialog
present(popup, animated: true, completion: nil)

@RyanHop3
Copy link
Author

RyanHop3 commented Jan 5, 2017

ViewController.swift is where I was setting the titleForTable:

// Create our custom view controller programatically
let vc = PopupTableViewController(nibName: nil, bundle: nil)

// Create the PopupDialog with a completion handler,
// called whenever the dialog is dismissed
let popup = PopupDialog(viewController: vc, gestureDismissal: false) {
    guard let city = vc.selectedCity else { return }
    print("User selected city: \(city)")
}

// Create a cancel button for the dialog,
// including a button action
let cancel = DefaultButton(title: "Cancel") {
    print("User did not select a city")
}

// Add the cancel button we just created to the dialog
popup.addButton(cancel)

// Moreover, we set a list of cities on our custom view controller
vc.cities = ["Munich", "Budapest", "Krakow", "Rome", "Paris", "Nice", "Madrid", "New York", "Moscow", "Peking", "Tokyo"]

// Set test title for the Title of the Popup
vc.titleForTable = "Test Title"

// We also pass a reference to our PopupDialog to our custom view controller
// This way, we can dismiss and manipulate it from there
vc.popup = popup

// Last but not least: present the PopupDialog
present(popup, animated: true, completion: nil)

But the information doesn't seem to be passing through to the custom view controller. How do I access the variable within the custom view controller?

Thanks for your help and time by the way!

@RyanHop3
Copy link
Author

RyanHop3 commented Jan 6, 2017

Any update or solution for this?

Thanks, Ryan.

@morganzysman
Copy link

Hello Ryan,
I'm trying to do the same thing.
Reading your exemple I have a doubt .
let vc = PopupTableViewController(nibName: nil, bundle: nil)

Here you are not specifying any xib file to be used, but i could use mine as it is used in the demo?

best regards,

Morgan

@RyanHop3
Copy link
Author

RyanHop3 commented Jan 7, 2017

Hi Morgan,

Apologies, I was copying the comment of Martyn and adding my code to it, but forgot to add the nibName as PopupTableViewController. The rest of the code and my issue still stands though.

Kind Regards, Ryan.

@mwfire
Copy link
Member

mwfire commented Jan 7, 2017

Hey guys, I am out of town for a few days. Please provide a repo/download link to an example project showcasing the problem as requested. I was not able to reproduce it. Thanks!

@RyanHop3
Copy link
Author

RyanHop3 commented Jan 8, 2017

https://www.dropbox.com/s/3zh8w62irn59zsd/PopupDialogCustomExample-master%202.zip?dl=0

In this project I have tried to add the title as shown but it loads nil.

@mwfire
Copy link
Member

mwfire commented Jan 8, 2017

@RyanHop3, I changed the code to your needs, you can find it here.

As mentioned in my comment above, you cannot expect the title or cities to be anything else than nil in viewDidLoad, as it is set at a later point in time. If you want to have it set and available in viewDidLoad already, you should create a custom initializer where these values are passed in.
Please find a link to some information on the view controller life cycle here.

With cities, I used the didSet observer on the property, so it would reload the table view whenever the values are changed at any point in time. I did the same for the titleForLabel property in the example above.

Moreover, I recommend to not use fixed widths for views that should fill the PopupDialog. The width adjusts to the viewport. Running your code changes of the example, you can see in the console errors as of broken constraints.

Please also note that these things are not connected to PopupDialog, but to the general use of the iOS framework.

Cheers
Martin

@mwfire mwfire closed this as completed Jan 8, 2017
@RyanHop3
Copy link
Author

RyanHop3 commented Jan 8, 2017

Hi Martyn,

Thank you for fixing my issue, I am still new to swift and learning!

All the best, Ryan.

@mwfire
Copy link
Member

mwfire commented Jan 8, 2017

Hey @RyanHop3. Sure thing! 💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants