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

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.

Related

Swift Trigger click in textfield

In my app I'm using some pickerviews that appear when I click on a textfield, that works fine!
I wanted to do the same when I click on a Left Bar Button Item. I can't do it with the button because buttons doesn't have inputView property, needed for associate the pickerview to the button (in this case). So I want to have a hidden textfield that is programmatically clicked when I click on the button (when it's clicked it show the pickerview and change the button name, that's all done)
Is that possible?
The best I can do right know is something like this
txtFantasma.perform(
#selector(becomeFirstResponder),
with: nil,
afterDelay: 0.1
)
It works fine, but just work at the first time.
EDIT1:
I've tried to make it with buttons... The popover is showing, now I wanted to click in one button and dismiss the popover and pass data to the main viewcontroller.
class ViewPopup:UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func btTituloAsc(_ sender: UIButton) {
let next = self.storyboard?.instantiateViewController(withIdentifier: "mainview") as! ViewController
next.ordenacao = "TituloAsc"
self.present(next, animated: true,completion:nil)
}
}
This works, but the main controller is showed without the Navigation Bar! How can I do the same but show de Navigation Bar?
The simplistic way to do it, is just display the pickerView as a popover once the user taps the button and then change the buttons title accordingly.

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:

Present Pop-up UIAlertController from within UITableViewCell Without UITableView

In my iOS app I have a UIViewController ViewController that presents a view that is partly made up of a TableView. The UITableViewCells include a UISegmentControl. When the UISegmentControl's valueHasChanged action is called, I want to present a pop-up. This is the code called within the action:
//Check that the text field isn't empty:
if self.TextField.text == "" {
self.Status.selectedSegmentIndex = 0
let viewController = ViewController()
viewController.alertStatusChangedForEmptyTextField(self)
}
This is the pop-up I have created within the ViewController class that checks if an adjacent text field in the same UITableViewCell is empty or not:
//Alert function that handles when toggle is set even though there is no content.
func alertStatusChangedForEmptyTextField(sender: AnyObject) {
let alert = UIAlertController(title: "Text field has no contents", message: "Please enter a text.", preferredStyle: UIAlertControllerStyle.Alert);
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Cancel, handler: nil));
showViewController(alert, sender: self);
}
I get an error that pops up in the viewDidLoad function of the ViewController, but originates from the showViewController method call within this function (I know because I've tried commenting out only this last line and then everything works fine).
How can I call a pop-up(ViewController) from within a UITableViewCell? Happy to hear suggestions that involve completely different approaches (Creating a custom UITableView subclass? A function that is defined within the TableViewCell class?)
Also, I don't have a root view controller.
check the value of storyboard, there is a good chance that you will find it nil;
storyboard.instantiateViewControllerWithIdentifier("PopMenuViewController") as! PopMenuViewController
I am trying to figure out how to present the pop up from custom tableview cell element. If you figured it out than do share the method :)

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

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!!!

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.