Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 282 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How would I create a UIAlertView in Swift?

#11
I got the following `UIAlertView` initialization code to compile without errors (I thing the last, varyadic part is tricky perhaps). But I had to make sure the class of `self` (which I am passing as the delegate) was adopting the `UIAlertViewDelegate` protocol for the compile errors to go away:

let alertView = UIAlertView(
title: "My Title",
message: "My Message",
delegate: self,
cancelButtonTitle: "Cancel",
otherButtonTitles: "OK"
)


By the way, this is the error I was getting (as of Xcode 6.4):
> Cannot find an initializer for type 'UIAlertView' that accepts an
> argument list of type '(title: String, message: String, delegate:
> MyViewController, cancelButtonTitle: String, otherButtonTitles:
> String)'

As others mentioned, you should migrate to UIAlertController if you can target iOS 8.x+. To support iOS 7, use the code above (iOS 6 is not supported by Swift).
Reply

#12
I have another trick. Suppose you have 5 classes where a logout alert to be applied. Try with swift class extension.

File- New- Swift class- Name it.

Add the following:

public extension UIViewController
{

func makeLogOutAlert()
{
var refreshAlert = UIAlertController(title: "Log Out", message: "Are You Sure to Log Out ? ", preferredStyle: UIAlertControllerStyle.Alert)

refreshAlert.addAction(UIAlertAction(title: "Confirm", style: .Default, handler: { (action: UIAlertAction!) in
self.navigationController?.popToRootViewControllerAnimated(true)
}))

refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .Default, handler: { (action: UIAlertAction!) in
refreshAlert .dismissViewControllerAnimated(true, completion: nil)
}))

presentViewController(refreshAlert, animated: true, completion: nil)
}
}


Implement using : self.makeLogOutAlert(). Hope it helps.

Reply

#13
I have made a singleton class to make this convenient to use from anywhere in your app:

[To see links please register here]


You can then create a popup with multiple buttons like this:

Popups.SharedInstance.ShowAlert(self, title: "Title goes here", message: "Messages goes here", buttons: ["button one" , "button two"]) { (buttonPressed) -> Void in
if buttonPressed == "button one" {
//Code here
} else if buttonPressed == "button two" {
// Code here
}
}

or popups with a single button like this:

Popups.SharedInstance.ShowPopup("Title goes here", message: "Message goes here.")
Reply

#14
With the protocol extensions of Swift 2, you can make a protocol that provides a default implementation to your view controllers:


ShowsAlert.swift

import UIKit

protocol ShowsAlert {}

extension ShowsAlert where Self: UIViewController {
func showAlert(title: String = "Error", message: String) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .Default, handler: nil))
presentViewController(alertController, animated: true, completion: nil)
}
}

ViewController.swift

class ViewController: UIViewController, ShowsAlert {
override func viewDidLoad() {
super.viewDidLoad()
showAlert(message: "Hey there, I am an error message!")
}
}
Reply

#15
**Here is a pretty simple function of AlertView in Swift :**



class func globalAlertYesNo(msg: String) {
let alertView = UNAlertView(title: "Title", message: msg)

alertView.messageAlignment = NSTextAlignment.Center
alertView.buttonAlignment = UNButtonAlignment.Horizontal

alertView.addButton("Yes", action: {

print("Yes action")

})

alertView.addButton("No", action: {

print("No action")

})

alertView.show()

}

*You have to pass message as a String where you use this function.*
Reply

#16
The Old Way: UIAlertView


let alertView = UIAlertView(title: "Default Style", message: "A standard alert.", delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles: "OK")
alertView.alertViewStyle = .Default
alertView.show()

// MARK: UIAlertViewDelegate

func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
switch buttonIndex {

// ...
}
}

The New Way: UIAlertController

let alertController = UIAlertController(title: "Default Style", message: "A standard alert.", preferredStyle: .Alert)

let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
// ...
}
alertController.addAction(cancelAction)

let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in
// ...
}
alertController.addAction(OKAction)
self.presentViewController(alertController, animated: true) {
// ...
}
Reply

#17
**Swift 3**

The following is a simple example of how to create a simple alert with one button with Swift 3.

let alert = UIAlertController(title: "Title",
message: "Message",
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default))
present(alert, animated: true)

In the above example the handle callback of the action has been omitted because the default behaviour of an alert view with one button is to disappear when the button is clicked.

Here is how to create another action, which could be added to the alert with "alert.addAction(action)". The different styles are .default, .destructive and .cancel.

let action = UIAlertAction(title: "Ok", style: .default) { action in
// Handle when button is clicked
}
Reply

#18
Use this code to display an alertview



let alertController = UIAlertController(title: "Hello Coders", message: "your alert message", preferredStyle: .Alert)
let defaultAction = UIAlertAction(title: "Close Alert", style: .Default, handler: nil)
alertController.addAction(defaultAction)

presentViewController(alertController, animated: true, completion: nil)

Reference: [Swift Show Alert using UIAlertController][1]


[1]:

[To see links please register here]

Reply

#19
You can create a UIAlert using the standard constructor, but the 'legacy' one seems to not work:

let alert = UIAlertView()
alert.title = "Alert"
alert.message = "Here's a message"
alert.addButtonWithTitle("Understood")
alert.show()
Reply

#20
in xcode 9

let alert = UIAlertController(title: "Alert", message: "message", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through