Swift Add arrow and sourceView to UIAlertController - ios

I have a UIAlertController and I am trying to add an arrow to it and change the sourceView to self.button so it does not appear at the bottom.
This is what I got so far:
let dropdown = UIAlertController(title: "Select Room", message: nil, preferredStyle: .ActionSheet)
let kitchen = UIAlertAction(title: "Kitchen", style: .Default) { (action) in
}
dropdown.addAction(kitchen)
let livingRoom = UIAlertAction(title: "Living Room", style: .Default) { (action) in
}
dropdown.addAction(livingRoom)
let bedroom = UIAlertAction(title: "Bedroom", style: .Default) { (action) in
}
dropdown.addAction(bedroom)
let bathroom = UIAlertAction(title: "Bathroom", style: .Default) { (action) in
}
dropdown.addAction(bathroom)
let cancel = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
}
dropdown.addAction(cancel)
dropdown.modalPresentationStyle = .Popover
dropdown.popoverPresentationController?.sourceRect = self.dropdownButton.frame
dropdown.popoverPresentationController?.sourceView = self.dropdownButton
dropdown.popoverPresentationController?.permittedArrowDirections = .Up
self.presentViewController(dropdown, animated: true, completion: nil)
But the arrow does not appear and the UIAlertController still appears at the bottom. What Am I doing wrong?

popoverPresentationController (and it's settings) are meant to be used by a UIViewController who's presentationStyle is UIModalPresentationPopover.
While UIAlertController is a subclass of UIViewController, it is special and uses UIModalPresentationCustom as it's presentationStyle. Attempts to set a new presentationStyle are ignored.
I don't think what you are trying to do is an intended use for UIAlertController. You best bet is to make your own view controller to achieve this purpose.

Related

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)

iOS SlideUp menu for phone numbers

Is there an solution on iOS where I could create an phone number picker menu like this? Does Apple support this view automatically or should I implement this alone?
This is called UIAlertController's ActionSheet style in iOS. You can show ActionSheet with below code:
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let action1 = UIAlertAction(title: "Phone", style: .default) { (action) in
// Add your code here when Phone action Tapped
}
let action2 = UIAlertAction(title: "Mobile", style: .default) { (action) in
// Add your code here when Mobile action Tapped
}
let action3 = UIAlertAction(title: "iPhone", style: .default) { (action) in
// Add your code here when iPhone action Tapped
}
let action4 = UIAlertAction(title: "Private", style: .default) { (action) in
// Add your code here when Private action Tapped
}
let cancel = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
// Add your code here when Cancel action Tapped
}
// To set image in ActionButton
action1.setValue(#imageLiteral(resourceName: "check").withRenderingMode(.alwaysOriginal), forKey: "image")
// Add Actions to AlertController
actionSheet.addAction(action1)
actionSheet.addAction(action2)
actionSheet.addAction(action3)
actionSheet.addAction(action4)
actionSheet.addAction(cancel)
// Present UIAlertController
present(actionSheet, animated: true, completion: nil)
You can create your own menu as per your requirement using UIActionSheet
let actionSheet = UIActionSheet(title: "Choose Option", delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: nil, otherButtonTitles: "Save", "Delete")
actionSheet.showInView(self.view)

How to change UIAlertAction font or show UIAlertActionStyle.cancel style ActionButton on second position?

I don't want to change UILabel.appearance as it will apply to all labels.
How I can show UIAlertController that will look like below image?
Need to show bold button in the second position.
By default, when I am setting UIAlertActionStyle.cancel it shows it on the Confirm button in the first position.
It looks like this now:
I did some research but I didn't find any solution.
Swift 4.2/Swift 5
You need to set preferred action method,
//Create alertController
let alert = UIAlertController(title: "title", message: "message", preferredStyle: .alert)
//Create and add the Confirm action
let confirmAction = UIAlertAction(title: "Confirm", style: .default, handler: { (action) -> Void in
//Do Something here...
})
alert.addAction(confirmAction)
//Create and add the Cancel action
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: { (action) -> Void in
//Do Something here...
})
alert.addAction(cancelAction)
// Set Preferred Action method
alert.preferredAction = confirmAction
self.present(alert, animated: true, completion: nil)
The output will be,
You need to add them to the alertView in the right order! If you want the order to change just add the confirm button second!
alert.addAction(cancelAction)
alert.addAction(confirmAction)
Instead of:
alert.addAction(confirmAction)
alert.addAction(cancelAction)
Sure, you can. Try this
let alertView = UIAlertController(
title: "Home",
message: "Are you sure for delete Reminder",
preferredStyle: .alert)
alertView.view.tintColor = .green
let confirmAction = UIAlertAction(title: "Confirm", style: .cancel) { (_) in
// Do confirm
}
alertView.addAction(confirmAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .default) { (_) in
// Do cancel
}
alertView.addAction(cancelAction)
self.present(alertView, animated: true, completion: nil)

