How to show UIAlertController top of the presenting cotroller - ios

I have the top view controller is presenting, and I want to show alert controller but it not display. Have any way to do it?
var appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)

(Objective-C)
UIAlertController *alertVC = [UIAlertController
alertControllerWithTitle:#"myTitle" message:#"myMessage"
preferredStyle:UIAlertControllerStyleAlert];
[alertVC addAction:[UIAlertAction
actionWithTitle:#"Ok"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction* action) {
[alertVC dismissViewControllerAnimated:YES completion:nil];
}
]];
[self presentViewController:alertVC animated:YES completion:nil];

Check this:
func showAlert() {
let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
let yesAction = UIAlertAction(title: "YES", style: UIAlertActionStyle.Default) { (action: UIAlertAction!) -> Void in
print("YES tapped")
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (action: UIAlertAction!) -> Void in
print("Cancel tapped")
}
alert.addAction(yesAction)
alert.addAction(cancelAction)
var appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
}
Please also check and make sure that your rootViewController is not nil.

Finally I can solved my problem. It so easy, just input the controller we are standing on into the show alertcontroller function below:
func showAlerControler(title: String?, message: String, confirmButtonText: String?, cancelButtonText: String?, atController: UIViewController){
var alert: UIAlertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
if let confirmStr = confirmButtonText{
alert.addAction(UIAlertAction(title: confirmStr, style: .Default, handler: { (ACTION) -> Void in
}))
}
if let cancelStr = cancelButtonText{
alert.addAction(UIAlertAction(title: cancelStr, style: .Default, handler: { (ACTION) -> Void in
}))
}
atController.presentViewController(alert, animated: true, completion: nil)
}
Don't use appDelegate.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
And you can standing on any controller to call showAlerControler and input atController is self

Related

ViewController reloads on presenting alert controller swift

I am working on project. i have added a simple alertController on clicking signup button. when i click on the button my viewcontroller reloads and then it shows that alertController. It is happening on iOS 13 and swift 5 or above
let alert = UIAlertController(title: "Your title", message: "Your message", preferredStyle: .alert)
let ok = UIAlertAction(title: "OK", style: .default, handler: { action in
})
alert.addAction(ok)
let cancel = UIAlertAction(title: "Cancel", style: .default, handler: { action in
})
alert.addAction(cancel)
DispatchQueue.main.async(execute: {
self.present(alert, animated: true)
})
i resolved my problem by these lines of code
func alert(message: String, title: String = "") {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
let alertAction = UIAlertAction(title: "OK", style: .default) { (action) in
print("Action")
}
alertController.addAction(alertAction)
(UIApplication.shared.delegate as! AppDelegate).alertWindow.isHidden = true
self.presentViewController(alertController: alertController)
// self.present(alertController, animated: true, completion: nil)
}
func presentViewController(alertController: UIAlertController, completion: (() -> Void)? = nil) {
if var topController = UIApplication.shared.keyWindow?.rootViewController {
while let presentedViewController = topController.presentedViewController {
topController = presentedViewController
}
DispatchQueue.main.async {
topController.present(alertController, animated: true, completion: completion)
}
}
}

In swift, How to keep a UIViewController().view at the top?like UIAlertController

I'm using CustomAlertViewController as an Alert on UIViewController. Current code snippet to present CustomAlertViewController -
CustomAlertViewController: UIViewController {}
self.present(CustomAlertViewController(), animated: true, completion: {})
But I want to addCustomAlertViewControlleron top of the view hierarchy.
Any suggestion how to achieve this?
extension UIViewController {
func showAlertConfirm(errorMessage: String, operation: #escaping ()->()) {
let alert = UIAlertController(title: "Confirmation", message: errorMessage,
preferredStyle: .alert)
let confirmationAction = UIAlertAction(title: "OK", style: .default) { (action)
in
operation()
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler:nil)
alert.addAction(confirmationAction)
alert.addAction(cancelAction)
present(alert, animated: true, completion: nil)
}
}
if you call this function
showAlertConfirm(errorMessage: "Test", operation:
requestData()
)
Probably you want to add your UIViewCotroller’s view as a subview of key window. Check this answer: https://stackoverflow.com/a/38540271/5779168

