Alert error in swift [duplicate] - ios

This question already has answers here:
UIAlertView is Not Working in Swift
(4 answers)
Closed 8 years ago.
I writing this code in swift and Xcode 6
#IBAction func Alert(sender : UIButton) {
var alert : UIAlertView = UIAlertView(title: "Hey", message: "This is one Alert", delegate: nil, cancelButtonTitle: "Working!!")
alert.show()
}
Xcode doesn't show error in compilation.
but in Simulator the APP fails and return the error:
(lldb)
thread 1 EXC_BAD_ACCESS(code 1 address=0x20)

There is a bug in the Swift shim of the UIAlertView convenience initializer, you need to use the plain initializer
let alert = UIAlertView()
alert.title = "Hey"
alert.message = "This is one Alert"
alert.addButtonWithTitle("Working!!")
alert.show()
This style code feels more true to the Swift Language. The convenience initializer seems more Objective-C'ish to me. Just my opinion.
Note: UIAlertView is deprecated (see declaration) but Swift supports iOS7 and you can not use UIAlertController on iOS 7
View of UIAlertView Declaration in Xcode
// UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead
class UIAlertView : UIView {
An Alert in Swift iOS 8 Only
var alert = UIAlertController(title: "Hey", message: "This is one Alert", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Working!!", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
Update for Swift 4.2
let alert = UIAlertController(title: "Hey", message: "This is one Alert", preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Working!!", style: UIAlertAction.Style.default, handler: nil))
self.present(alert, animated: true, completion: nil)

Related

Show an UIAlertController from a button on an UITableViewCell

This is what I coded inside a button in a UITableViewCell.
var shareAlert = UIAlertController(title: "Post Alert",
message: "Your Post has been Shared with your Friends",
preferredStyle: UIAlertControllerStyle.Alert)
var Ok = UIAlertAction(title: "Ok",
style: UIAlertActionStyle.Default,
handler: nil)
shareAlert.addAction(Ok)
shareAlert.presentViewController(shareAlert, animated: true, completion: nil)
When the user clicks the button I want it to show this alert, but the app crashes as soon as I click the button. Am I doing it wrong? How do I show an alert view from a button in a UITableViewCell?
It crashed because of this line
shareAlert.presentViewController(shareAlert, animated: true, completion: nil)
A controller cannot present itself, so
Replace
shareAlert.presentViewController(shareAlert, ...)
by
yourCurrentViewController.presentViewController(shareAlert, ...)
And you should not use UIAlertView because it was deprecated in iOS 9 and maybe obsoleted in the next iOS, so use UIAlertController and you don't have to change it in near future
Please check this:
https://stackoverflow.com/a/32574681/5304286
You can call alertShow function in this file to show simple message by the way:
Utility.UI.alertShow("message", withTitle: "title", viewController: self)
Instead of using UIAlertController you can use UIAlertView, like this:
var alert = UIAlertView(title: "Post Alert", message: "Your Post has been Shared with your Friends", delegate: nil, cancelButtonTitle: "Ok")
alert.show()
Documentation:
https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIAlertView_Class/index.html
Note:
UIAlertView is deprecated in iOS 9.

How to initialize Text to Speech object from Nuance SpeechKit in Swift [duplicate]

I have a an objective-c class (RDAlertView) which create an Alert (UIAlertView for ios < 8 and UIAlertController for ios >=8 )
Here is the code in Objective-C:
RDAlertView* alert = [[RDAlertView alloc] initWithTitle:#"Notification"
message:#"message"
completion:completion:^(NSInteger buttonIndex, NSInteger cancelButtonIndex, NSArray *textflieds) { }
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
My question is: How to call this in Swift without any change in my class (if possible)?
Here is a link for the sample project : https://bitbucket.org/ejanowski/rdalertview-issue
I think this is what you need:
var alert = RDAlertView(title: "Notification", message: "message", completion: nil, cancelButtonTitle: "OK", otherButtonTitles: nil)
alert.show()
Hope It will help.
These methods are treated like constructors. You can call them like this:
var alert = RDAlertView("notification", message:"message", completion:nil, cancelButtonTitle:"Cancel", otherButtonTitles:nil)
alert.show()
Caveat: written without an IDE, careful of syntax errors / typos
UIAlertView is deprecated in iOS 8.
You can show an alert with this code:
var alert = UIAlertController(title: "Alert", message: "test", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
UIAlertView.showWithTitle("Hello", message: "Hello World", cancelButtonTitle: "Okay") { alertView, buttonIndex in
// Do something when the alert view is clicked
let anAlertView = UIAlertView(title: "Choice", message: "Pick one", cancelButtonTitle: "No", otherButtonTitles: "Yes", "Maybe")
anAlertView.showWithCompletion { alertView, buttonIndex in
switch buttonIndex
{
case 1: println("Yes")
case 2: println("Maybe")
default: println("No")
}
}

Swift Alert popup is appearing in simulator iphone4s(8.1), not iphone4s (7.1)

I have written code to display a simple alert popup when a button is clicked. When trying the app in simulator iPhone 4s (8.1), it's working as expected, but when trying in simulator iPhone 4s (7.1), the app keeps crashing.
Here the code:
#IBAction func buttonPressed(sender: UIButton) {
let controller = UIAlertController(title: "This is a title", message: "Hello, my friend", preferredStyle: UIAlertControllerStyle.Alert)
let cancelAction = UIAlertAction(title: "Phew!", style: .Cancel, handler: nil)
controller.addAction(cancelAction)
self.presentViewController(controller, animated: true, completion: nil)
}
Here's the error message:
The first line code (the one creating the "controller" constant) is highlighted in green with the message "Thread 1: EXC_BAD_ACCESS(Code=1,address=0x10)"
I would appreciate any help
UIAlertController is available for iOS >= 8.0
You have to use UIAlertView for iOS < 8.0
UIAlertController is only available in iOS 8.0 and on wards unfortunately, here is documentation and it states this on the right: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertController_class/#//apple_ref/doc/uid/TP40014538-CH1-SW2 I believe this replaced the now deprecated UIAlertView which can be found here in Apple Documentation: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertView_Class/index.html#//apple_ref/occ/cl/UIAlertView Swift doesn't like the old but consider and "if" clause and create both a UIAlertView and UIAlertController for the respective iOS system using the app.
Thanks to recommended links from Dom Bryan, I managed the following solution:
#IBAction func buttonPressed(sender: UIButton) {
if NSClassFromString("UIAlertController") == nil{
let alert = UIAlertView(title: "This is a title", message: "I am an iOS7 alert", delegate: self, cancelButtonTitle: "Phew!")
alert.show()
}else{
let controller = UIAlertController(title: "This is a title", message: "Hello, my friend", preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Phew!", style: .Cancel, handler: nil)
controller.addAction(cancelAction)
self.presentViewController(controller, animated: true, completion: nil)
}
}
And it's working fine on iOS7 and iOS8 devices

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)

UIAlertView in Swift, getting EXC_BAD_ACCESS

First and foremost, I'm quite aware that Xcode 6 and the Swift language are in Beta and are prone to errors; however, this particular one seems to be something strange as everything else I've tried so far seems to work fine.
If this is not appropriate for StackOverflow, I will gladly remove the question.
I've begin playing with Xcode 6/Swift (preparing for its release) and it has been an extraordinarily enjoyable experience compared to what I thought it would be. That being said, one issue in porting a "training" style app I like to do is that I can't seem to generated a UIAlertView due to EXC_BAD_ACCESS the code in question is:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
var alert = UIAlertView(title: "Title", message: "Message", delegate: nil, cancelButtonTitle: "OK") // EXC_BAD_ACCESS here
alert.show()
}
On the line that creates the UIAlertView I get an EXC_BAD_ACCESS because [UIAlertView retain] was called on a deallocated instance.
Again, I'm chalking this up to the beta banner but was curious if I was doing something wrong or if anyone else has run into similar issues.
Try the following code
let alert = UIAlertView()
alert.title = "Title"
alert.message = "My message"
alert.addButtonWithTitle("Ok")
alert.show()
But in iOS 8
UIAlertView is deprecated. So use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert. It should be:
var alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
Check the above code, are you getting same error or not ?
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: "Send",
message: "You have successfully send your feedback.",
preferredStyle: UIAlertControllerStyle.Alert
)
alert.addAction(UIAlertAction(
title: "Ok",
style: UIAlertActionStyle.Default,
handler: nil
))
self.presentViewController(alert, animated: true, completion: nil)
2 things
u have to use delegate:self
cancelButtonTitle ends with nil

Resources