I want to have a UISheet which shows half way. My app is landscape only.
When I show the UISheet, it just shows at full height, even though I set its detents to be medium.
Here's my code:
#IBAction func openMemoryLog(_ sender: UIButton) {
if let memoryLogViewController = storyboard?.instantiateViewController(withIdentifier: "memoryLogViewController") {
if let presentationController = memoryLogViewController.presentationController as? UISheetPresentationController {
presentationController.detents = [ .medium() ]
present(memoryLogViewController, animated: true)
}
}
}
class MemoryLogViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print("Memory log loaded")
}
}
This is what it looks like. The MemoryLogViewController just has a simple text in the middle.
I want this shown half way on landscape mode. Is this possible?
Also any hints on how to make the nested if let look a bit cleaner?
#IBAction func callButton(_ sender: UIButton) {
let contact = contacts[sender.tag]
self.makeCall(contact: contact)
}
func makeCall(contact: Characters){
if let phoneCallURL = URL(string: "tel://\(contact.phone)") {
let application:UIApplication = UIApplication.shared
if (application.canOpenURL(phoneCallURL)) {
application.open(phoneCallURL, options: [:], completionHandler: nil)
}
}
}
The phone appears, just the the right one. The "sender.tag" is suppose to give me the array number to which the phone belongs. Instead the phone that shows up is the one attached to the first object in the array.
Solved my own problem by adding cell.callbutton.tag = indexPath.row to the cellForRowAt function.
I keep getting a fatal error saying how a value was unwrapped and it was nil and I don't understand how. When I instantiate a view controller with specific variables they all show up, but when I perform a segue to the exact VC, the values don't show up.
Take these functions for example...
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if let displayVC = storyboard?.instantiateViewController(withIdentifier: Constants.Storyboards.TeachStoryboardID) as? SchoolEventDetailsViewController {
displayVC.selectedEventName = events[indexPath.row].eventName
displayVC.selectedEventDate = documentsDate[indexPath.row].eventDate
displayVC.selectedEventCost = documentsCost[indexPath.row].eventCost
displayVC.selectedEventGrade = documentsGrade[indexPath.row].eventGrade
displayVC.selectedEventDocID = documentsID[indexPath.row]?.docID
navigationController?.pushViewController(displayVC, animated: true)
}
}
This combined with this function :
func verifyInstantiation() {
if let dateToLoad = selectedEventDate {
dateEditableTextF.text = dateToLoad
}
if let costToLoad = selectedEventCost {
costEditableTextF.text = costToLoad
}
if let gradesToLoad = selectedEventGrade {
gradesEditableTextF.text = gradesToLoad
}
if let docIDtoLoad = selectedEventDocID {
docIDUneditableTextF.text = docIDtoLoad
}
if let eventNameToLoad = selectedEventName {
eventNameEditableTextF.text = eventNameToLoad
}
}
Helps load the data perfectly, but when I try to perform a segue from a search controller the data is not there.
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = selectedEventName
I set the title of the vc to have the event name , and I also recently added a text field to store it as well for experimental purposes (this question).
Now the issue is I want to do a data transfer from an Algolia Search Controller to that VC and I got all the other fields to show up, except for one and that was the document ID. So I created a completion handler function to get the document ID as a string and have it inserted into the vc when the segue is performed, just like how it's there when the vc is instantiated.
Here is the function :
func getTheEventDocID(completion: #escaping ((String?) -> ())) {
documentListener = db.collection(Constants.Firebase.schoolCollectionName).whereField("event_name", isEqualTo: selectedEventName ?? navigationItem.title).addSnapshotListener(includeMetadataChanges: true) { (querySnapshot, error) in
if let error = error {
print("There was an error fetching the documents: \(error)")
} else {
self.documentsID = querySnapshot!.documents.map { document in
return EventDocID(docID: (document.documentID) as! String)
}
let fixedID = "\(self.documentsID)"
let substrings = fixedID.dropFirst(22).dropLast(3)
let realString = String(substrings)
completion(realString)
}
}
}
I thought either selectedEventName or navigationItem.title would get the job done and provide the value when I used the function in the data transfer function which I will show now :
//MARK: - Data Transfer From Algolia Search to School Event Details
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
otherVC.getTheEventDocID { (eventdocid) in
if let id = eventdocid {
if segue.identifier == Constants.Segues.fromSearchToSchoolEventDetails {
let vc = segue.destination as! SchoolEventDetailsViewController
vc.selectedEventName = self.nameTheEvent
vc.selectedEventDate = self.dateTheEvent
vc.selectedEventCost = self.costTheEvent
vc.selectedEventGrade = self.gradeTheEvent
vc.selectedEventDocID = id
}
}
}
}
But it ends up showing nothing when a search result is clicked which is pretty upsetting, I can't understand why they're both empty values when I declared them in the SchoolEventDetailsVC. I tried to force unwrap selectedEventName and it crashes saying there's a nil value and I can't figure out why. There's actually a lot more to the question but I just tried to keep it short so people will actually attempt to read it and help since nobody ever reads the questions I post, so yeah thanks in advance.
I'm a litte confused what the otherVC is, which sets a property of itself in the getTheEventDocID, whilste in the closure you set the properties of self, which is a different controller. But never mind, I hope you know what you are doing.
Since getTheEventDocID runs asynchronously, the view will be loaded and displayed before the data is available. Therefore, viewDidLoad does not see the actual data, but something that soon will be outdated.
So, you need to inform the details view controller that new data is available, and refresh it's user interface. Something like
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
otherVC.getTheEventDocID { (eventdocid) in
if let id = eventdocid {
if segue.identifier == Constants.Segues.fromSearchToSchoolEventDetails {
let vc = segue.destination as! SchoolEventDetailsViewController
vc.selectedEventName = self.nameTheEvent
vc.selectedEventDate = self.dateTheEvent
vc.selectedEventCost = self.costTheEvent
vc.selectedEventGrade = self.gradeTheEvent
vc.selectedEventDocID = id
vc.updateUI()
}
}
}
}
and in the destination view controller:
class SchoolEventDetailsViewController ... {
override func viewDidLoad() {
super.viewDidLoad()
updateUI()
}
func updateUI () {
navigationItem.title = selectedEventName
// and so on
}
}
Ok so I decided to attempt a workaround and completely ditched the getTheEventDocID() method because it was just causing me stress. So I decided to ditch Firebase generated document IDS and just use 10 digit generated ids from a function I made. I also figured out how to add that exact same 10 digit id in the Algolia record by just storing the random 10 digit id in a variable and using it in both places. So now instead of using a query call to grab a Firebase generated document ID and have my app crash everytime I click a search result, I basically edited the Struct of the Algolia record and just added an eventDocID property that can be used with hits.hitSource(at: indexPath.row).eventDocID.
And now the same way I added the other fields to the vc by segue data transfer, I can now do the same thing with my document ID because everything is matching :).
I currently have multiple balls all on the same SKScene. I handle all touches and gestures within GameScene. Below is the code I use to detect which node was touched, which works.
What I am unsure of, since there are always some touchesMoved when using this on a real device, is there anyway possible for more than one node to receive a tap at the same time? If so I obviously would need to write my code differently.
#objc func tappedView(_ sender:UITapGestureRecognizer) {
if sender.state == .ended{
let point : CGPoint = sender.location(in: self.view)
var post = sender.location(in: sender.view)
post = self.convertPoint(fromView: post)
if let touchNode = self.atPoint(post) as? MyBall{
//the declaration below is just so I have somewhere to stop in the debugger
var x = 1
}
}
}
Use nodes(at:) to get multiple nodes at a point.
#objc func tappedView(_ sender:UITapGestureRecognizer) {
if sender.state == .ended{
let point : CGPoint = sender.location(in: self.view)
var post = sender.location(in: sender.view)
post = self.convertPoint(fromView: post)
for touchNode in self.nodes(at:post){
//the declaration below is just so I have somewhere to stop in the debugger
var x = 1
}
}
}
https://developer.apple.com/documentation/spritekit/sknode/1483072-nodes
In an ARKit project, when the user taps the screen, I want to get the rootNode of the element, that the user want to interact with.
Gesture & Hit test
func screenTapped(_ sender: UITapGestureRecognizer) {
let hitTestResult = sceneView.hitTest(touchLocation)
if let result = hitTestResult.first {
guard let rootNode = getRoot(for: result.node) else {return}
...
}
Recursive function to get the root node
func getRoot(for node: SCNNode) -> SCNNode? {
if let node = node.parent {
return getRoot(for: node)
}
else {
return node
}
}
But it seems odd to me that Swift doesn't offer something by default, while offering recursive methods for child nodes.
Is there an alternative/better approach to this?
Should I write this function as extension for SCNNode?
Isn't it equivalent to sceneView.scene.rootNode?