Execute handler function after tap on UIAlertAction of UIAlertController *SpriteKit* - swift

I am trying to get a UIAlterController to display (as soon as the scene is set up) with the following buttons:
Accept - won't do anything, just close the UIAlertController
Leave to Menu - should transition to the MenuScene
My code looks as follows (I put the alert controller inside an action so it first displays the scene and after 0.5 seconds it should start displaying the alert):
//make action
var actionBlock = SKAction.runBlock({
//alert controller
var alert = UIAlertController(title: "Welcome", message: "Are you sure you want to proceed?", preferredStyle: UIAlertControllerStyle.Alert)
//action with no handler
var acceptAction = UIAlertAction(title: "Yes", style: UIAlertActionStyle.Cancel, handler: nil)
//action thats supposed to transition to MenuScene (AFTER BEING TAPPED!!)
var leaveAction = UIAlertAction(title: "Back to Menu", style: UIAlertActionStyle.Destructive, handler: {_ in self.backToMenu()})
//add action to the alert controller
alert.addAction(acceptAction)
alert.addAction(leaveAction)
//add alert controller to view
self.view?.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
}
//wait for 0.5s action
var wait = SKAction.waitForDuration(0.5)
//run the action
self.runAction(SKAction.sequence([wait, actionBlock]))
and I placed it right into the DidMoveToView function. When I run the code, it immediately transitions back to the MenuScene without even waiting for the user to tap either one of the actions, and then the alert is still on the screen (but on the MenuScene).
I am really confused and I've been on this for several hours but I just can't figure out what my mistake is, and how to make it work..
Anybody help please? Any code that works would be really appreciated!!!

Related

Disable the auto dismissal of the alert when tapping on a button

func showAlert(...) {
let alertController = UIAlertController(...)
let add = UIAlertAction(title: "Add", style: .default) { (action) in
onAdd?()
}
alertController.addAction(ok)
...
}
I would like to keep the popup alert after click on Add. But it seem iOS will auto close the popup. How to make it work?
The only way to prevent a UIAlertController's alert from being dismissed when a button is tapped is to disable the button.
If you don't like that, don't use UIAlertController; use your own presented view controller. That's all a UIAlertController is, after all. So it's easy to write your own if you need to.

Long delay when presenting UIAlert

I'm having a problem with presenting an alert. I'm using UIAlertController. When the user presses the submit button, an alert needs to be presented immediately saying "Please Wait.." while the app does some operations. The alert is dismissed after the operations are complete. I'm having a long delay (several seconds) before the alert is presented even though the alert is supposed to be presented before i even start doing the operations. I tried dispatch async and tried using closures and nothing is working..still a delay of several seconds before alert is presented.
How do i make it so that the alert is presented immediately after the button the pressed?
override func viewDidLoad() {
super.viewDidLoad()
submitButton.addTarget(self,action:#selector(buttonAction),for:.touchUpInside)
}
#objc func buttonAction(){
waiting()
doOperations()
}
func waiting(){
DispatchQueue.main.async{
let alert = UIAlertController(title: nil, message: "Please wait...", preferredStyle: .alert)
let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
loadingIndicator.hidesWhenStopped = true
loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
loadingIndicator.startAnimating()
alert.view.addSubview(loadingIndicator)
self.present(alert, animated: false, completion: nil)
}
}
func doOperations(){
...
}
func dismissWaiting{
...
//dismiss alert
}
Just dispatch to main thread the alert.
Hey I resolved the problem just by putting the call to doOperations in the completion of the presentation of the alert. Like this:
self.present(alert, animated: false, completion: {self.doOperations()})
Now the alert is presented instantly after button press and everything works fine. Thanks guys for all the suggestions.

How to trigger a notification if label is dropped on wrong target?

In this drag and drop game (adapted from this tutorial), the player has to match the label to the right target. I have it set so that if the touch ends and the label's center is inside the target, it is removed from the screen and the game starts over.
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if label.name == "letters" {
if lettersBin.frame.contains(label.position) {
// remove it and create a new label
label.removeFromParent()
setupDragLabel()
However, I want to trigger a notification if the player drops the wrong label onto the bin, so:
else if label.name == "numbers" {
if lettersBin.contains(label.position) ...
What would I write to finish this? Do I make another label appear on the screen?
It might be easiest to use a UIAlertController. You can add the messages along with button actions to display that the user was wrong.
let converterAction = UIAlertController(title: "Your title here", message: "Your message to the user here", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default){ _ in
//Any custom action to happen here
}
converterAction.addAction(okAction)
present(converterAction, animated: true, completion: nil)
If you do not want to show an alert, you can do the following:
Add a label to the Storyboard and to your file.
Set that label to be hidden by default.
Programatically set label.isHidden = false and set it's text as you wish.
What I now labeled "label" can be anything though, you could even create a group of views and hide/show them all at once. Useful when wanting to show a Progress Indicator and a "Loading..." Label at the same time.

Swift : OverlayView that appears at the bottom of the screen

I'd like to create a very common effect as in the picture below :
Explanation : the effect I'd like to accomplish consists in a view that appears (slides in) at the bottom of the screen when user clicks a button : you can still see the screen behind this view, but it applies a "dark layer" (black view with let's say 60% opacity) on the top of it. When user clicks on Block or Report absue (as for this example) it would perform the respective actions. Now when user clicks on cancel, and also when he clicks anywhere on the "dark layer" it would bring him back to the screen.
What I tried : presenting a new view controller (but it would use more data than necessary), using a overlay layer but I even didnt get close to that effect that we're usually seeing on apps. I'm not sure but I'd say that the best way to get that effect is using views ?
Does anyone have an idea please ?
Thanks and have a good day,
J
You are looking for a native element called UIActionSheet. It has become the part of UIAlertController, and does exactly what you are looking for.
Here is a little help how to set up one:
// Create you actionsheet - preferredStyle: .actionSheet
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
// Create your actions - take a look at different style attributes
let reportAction = UIAlertAction(title: "Report abuse", style: .default) { (action) in
// observe it in the buttons block, what button has been pressed
print("didPress report abuse")
}
let blockAction = UIAlertAction(title: "Block", style: .destructive) { (action) in
print("didPress block")
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
print("didPress cancel")
}
// Add the actions to your actionSheet
actionSheet.addAction(reportAction)
actionSheet.addAction(blockAction)
actionSheet.addAction(cancelAction)
// Present the controller
self.present(actionSheet, animated: true, completion: nil)
It should produce the following output:

UIAlertAction dismisses the UIAlertController in its callback [duplicate]

This question already has answers here:
Prevent dismissal of UIAlertController
(5 answers)
Closed 8 years ago.
I am trying to use new UIAlertController introduced in iOS 8. Everything works great except the fact that UIAlertAction always end up dismissing the alert controller in its callback. Following is my code:
let alert = UIAlertController(title: "New Group", message: "Enter Group name", preferredStyle: UIAlertControllerStyle.Alert);
alert.addTextFieldWithConfigurationHandler({ [weak self] (nameField: UITextField!) in
nameField.becomeFirstResponder();
nameField.delegate = self;
return;
})
alert.addAction(UIAlertAction(title: "Done", style: .Default, handler: { action in
println("Done Entering");
}));
alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil));
self.presentViewController(alert, animated: true, completion: nil);
Now, when I click "Done" button, the controls enters the callback method and then alert is dismissed even though I don't have any statement to dismiss the alert. Is this behavior by default? If yes, how can I make sure that in some cases the alert stays on the screen (depending upon my conditions)? Am I missing something here?
I would really appreciate any help regarding this.
Yes, alert buttons always dismiss the alert and there is no way to configure them otherwise. If you want that behavior, you'll have to write your own alert. I've written SDCAlertView, which looks very much like a normal alert, but has some added functionality, including prevention of dismissing an alert when a button is tapped.
However, it doesn't use the UIAlertController API yet and it looks a little bit different (most users won't notice) on iOS 8 than the UIAlertController alert.
EDIT: It now has support for the UIAlertController-like API.