I have an alert controller that is supposed to appear after a user enters an incorrect amount of characters in the text fields. The alert controller does not appear at all. W
func usernameFieldCharacters() {
let alertController = UIAlertController(title: "Alert", message: "Five characters or more is required in all fields" , preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title: "OK", style: .Default) {
action -> Void in // Does not do anything
}
alertController.addAction(okAction) // adds the OK button to
// to alert controller
let allowedChars = 5 // character amount has to be equal or greater in each field
let usernameCount = theUsernameField.text?.characters.count
if usernameCount < allowedChars {
self.presentViewController(alertController, animated: true, completion: nil)
} else {
alertController.viewDidAppear(false)
}
}
the code works correct, when u move the alertcontroller to i.e. viewDidAppear-method:
class ViewController: UIViewController {
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
let allowedChars = 5 // character amount has to be equal or greater in each field
let usernameCount = theUsernameField.text?.characters.count
if usernameCount < allowedChars {
// Do any additional setup after loading the view, typically from a nib.
let alertController = UIAlertController(title: "Alert", message: "Five characters or more is required in all fields" , preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title: "OK", style: .Default) {
action -> Void in // Does not do anything
}
alertController.addAction(okAction) // adds the OK button to
// to alert controller
self.present(alertController, animated: true, completion: nil)
}
Above code works correctly.
And alertController.viewDidAppear(false) is needn't
Related
I want to create an alert for the first time an app is opened on the device but I am having some trouble with the code. this is what I have so far, any help would be appreciated
// alert first time app is opened
// making of alert
let alert = UIAlertController(title: "Navigation", message: "Tap Right Hand Side of Screen For Next Quote, Left Hand Side To Go Back", preferredStyle: UIAlertControllerStyle.alert)
//add ok button
alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default, handler: nil))
// detect if first launch
let launchedBefore = UserDefaults.standard.bool(forKey: "launcedBefore")
if launchedBefore {
}
else {
self.present(alert, animated: true, completion: nil)
UserDefaults.standard.set(true, forKey: "launchedBefore")
}
this is where I am trying to place the code
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
There is a typo in the launcedBefore key
Change it to:
let launchedBefore = UserDefaults.standard.bool(forKey: "launchedBefore")
If you try it in AppDelegate you have to present the alert in window?.rootViewController?
var alertController = UIAlertController(title: "Title", message: "Any message", preferredStyle: .ActionSheet)
var okAction = UIAlertAction(title: "Yes", style:
UIAlertActionStyle.Default) {
UIAlertAction in
NSLog("OK Pressed")
}
var cancelAction = UIAlertAction(title: "No", style: UIAlertActionStyle.Cancel) {
UIAlertAction in
NSLog("Cancel Pressed")
}
alertController.addAction(okAction)
alertController.addAction(cancelAction)
self.window?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
Check the value(true or false) you are getting in let launchedBefore = UserDefaults.standard.bool(forKey: "launcedBefore"). Show alert depends on the bool value.
I have a button that sends me on another View Controller. What I am trying is to display an alert on the next View Controller.
In the viewDidLoad() method of the new controller, create a new UIAlertController and display it like the following
let alertController = UIAlertController(title: "Default Style", message: "A standard alert.", preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
// ...
}
alertController.addAction(cancelAction)
let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in
// ...
}
alertController.addAction(OKAction)
self.presentViewController(alertController, animated: true) {
// ...
}
Note that this example was taken from the NSHipster website which offers nice articles about iOS. You can find the article about UIAlertController here. They also explain other stuff you can do with that class, like display an Action Sheet for example.
Swift 4
Create an extension of UIViewController with your function to display alert with required parameter arguments
extension UIViewController {
func displayalert(title:String, message:String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction((UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
alert.dismiss(animated: true, completion: nil)
})))
self.present(alert, animated: true, completion: nil)
}
}
Now call this function from your view controller:
class TestViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.displayalert(title: <String>, message: <String>)
}
}
so the way my app works is you tap on a cell , a var value gets modified (+1 for example). I've figured out how to get a UIalert to pop when my var reaches a certain value (10). But now everytime I update the var the alert pops. What i would like it to do is to pop when the var hits 10 and stop after that
Heres the code :
if (section1score >= 10){
let alertController: UIAlertController = UIAlertController(title: NSLocalizedString("Section 1 score is over 10", comment: ""),
message: " \(message1)",
preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "OK", style: .Default) {
action -> Void in }
alertController.addAction(OKAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
if (section2score >= 10){
let alertController: UIAlertController = UIAlertController(title: NSLocalizedString("Section 2 Score is over 10", comment: ""),
message: "\(message2)",
preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "OK", style: .Default) {
action -> Void in }
alertController.addAction(OKAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
Setup a Bool to check if the alert has been shown or not. Create the Bool globally and set it to false initially.
var showedAlert = false
func yourFunction() {
if section1score >= 10 && showedAlert == false {
// show alert
showedAlert = true
}
if section2score >= 10 && showedAlert == false {
// show alert
showedAlert = true
}
}
Comparison Operators
You could use a property to keep track of when you show the alert so that once you show it, you won't show it again.
var hasShownAlert: Bool = false
if (section1score >= 10 && !hasShownAlert){
let alertController: UIAlertController = UIAlertController(title: NSLocalizedString("Section 1 score is over 10", comment: ""),
message: " \(message1)",
preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "OK", style: .Default) {
action -> Void in }
alertController.addAction(OKAction)
self.presentViewController(alertController, animated: true, completion: nil)
hasShownAlert = true
}
if (section2score >= 10 && !hasShownAlert){
let alertController: UIAlertController = UIAlertController(title: NSLocalizedString("Section 2 Score is over 10", comment: ""),
message: "\(message2)",
preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "OK", style: .Default) {
action -> Void in }
alertController.addAction(OKAction)
self.presentViewController(alertController, animated: true, completion: nil)
hasShownAlert = true
}
I'd like to get an alert to show when 3 specific value is reached,
here's my code
func SectionAlert () {
if (section1score >= 10){
let alertController: UIAlertController = UIAlertController(title: "Alert", message: "\((section1score as NSNumber).stringValue)", preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "OK", style: .Default) {
action -> Void in
}
alertController.addAction(OKAction)
self.presentViewController(alertController, animated: true, completion: nil)
} else if (section2score >= 10){
} else if (section3score >= 10){
}
}
My App updates each sections value when a cell is tapped in the TableView
My code does not show any error but it does not give me my alert when my value reaches 10 and over
any ideas??
thanks !
I have no idea how to change screens programatically. I have an alert view and I want to be able to change screen when the user presses the "Ok" button. How do I do this?
Here is my new code:
func showAlertController(){
let tilte = "My Medication"
let message = NSLocalizedString("Go through Medication guide?", comment: "")
let cancelButtonTitle = NSLocalizedString("Dismiss", comment: "")
let otherButtonTitle = NSLocalizedString("Ok", comment: "")
let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: cancelButtonTitle, style: .Cancel){ action in
NSLog("User said no")}
let otherAction = UIAlertAction(title: otherButtonTitle, style: .Default){action in
// I know I need to put something in here.
let appointment = Appointment()
self.presentViewController(appointment, animated:true, completion:nil)
}
alertController.addAction(cancelAction)
alertController.addAction(otherAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
However now I get a bug saying:
Thread 1: EXC_BAD_INSTRUCTION(code=EXC_l1386_INVOP,subcode=0x0)
Add presentViewController inside UIAlertAction closure for "Ok" button, it means that the button is pressed and so you do your stuffs for the button being pressed inside the block.
class MainViewController: UIViewController {
...
...
func showAlertController(){
let tilte = "My Medication"
let message = NSLocalizedString("Go through Medication guide?", comment: "")
let cancelButtonTitle = NSLocalizedString("Dismiss", comment: "")
let otherButtonTitle = NSLocalizedString("Ok", comment: "")
let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: cancelButtonTitle, style: .Cancel){ action in
NSLog("User said no")}
let otherAction = UIAlertAction(title: otherButtonTitle, style: .Default){action in
// I know I need to put something in here.
let appointmentViewController = AppointmentViewController()
self.presentViewController(appointmentViewController, animated:true, completion:nil)
}
alertController.addAction(cancelAction)
alertController.addAction(otherAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
...
...
}
class AppointmentViewController: UIViewController {
}