Bug with UIAlert and View Controller

I have very strange bug with default Alert in my iOS app for iPad.
On it I have three buttons: one for camera, second for photo gallery and third is dismiss button. Alert is used to pick photos. Issue I have is sometimes, when I click on dismiss button on that alert, this type of code get's executed:
let loginVC = self.storyboard?.instantiateViewController(withIdentifier: "LoginViewController") as! LoginPageViewController
I'm not sure if this is exactly the code that get's executed, but it performs logout action and users get redirected to login screen.
Here's my code for Alert
func showPopover(uiView: UIView) {
let alertController = UIAlertController(title: nil, message: "Choose Photos", preferredStyle: .actionSheet)
let defaultAction = UIAlertAction(title: "Camera", style: .default, handler: { (alert: UIAlertAction!) -> Void in
self.view?.pickPhotoFromCamera()
})
let galleryAction = UIAlertAction(title: "Gallery", style: .default, handler: { (alert: UIAlertAction!) -> Void in
self.view?.pickPhotoFromLibrary()
})
let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: { (alert: UIAlertAction!) -> Void in
(self.view as? UIViewController)?.dismiss(animated: true, completion: nil)
})
alertController.addAction(defaultAction)
alertController.addAction(galleryAction)
alertController.addAction(cancelAction)
if let popoverController = alertController.popoverPresentationController {
popoverController.sourceView = uiView
popoverController.sourceRect = CGRect(x: uiView.bounds.midX, y: uiView.bounds.midY, width: 0, height: 0)
popoverController.permittedArrowDirections = []
}
(view as? UIViewController)?.tabBarController!.present(alertController, animated: true, completion: nil)
}
As you can see, my code in alert doesn't have any actions for logout, but sometimes such thing happens. Maybe someone had similar issues? What could be the reason for that?
If you need more information - let me know. Thanks in advance for your help!
First you don't need to write any code for a .cancel type action button to dismiss the presented alert view controller. Secondly, you can just use view to present the alert controller, no need to ask it's parent (tabBarController) to do it. Your code in the .cancel button dismiss the Login controller itself.
import UIKit
class LoginViewController: UIViewController {
func showPopover(uiView: UIView) {
let alertController = UIAlertController(title: nil, message: "Choose Photos", preferredStyle: .actionSheet)
let defaultAction = UIAlertAction(title: "Camera", style: .default, handler: { (alert: UIAlertAction!) -> Void in
self.pickPhotoFromCamera()
})
let galleryAction = UIAlertAction(title: "Gallery", style: .default, handler: { (alert: UIAlertAction!) -> Void in
self.pickPhotoFromLibrary()
})
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
alertController.addAction(galleryAction)
alertController.addAction(cancelAction)
if let popoverController = alertController.popoverPresentationController {
popoverController.sourceView = uiView
popoverController.sourceRect = uiView.bounds
}
present(alertController, animated: true, completion: nil)
}
private func pickPhotoFromCamera() { }
private func pickPhotoFromLibrary() { }
}
try
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
The issue in cancelAction which dismiss the current view controller. set its style to cancel with no handler.
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)

'UIAlertView' was deprecated in iOS 9.0. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead

