Increment Tab Bar Badge W/ Uialertaction Swift

Increment tab bar badge w/ UIAlertAction swift?

You can try to access the badgeValue and convert it to Integer as follow:

Swift 2

if let badgeValue = tabBarController?.tabBar.items?[1].badgeValue,
nextValue = Int(badgeValue)?.successor() {
tabBarController?.tabBar.items?[1].badgeValue = String(nextValue)
} else {
tabBarController?.tabBar.items?[1].badgeValue = "1"
}

Swift 3 or later

    if let badgeValue = tabBarController?.tabBar.items?[1].badgeValue,
let value = Int(badgeValue) {
tabBarController?.tabBar.items?[1].badgeValue = String(value + 1)
} else {
tabBarController?.tabBar.items?[1].badgeValue = "1"
}

To delete the badge just assign nil to the badgeValue overriding viewDidAppear method:

override func viewDidAppear(animated: Bool) {
tabBarController?.tabBar.items?[1].badgeValue = nil
}

Swift 3 - Popup when certain amount of actions are made

First, create an integer with an initial value of zero. Every time the button is pressed, increment one to this integer. At the end of the function, check whether this integer is equal to three and if so, present the alert.

var counter = 0

@IBAction func buttonPress(_ sender: Any) {
counter += 1

if counter == 3 {
counter = 0
let alert = UIAlertController(title: "Pressed 3 Times", message: "", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}

Access input from UIAlertController

I know comments have been posted to answer the question, but an answer should make the solution explicit.

@IBOutlet var newWordField: UITextField
func wordEntered(alert: UIAlertAction!){
// store the new word
self.textView2.text = deletedString + " " + self.newWordField.text
}
func addTextField(textField: UITextField!){
// add the text field and make the result global
textField.placeholder = "Definition"
self.newWordField = textField
}

// display an alert
let newWordPrompt = UIAlertController(title: "Enter definition", message: "Trainging the machine!", preferredStyle: UIAlertControllerStyle.Alert)
newWordPrompt.addTextFieldWithConfigurationHandler(addTextField)
newWordPrompt.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: nil))
newWordPrompt.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: wordEntered))
presentViewController(newWordPrompt, animated: true, completion: nil)

This displays a prompt with a text field and two action buttons. It reads the text field when done.

How to present UIAlertController when not in a view controller?

I posted a similar question a couple months ago and think I've finally solved the problem. Follow the link at the bottom of my post if you just want to see the code.

The solution is to use an additional UIWindow.

When you want to display your UIAlertController:

  1. Make your window the key and visible window (window.makeKeyAndVisible())
  2. Just use a plain UIViewController instance as the rootViewController of the new window. (window.rootViewController = UIViewController())
  3. Present your UIAlertController on your window's rootViewController

A couple things to note:

  • Your UIWindow must be strongly referenced. If it's not strongly referenced it will never appear (because it is released). I recommend using a property, but I've also had success with an associated object.
  • To ensure that the window appears above everything else (including system UIAlertControllers), I set the windowLevel. (window.windowLevel = UIWindowLevelAlert + 1)

Lastly, I have a completed implementation if you just want to look at that.

https://github.com/dbettermann/DBAlertController

Presenting UIAlertController (in actionsheet style) caused mysterious autolayout warning

This is a bug which is not fixed by Apple team this bug is related to alerts and action sheets related to animation. You may follow these links to verify this:-

UIAlertController's actionSheet gives constraint error on iOS 12.2 / 12.3

Swift default AlertViewController breaking constraints

The solution for this is to pass animation value as false like:-

controller.present(alertController, animated: false, completion: nil)

I have present the UIAlertController without animation and warning got vanished.

or you may try this solution mentioned in the one of the links of stackoverflow:-

@IBAction func buttonTapped(_ sender: UIButton) {
...
self.present(alertController,animated: true,completion: nil)

alertController.view.subviews.flatMap({$0.constraints}).filter{ (one: NSLayoutConstraint)-> (Bool) in
return (one.constant < 0) && (one.secondItem == nil) && (one.firstAttribute == .width)

}.first?.isActive = false

}


Related Topics



Leave a reply



Submit