Is there a way to call an alert controller from inside a current alert controller (or a textfield in an action sheet)? Xcode 8, Swift 3, IOS

Please help! Im a huge beginner. Im trying to get a picker view and a textfield into one alert controller. Apparently I can't add a picker view into an alert controller since IOS 8. Instead I need to use an action sheet with multiple actions. Why can't I use a default alert style? Because I need more than 2 actions and the default alert style apparently only permits a max of 2 actions. So, I need to use an action sheet. Only problem is I can't seem to find a way to put an editable textfield into an action sheet, to have multiple action options - instead of an editable textfield in my default alert style, with only two action options.
This is what I have so far, for my editable textfield in my default alert style, with only two options (OK and cancel):
#IBOutlet weak var TEXTTESTLabel: UILabel!
#IBAction func TEXTESTTapped(_ sender: UIButton) {
print("TEXTTEST Button Tapped")
openTEXTTESTAlert()
}
func openTEXTTESTAlert() {
//Create Alert Controller
let alert9 = UIAlertController (title: "Test Your Text:", message: nil, preferredStyle: UIAlertControllerStyle.alert)
//Create Cancel Action
let cancel9 = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil)
alert9.addAction(cancel9)
//Create OK Action
let ok9 = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { (action: UIAlertAction) in print("OK")
let textfield = alert9.textFields?[0]
print(textfield?.text!)
self.TEXTTESTLabel.text = textfield?.text!
}
alert9.addAction(ok9)
//Add Text Field
alert9.addTextField { (textfield: UITextField) in
textfield.placeholder = "TEXTTEST"
}
//Present Alert Controller
self.present(alert9, animated:true, completion: nil)
}
//////////////////////////////////////////////////////////////////////////////////
Since this, I've gotten help to create two separate alert controllers but it looks and feels messy. It's set up so if I click button 2 the default alert style with a textfield will pop and insert text into a label. But, if i click button 1, AND THEN I click button 2, it will show an action sheet with 4 actions that put a word into that same label. This is what I have for that method:
#IBOutlet weak var TextLabel: UILabel!
var userWantsToShowAlert = false
#IBAction func yourNewButtonTapped(_ sender: UIButton) {
userWantsToShowAlert = !userWantsToShowAlert
print("User wants to show alert? \(userWantsToShowAlert)")
//This is userWantsToShowAlert is false, it will change it to true. And if it is true, it will change it to false.
}
#IBAction func TextButtonTapped(_ sender: UIButton) {
print("Text Button Tapped")
if(userWantsToShowAlert){
openTextAlert()
}else{
openActionSheetAlert()
}
}
func openTextAlert() {
//Create Alert Controller
let alert9 = UIAlertController (title: "Whatever Text Your Heart Desires:", message: nil, preferredStyle: UIAlertControllerStyle.alert)
//Create Cancel Action
let cancel9 = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil)
alert9.addAction(cancel9)
//Create OK Action
let ok9 = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { (action: UIAlertAction) in print("OK")
let textfield = alert9.textFields?[0]
print(textfield?.text!)
self.TextLabel.text = textfield?.text!
}
alert9.addAction(ok9)
//Add Text Field
alert9.addTextField { (textfield: UITextField) in
textfield.placeholder = "Whatever text you want to enter"
}
//Present Alert Controller
self.present(alert9, animated:true, completion: nil)
}
func openActionSheetAlert(){
let alert9 = UIAlertController (title: "Whatever Text Your Heart Desires:", message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)
//Create Cancel Action
let cancel9 = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil)
alert9.addAction(cancel9)
let bt1 = UIAlertAction(title: "1", style: UIAlertActionStyle.default){
(action) in self.TextLabel.text = "Word 1"}
alert9.addAction(bt1)
let bt2 = UIAlertAction(title: "2", style: UIAlertActionStyle.default){
(action) in self.TextLabel.text = "Word 2"}
alert9.addAction(bt2)
let bt3 = UIAlertAction(title: "3", style: UIAlertActionStyle.default){
(action) in self.TextLabel.text = "Word 3"}
alert9.addAction(bt3)
let bt4 = UIAlertAction(title: "4", style: UIAlertActionStyle.default){
(action) in self.TextLabel.text = "Word 4"}
alert9.addAction(bt4)
alert9.popoverPresentationController?.sourceView = self.view
alert9.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.size.width / 2.0, y: self.view.bounds.size.height / 4.0, width: 1.0, height: 1.0)
self.present(alert9, animated:true, completion: nil)
}
I find that this method is confusing to the application user. If anyone has a different method to get a textfield into an action sheet (or even a separate alert controller imbedded in a current alert controller), I'd appreciate it VERY much!
Thank you :)
For Action sheet you can use as
func showActionSheet()
{
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)
actionSheet.addAction(UIAlertAction(title: "1:", style: UIAlertActionStyle.default, handler: { (alert:UIAlertAction!) -> Void in
)
self.action1()
}))
actionSheet.addAction(UIAlertAction(title: "2", style: UIAlertActionStyle.default, handler: { (alert:UIAlertAction!) -> Void in
self.action2()
}))
actionSheet.addAction(UIAlertAction(title: "3", style: UIAlertActionStyle.default, handler: { (alert:UIAlertAction!) -> Void in
// self.action3()
}))
actionSheet.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil))
self.present(actionSheet, animated: true, completion: nil)
}
// showActionSheet()
I found the solution! I was wrong about having only 2 actions in the default alert style. How to scroll through actions in alert controller? Xcode 8, Swift 3, IOS