I have see more so answers , but nothing helped.Here is my older alert and action for that
override func viewWillAppear(animated: Bool) {
if Reachability.isConnectedToNetwork() == true {
print("internet connection ok")
} else
{
print("internet not ok")
let alertView: UIAlertView = UIAlertView(title: "Alert ", message: "connect to internet", delegate: self, cancelButtonTitle: "settings", otherButtonTitles: "cancel")
alertView.show()
return
}
}
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int)
{
if buttonIndex == 0 {
//This will open ios devices wifi settings
UIApplication.sharedApplication().openURL(NSURL(string: "prefs:root")!)
}
else if buttonIndex == 1
{
//TODO for cancel
exit(0)
}
}
In that i am getting warning :
'UIAlertView' was deprecated in iOS 9.0. Use UIAlertController with a
preferredStyle of UIAlertControllerStyleAlert instead
I tried :
let alert = UIAlertController(title: "Alert", message: "My Alert for test", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: { (action:UIAlertAction!) in
print("you have pressed the Cancel button")
}))
self.presentViewController(alert, animated: true, completion: nil)
But to add two button and add the index path of button press method link my older code,I am not able to do that. Nothing action happening fro my uialert button,
Please help me out,How can i remove that warnings and recode my Uialert with my two button action.
I am new to swift.Your help will be useful.Thanks!
See this Code Destructive and OK buttons in UIAlertController:
let alertController = UIAlertController(title: "Destructive", message: "Simple alertView demo with Destructive and Ok.", preferredStyle: UIAlertControllerStyle.alert) //Replace UIAlertControllerStyle.Alert by UIAlertControllerStyle.alert
let DestructiveAction = UIAlertAction(title: "Destructive", style: UIAlertActionStyle.Destructive) {
(result : UIAlertAction) -> Void in
print("Destructive")
}
// Replace UIAlertActionStyle.Default by UIAlertActionStyle.default
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
(result : UIAlertAction) -> Void in
print("OK")
}
alertController.addAction(DestructiveAction)
alertController.addAction(okAction)
self.presentViewController(alertController, animated: true, completion: nil)
Swift 3:
let alertController = UIAlertController(title: "Destructive", message: "Simple alertView demo with Destructive and Ok.", preferredStyle: UIAlertControllerStyle.alert) //Replace UIAlertControllerStyle.Alert by UIAlertControllerStyle.alert
let DestructiveAction = UIAlertAction(title: "Destructive", style: UIAlertActionStyle.destructive) {
(result : UIAlertAction) -> Void in
print("Destructive")
}
// Replace UIAlertActionStyle.Default by UIAlertActionStyle.default
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
(result : UIAlertAction) -> Void in
print("OK")
}
alertController.addAction(DestructiveAction)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
See Alert With Destructive and OK Button:
In Swift 3, you can write this:
let alertController = UIAlertController(title: "Title", message: "This is my text", preferredStyle: UIAlertControllerStyle.alert)
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default)
{
(result : UIAlertAction) -> Void in
print("You pressed OK")
}
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
For a basic alert message I like using an extension on UIViewController:
extension UIViewController {
func alertMessageOk(title: String, message: String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
let action = UIAlertAction(title: "Ok", style: .default, handler: nil)
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
}
Usage:
self.alertMessageOk(title: "Test Title", message: "Test message")
Swift 3
// Create message
let alertController = UIAlertController(title: "Title",
message: "Message",
preferredStyle: .actionSheet)
// Clear Action
let clearAction = UIAlertAction(title: "Clear",
style: .destructive,
handler: { (action:UIAlertAction!) in
print ("clear")
})
alertController.addAction(clearAction)
// Cancel
let cancelAction = UIAlertAction(title: "Cancel",
style: .cancel,
handler: { (action:UIAlertAction!) in
print ("Cancel")
})
alertController.addAction(cancelAction)
// Present Alert
self.present(alertController,
animated: true,
completion:nil)
UIAlertController *AC = UIAlertController.alertControllerWithTitle("Title",message:"Message",preferredStyle:UIAlertControllerStyleAlert)
UIAlertAction *ActionOne = [UIAlertAction actionWithTitle:"ActionOne" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog("ActionOne")
} ]
UIAlertAction *ActionTwo = [UIAlertAction actionWithTitle:"ActionTwo" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog("ActionTwo")
} ]
AC.addAction(ActionOne)
AC.addAction(ActionTwo)
self.presentViewController(AC,animated:true,completion:nil)
Try this code.
Ex
let actionSheetController: UIAlertController = UIAlertController(title: "Are you sure?", message: "", preferredStyle: .Alert)
let cancelAction: UIAlertAction = UIAlertAction(title: "NO", style: .Cancel) { action -> Void in
//Do your task
}
actionSheetController.addAction(cancelAction)
let nextAction: UIAlertAction = UIAlertAction(title: "YES", style: .Default) { action -> Void in
//Do your task //NSUserDefaults.standardUserDefaults().removePersistentDomainForName(NSBundle.mainBundle().bundleIdentifier!)
// NSUserDefaults.standardUserDefaults().synchronize()
}
actionSheetController.addAction(nextAction)
self.presentViewController(actionSheetController, animated: true, completion: nil)
I have get this answer from this link i think it is useful to you
How to add an action to a UIAlertView button using Swift iOS
var alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert)
// Create the actions
var okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
UIAlertAction in
NSLog("OK Pressed")
}
var cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) {
UIAlertAction in
NSLog("Cancel Pressed")
}
// Add the actions
alertController.addAction(okAction)
alertController.addAction(cancelAction)
// Present the controller
self.presentViewController(alertController, animated: true, completion: nil)
This code will help
let alertController = UIAlertController(title: "Default AlertController", message: "A standard alert", preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel)
{
(action:UIAlertAction!) in
print("you have pressed the Cancel button");
}
alertController.addAction(cancelAction)
let OKAction = UIAlertAction(title: "OK", style: .Default)
{
(action:UIAlertAction!) in
print("you have pressed OK button");
}
alertController.addAction(OKAction)
self.presentViewController(alertController, animated: true, completion:nil)

