Using the Guidebook application as a reference, I have ran into some issues trying to add a couple of specific enhancements, which I was hoping there might be some input on.
Firstly, I would like the user to be able to copy and paste content from any of the labels in the Detail View (the idea that they may wish to use some of this in their custom notes, or send text to other apps such as WhatsApp messages to friends, etc.).
If I just focus on a specific label, than this is pretty straightforward using the in-built GestureRecognizer and over-riding the copyMenu and copy functions. From viewDidLoad():
let tapGesture = UITapGestureRecognizer(target: self , action: #selector ((copyMenu( _ :))))
Then, in the copy method, the contents of the label gets added to the pasteboard through “UIPasteboard.general.string = self.textBodyLabel.text”
The issue comes when trying to copy the text from a different label (for example, the address label).
I assumed I could do this pretty easily using tags and a Switch statement to copy the text of the label that was selected (double-clicked or long-pressed). However, I have been unable to get this to work. If I call self.view.addGestureRecognizer(tapGesture), the value of tag is always returned as 0 (the value for the view, rather than a specific element in it) so the Switch statement does not get executed properly. Similarly, the tag is always returned as 0 within viewDidLoad().
Essentially, how do we determine which label has been selected (double-clicked or long-pressed) so that action can be performed accordingly? I understand that standard approach would generally be to use buttons but that would not be applicable here.
I have explicitly set isUserInteractionEnabled to true for each label, both programatically and in Main.Storyboard, but that makes no difference (which is what I expected, since I believe this is true by default, and wouldn’t explain why it works when I focus on just one specific label).
The second roadblock I have encountered is regarding segues and view controller transitions.
I have expanded upon Chris’s example and separated the Favourites list into it’s own View Controller. This works fine but there is a behavioural issue with respect to the Back Button within the DetailViewController.
In Chris’s example, he uses “navigationController?.popViewController(animated: true)” when the Back Button is tapped. For me, this works fine for the initial traversing between PlacesVC and DetailVC. However, once the FavouritesViewController is loaded, all subsequent attempts at popping the ViewController fail (i.e. nothing happens when the Back Button is selected). A few points to note about this:
- This happens regardless of whether the transition from PlacesVC to FavouritesVC is done via a Segue or by calling instantiateViewController.
- I use the same kind of segue to get from FavouritesVC to DetailVC as I do from PlacesVC to DetailVC.
- Calling popToRootViewController gives the same behaviour.
I then tried using the Back Button tapped method to call self.dismiss(animated: true, completion: nil), which yielded some interesting behaviour.
- This fails to dismiss the very first time the DetailViewController is loaded after selecting a location from the PlacesVC.
- However, if I then navigate back to either the PlacesVC or FavouritesVC directly (using buttons I added) and then load the DetailVC again by selecting a place from the menu, this works fine (as does all subsequents attempts to use the Back Button).
- Additionally, if *the user initially goes from PlacesVC to FavesVC and then loads the DetailVC by selecting a location from the list of Favourites, the Back button works the first time.
- Thus, it’s only when going from PlacesVC to DetailVC the first time that the Back button fails.
- I experimented with changing from *a Segue to calling instantiateViewController(withIdentifier: Constants.Storyboard.detailViewController) within PlacesVC (at didSelectRowAt) but this caused a run-time crash whenever the user clicked on a location from the menu.
I suspect I may not be fully understanding how to best use Segues but have spent quite a bit of time on this without being able to figure it out completely, so any pointers would be greatly appreciated.
Finally, what is the best way to get URL’s that are in the text to appear as clickable links rather than plain text, when being read from the realm file?
I appreciate any assistance with any of these three items and I hope that I have properly articulated each one and that each scenario should be easily reproducible from Chris’s Guidebook application.