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.
Related
I am quite new to Swift and IOS, but I don't think that I should be stuck in the alert "popup". I can see the following but when pressing Restart, nothing happens.Alert popup
If I use a closure instead of nil to handler nothing happened.
I wrote the following in Swift5 using Xcode 10.3:
let alert = UIAlertController(title: title, message: message,
preferredStyle: .alert)
let action = UIAlertAction(title: "Restart", style: .default, handler: nil)
alert.addAction(action)
present(alert, animated: true, completion: nil)
You need to dismiss the alert inside the completion handler of the ok action.
I have created a UIAlertView. The main content is loaded with JSON and I want that when I click "Completo" button, the content change to another variable value completo. When I hit Mapa I need to open a map. And when I click Atrás it should close the alert (right now all the buttons close it).
var tabla: String = "" // JSON Data
...
#IBAction func Itinerario(sender: AnyObject) {
let alert = UIAlertView(title: "Itinerario de Procesión", message: (tabla), delegate: nil, cancelButtonTitle: nil, otherButtonTitles: "Completo", "Mapa", "Atrás")
alert.show()
}
Also I wanted to change buttons color.
I am getting this advice: /Users/Javi/Desktop/ElPenitente/El Penitente/Domingo R.swift:87:21: 'UIAlertView' was deprecated in iOS 9.0: UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead
So what the error means is that you should use a UIAlertController instead.
Usage is quite straightforward:
let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
let action1 = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)
let action2 = UIAlertAction(title: "Action", style: UIAlertActionStyle.Default) { (action) in
// Do something here when action was pressed
}
alert.addAction(action1)
alert.addAction(action2)
self.presentViewController(alert, animated: true, completion: nil)
Where action1 has a system style .Cancel and no handler, and action2 has a default style and a handler.
Changing the color of the actions can be done using the tintColor attribute of the views, see this answer.
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
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)
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