How to make a UIAlert button press advance to next Storyboard?

I'm quite new to Swift (and coding in general) and to this website.
I'm having a little bit of an issue now. In my app, I have an alert when a timer reaches 0. In that alert, there are 2 buttons. One says "Share" and the other says "Continue". I want to make it such that when a user taps "Continue", the next Storyboard will be shown to them. As of now, whatever button I press will dismiss the alert, but stay on the same Storyboard. (It also prints to the console which button I pressed, but of course that's just for me to see).
How do I go about doing this? Here is my code, in case anyone wants to know.
let alert = UIAlertController(title: "Time's Up!", message: "What would you like to do now?", preferredStyle: .Alert)
let firstAction = UIAlertAction(title: "Continue", style: .Default) { (alert: UIAlertAction!) -> Void in
NSLog("You pressed button one")
}
let secondAction = UIAlertAction(title: "Share", style: .Default) { (alert: UIAlertAction!) -> Void in
NSLog("You pressed button two")
}
alert.addAction(firstAction)
alert.addAction(secondAction)
presentViewController(alert, animated: true, completion:nil)
Try with this:
// Create the alert controller
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")
let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as UIViewController
self.presentViewController(vc, animated: true, completion: nil)
}
var cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) {
UIAlertAction in
NSLog("Cancel Pressed")
//do whatever you want here
}
// Add the actions
alertController.addAction(okAction)
alertController.addAction(cancelAction)
// Present the controller
self.presentViewController(alertController, animated: true, completion: nil)
If you haven't already set your ViewControllerIdentifier:
You need to set the Identifier value that you can find on the right column
Here is the code that might help you. When you create a AlertController with actions, you can provide the actions and style of the button when you define them. In code below action is in the closure (Block) and style is defined as .Default or .Cancel
let alert = UIAlertController(title: "Time's Up!", message: "What would you like to do now?", preferredStyle: .Alert)
let firstAction = UIAlertAction(title: "Continue", style: .Default) { (alert: UIAlertAction!) -> Void in
// Action when you press button goes here
print("Here you show next storyboard item.")
// Code to push new ViewController. For example :
self.navigationController?.pushViewController(newVCInstance, animated: true)
}
let secondAction = UIAlertAction(title: "Share", style: .Default) { (alert: UIAlertAction!) -> Void in
print("Here you share things.")
// Code to share things.
}
let thirdAction = UIAlertAction(title: "Cancel", style: . Cancel) { (alert: UIAlertAction!) -> Void in
print("Just dismissing the Alert Controller.")
}
alert.addAction(firstAction)
alert.addAction(secondAction)
alert.addAction(thirdAction)
presentViewController(alert, animated: true, completion:nil)

Resources