UIAlertView in Swift, getting EXC_BAD_ACCESS - ios

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

Related

How to display (NS)Errors in UIAlertController?

I have found myself repeatedly writing the following lines of code
let alert = UIAlertController(title: "Something went wrong", message: "This is why something went wrong", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true)
And then I would populate the titles and message of the alert and buttons from NSError objects. In search of a better, more efficient way I came across Apple's Error Handling Programming Guide, which seemed promising:
Note: Beginning with OS X version 10.4, you can use the alertWithError:class method of NSAlert as a convenience for creating NSAlert objects to use when displaying alert dialogs or sheets. The method extracts the localized information from the passed-in NSError object for its message text, informative text, and button titles. You may also use the presentError: message to display error alerts.
The NSError class contains properties specifically meant to be displayed in an alert view (at least for OS X). Unfortunately, I was not able to find a similar approach for iOS.
Is there an easy, convenient way to display Error objects in an Alert in iOS?
Write your own extension to UIAlertController:
extension UIAlertController {
func alert(with error: Error) -> UIAlertController {
// Create and setup the alert as needed using the error
var res = UIAlertController(title: "Something went wrong", message: "Some message", preferredStyle: .alert)
res.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
return res
}
}
Update the parameter to use NSError if desired and/or add additional parameters as needed to suit your needs.
Then you can use it as follows:
let alert = UIAlertController.alert(with: someError)
self.present(alert, animated: true)
Is there an easy, convenient way to display Error objects in an Alert in iOS?
UIAlertController does not have a convenient (auto-parsing) way to display an Error/NSError atm. UIKit does not provide any, so you will end up with a custom solution.
NSError has localizedDescription property which can be shown in UIAlertController's message as follow,
let alert = UIAlertController(title: "Something went wrong", message: error.localizedDescription, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true)

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

Alert error in swift [duplicate]

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)

Resources