UIActionSheet crashes on iPad swift

I have the following button action in a toolbar:
#IBAction func share(sender: AnyObject) {
let modifiedURL1 = "http://www.declassifiedandratified.com/search.html?q=\(self.searchBar.text)"
let modifiedURL = modifiedURL1.stringByReplacingOccurrencesOfString(" ", withString: "%20", options: NSStringCompareOptions.LiteralSearch, range: nil)
let alert = UIAlertController(title: "Share", message: "Share your findings", preferredStyle: UIAlertControllerStyle.ActionSheet)
let twBtn = UIAlertAction(title: "Twitter", style: UIAlertActionStyle.Default) { (alert) -> Void in
if SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter){
var twitterSheet:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
twitterSheet.setInitialText("Look what I found on Declassified and Ratified: \(modifiedURL)")
self.presentViewController(twitterSheet, animated: true, completion: nil)
} else {
var alert = UIAlertController(title: "Accounts", message: "Please login to a Twitter account to share.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
let fbBtn = UIAlertAction(title: "Facebook", style: UIAlertActionStyle.Default) { (alert) -> Void in
if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook){
var facebookSheet:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
facebookSheet.setInitialText("Look what I found on Declassified and Ratified: \(modifiedURL)")
self.presentViewController(facebookSheet, animated: true, completion: nil)
} else {
var alert = UIAlertController(title: "Accounts", message: "Please login to a Facebook account to share.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
let safariBtn = UIAlertAction(title: "Open in Safari", style: UIAlertActionStyle.Default) { (alert) -> Void in
let URL = NSURL(string: modifiedURL)
UIApplication.sharedApplication().openURL(URL!)
}
let cancelButton = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (alert) -> Void in
println("Cancel Pressed")
}
let textBtn = UIAlertAction(title: "Message", style: UIAlertActionStyle.Default) { (alert) -> Void in
if (MFMessageComposeViewController.canSendText()) {
let controller = MFMessageComposeViewController()
controller.body = "Look what I found on Declassified and Ratified: \(modifiedURL)"
controller.messageComposeDelegate = self
self.presentViewController(controller, animated: true, completion: nil)
}
}
alert.addAction(twBtn)
alert.addAction(fbBtn)
alert.addAction(safariBtn)
alert.addAction(textBtn)
alert.addAction(cancelButton)
self.presentViewController(alert, animated: true, completion: nil)
}
However, this code crashes when called on an iPad with a sigbart crash (that is all I can see in the console). I see this is a common problem but the other solutions have not worked for me. I even set the version to the latest iOS and that didn't fix it. Can someone explain?
On an iPad, an action sheet is a popover. Therefore you must give its UIPopoverPresentationController a sourceView and sourceRect, or barButtonItem, so that it has something to attach its arrow to.

Resources