UIAlertView displays alert showing late than when required ios - ios

I am using UIAlertView to display alert and it is working for first time that when i click alert appears but when second time i do it then on clicking twice alert gets displayed instead of 1st click.
During secong time "visited full" gets printed on 1st click but alert appears on 2click, Why does this appear?Please help me to resolve.Thanks in advance
println("visited full")
var alert:UIAlertView = UIAlertView(title: "Video", message: "You have played all videos", delegate: self, cancelButtonTitle: "OK")
alert!.show()

IF it doesn't work then try this may be this will work.
let alert = UIAlertController(title: "Video", message: "You have played all videos", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
EDIT:
I think you can use it like this way too:
dispatch_async(dispatch_get_main_queue(), {
var alert:UIAlertView = UIAlertView(title: "Video", message: "You have played all videos", delegate: self, cancelButtonTitle: "OK")
alert.show()
})
and for UIAlertController
dispatch_async(dispatch_get_main_queue(), {
let alert = UIAlertController(title: "Video", message: "You have played all videos", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
})

Related

alert iOS swift

When I test for bad data in a text field and try to show an alert for missing or bad data the alert does not display. But I can get an alert to display in the viewDidLoad function
Here is the code
import UIKit
import CoreData
class SellViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
#IBOutlet weak var customer: UITextField!
#IBOutlet weak var bales: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let alert = UIAlertController(title: "Hey", message: "# SellViewController viewDidLoad ", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Working!!", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
func checkDataInput(){
print("checking data input customer.text \(customer.text)")
print("checking data input bales.text \(bales.text)")
if (customer.text!.isEmpty) {
customer.text = "REQUIRED"
missingCustomer()
}
if (bales.text!.isEmpty){
availableAlert()
}else{
newQuantity = Int(bales.text!)!
}
func availableAlert() {
print(" at availableAlert")
let alert = UIAlertController(title: "Hey", message: "# SellViewController func missingValues ", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Working!!", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
func missingCustomer() {
print(" at missingCustomer")
let alert = UIAlertController(title: "Hey", message: "# SellViewController func missingCustomer ", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Working!!", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
This displays alert at viewDidLoad but no alert when I have missing data. The print statements when I have missing data return as.
checking data input customer.text Optional("")
checking data input bales.text Optional("")
at missingCustomer
at availableAlert
fatal error: unexpectedly found nil while unwrapping an Optional value
The fatal error happens because the user is not able to correct their response when they get the alert.
What am I doing wrong? Shouldn't the alert display immediately when my
missingData functions are triggered and the be able to correct their entries?
This sounds like a threading issue. Your data evaluation code is most likely executing on the main thread (you haven't posted it so cant say for sure) so when you try to do UI work, presenting an alert in this case, it will not execute. To fix this, try this code instead in your missingData function:
let alert = UIAlertController(title: "Hey", message: "# SellViewController viewDidLoad ", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Working!!", style: UIAlertActionStyle.Default, handler: nil))
dispatch_async(dispatch_get_main_queue()) {
self.presentViewController(alert, animated: true, completion: nil)
}
let alert = UIAlertController(title: "Hey", message: "# SellViewController viewDidLoad ", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Working!!", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
you can change the code to ->
let alert = UIAlertController(title: "Hey", message: "# SellViewController viewDidLoad ", preferredStyle: UIAlertControllerStyle.Alert)
let alertButton = UIAlertAction(title: "Working!!", style: UIAlertAction.Style.default)
alert.addAction(alertButton)
present(alert, animated: true)

UIAlertController not showing

I'm trying to display a message to my users with alert controller but it wont show
let alert = UIAlertController(title: "Error", message: "Incorrect username/password", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "okay", style: UIAlertActionStyle.Default, handler: nil))
looks like you haven't presented it yet. paste this code where you want to show the alert
self.present(alert, animated: true, completion: nil)

Alertview when there is no profile picture added (Swift)

Im new to programming and need some help here I have created a signup page in swift with some text fields and buttons for names, passwords. I have added a UIImage for users to add their picture. I want each users to MUST include a picture to be able to signup.
if (user does not include picture empty) <--- code here {
var alert = UIAlertController(title: "Warning", message: "you need to add profile picture", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title:"ok", style:UIAlertActionStyle.Default, handler:nil))
self.presentViewController(alert, animated: true, completion: nil)
}
thank you.
You can check this way if imageView is nil or not:
if yourImageView.image == nil {
var alert = UIAlertController(title: "Warning", message: "you need to add profile picture", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}

UIAlertController is giving error on turning off WiFi

UIAlertController is giving error on turning WiFi off
let networkIssueController = UIAlertController(title: "Error",
message: "Unable to load data. Connectivity error!",
preferredStyle: .Alert)
let okButton = UIAlertAction(title: "Ok", style: .Default, handler: nil)
networkIssueController.addAction(okButton)
let cancelButton = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
networkIssueController.addAction(cancelButton)
self.presentViewController(networkIssueController, animated: true, completion: nil)
error showing is EXC_BAD_ACCESS(code=1,address=0x10)
What may be the cause for this error?
Try adding a separate alertView instance.
This is a simple alert view and this works perfectly. Try this out.
let alertController = UIAlertController(title: "Warning", message: "Test", preferredStyle: UIAlertControllerStyle.Alert)
let cancel = UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel, handler: nil)
alertController.addAction(cancel)
self.presentViewController(alertController, animated: true, completion: nil)

UIAlertView is Not Working in Swift

when i runt this code in swift, i dont know why the app terminates by showing a break point in the "alertView.show()" part, Somebody please help me.
var alertView = UIAlertView(
title: "Hey",
message: "Hello",
delegate: self,
cancelButtonTitle: "Cancel"
)
alertView.show()
From Xcode 6.0 UIAlertView class:
UIAlertView is deprecated. Use UIAlertController with a preferredStyle
of UIAlertControllerStyleAlert instead.
On swift ( iOS 8 and OS X 10.10 ), you can do this:
var alert = UIAlertController(title: "Alert Title", message: "Alert Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Cancel, handler:handleCancel))
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler:{ (ACTION :UIAlertAction!)in
println("User click Ok button")
}))
self.presentViewController(alert, animated: true, completion: nil)
func handleCancel(alertView: UIAlertAction!)
{
println("User click cancel button")
}
If you want to use in 'ActionSheet' instead 'Alert' you need only to change the UIAlertControllerStyle for example:
var alert = UIAlertController(title: "Alert Title", message: "Alert Message", preferredStyle: UIAlertControllerStyle.ActionSheet)
UIAlertView is deprecated in iOS 8, But Swift supports iOS7 and you can not use UIAlertController on iOS 7.
Add the following method to solve the issue :
func showAlert(title:NSString, message:NSString,owner:UIViewController) {
if let gotModernAlert: AnyClass = NSClassFromString("UIAlertController") {
var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
owner.presentViewController(alert, animated: true, completion: nil)
}
else {
let alertView = UIAlertView(title: title, message: message, delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles: "OK")
alertView.alertViewStyle = .Default
alertView.show()
}
}
and call the method anywhere from the code like this :
showAlert(APP_NAME,message: "Add your alert message here" ,owner: self)
the best way for me is...
class ViewController: UIViewController, UIAlertViewDelegate {
var allarme = UIAlertView(title: "Warning", message: "This is a best way to create a alarm message", delegate: self, cancelButtonTitle: "OK")
allarme.show()
remember to import on the class UIAlertViewDelegate
Use following way :
var altMessage = UIAlertController(title: "Warning", message: "This is Alert Message", preferredStyle: UIAlertControllerStyle.Alert)
altMessage.addAction(UIAlertAction(title: "Done", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(altMessage, animated: true, completion: nil)

Resources