In Swift how to create Custom Alert View [closed] - ios

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have to implement a Custom alerts in my App programmatically using Swift language. i tried implementing using some third party library "SCLAlertView" but not able to understand from that, i need a implement simple alert pop ups with dynamic message and number of button changes in App. As there were lots of AlertView in my App. so i need to update the dynamically.
below i have attached an sample image of custom alert how it looks to implement
Please help me to implement this feature.

After the installing pod with pod SCLAlertView
You can choose Alert view Style and Alert View Animation style with these enums
enum SCLAlertViewStyle: Int {
case Success, Error, Notice, Warning, Info, Edit, Wait
}
public enum SCLAnimationStyle {
case NoAnimation, TopToBottom, BottomToTop, LeftToRight, RightToLeft
}
SCLAlertView has many Control groups like add textField , buttons and icons
here is a adding button function codes
let alertView = SCLAlertView()
alertView.addButton("First Button", target:self, selector:Selector("firstButton"))
alertView.addButton("Second Button") {
println("Second button tapped")
}
alertView.showSuccess("Button View", subTitle: "This alert view has buttons")
and Alert view custom types
SCLAlertView().showError("Hello Error", subTitle: "This is a more descriptive error text.") // Error
SCLAlertView().showNotice("Hello Notice", subTitle: "This is a more descriptive notice text.") // Notice
SCLAlertView().showWarning("Hello Warning", subTitle: "This is a more descriptive warning text.") // Warning
SCLAlertView().showInfo("Hello Info", subTitle: "This is a more descriptive info text.") // Info
SCLAlertView().showEdit("Hello Edit", subTitle: "This is a more descriptive info text.") // Edit
in Github Page you will find many beatiful desing alert views , it is easy to use
https://github.com/vikmeup/SCLAlertView-Swift

Just add different UIAlertAction to your UIAlertController whenever you want it to.
let alertAction: UIAlertAction = UIAlertAction(title: "YES", style: UIAlertActionStyle.Default, handler: {
//Code goes here
})
let alertAction2: UIAlertAction = UIAlertAction(title: "NO", style: UIAlertActionStyle.Default, handler: {
//Code goes here
})
let alertAction3: UIAlertAction = UIAlertAction(title: "Maybe", style: UIAlertActionStyle.Default, handler: {
//Code goes here
})
alert.addAction(alertAction)
alert.addAction(alertAction2)
alert.addAction(alertAction3)
You can dynamically add UIAlertAction's to your UIAlertController depending on your needs. If you only need two buttons, then don't add alertAction3. If you need three or four, then add them as necessary.

Related

Subsequent presentations of UIAlertController stutter

In a very loaded ViewController in an in-house app I've developed (iPad app written in Swift) for my company, I present alert style UIAlertControllers in a few circumstances. The first time an alert is presented, it always displays very smoothly. Any subsequent displays of an alert however end up being very stuttery, even for alerts that don't have much code surrounding their presentation and dismissal.
For example, if the user tries to dismiss the vc without saving after making changes (simple conditional of "if bool_Saved == false { create and display alert }") an alert is presented asking if they're sure they want to exit without saving. If they choose Yes, the vc is dismissed, otherwise, the alert is dismissed. The first creation and presentation of the alert animates smoothly, but anytime this conditional or any other code needs to present an alert, the presentation stutters its way through the animation.
The UI on this vc is pretty loaded. The whole screen is a scrollView with a contentView that contains 10 sub UIViews which in turn each have 3 UITextFields, ~9 UILabels, and ~8 UIButtons (the company wanted an exact digital replica of a paper form they've been using). Dismissing the vc and reloading it causes the next alert to again display smoothly, but again, subsequent alerts stutter through their present animation.
I've begun profiling in Instruments, but am a bit inexperienced in it and intend to use most of today getting more familiar with the various tools to hopefully find a source for this issue. What I'd like to know is if anyone has any suggestions on what might be causing this stuttering problem.
Thanks, and please let me know if there's any additional information I can provide.
Editing with code snippet described above:
func cancelTapped() {
if savedOnce == false {
let alert_Exit = UIAlertController(title: "Exit Inspection?", message: "Unsaved changes to the sheet will be lost upon exit. Are you sure you want to exit without saving?", preferredStyle: .alert)
let action_No = UIAlertAction(title: "No", style: .cancel, handler: nil )
let action_Yes = UIAlertAction(title: "Yes", style: .default, handler: { [unowned self]
(action) in
self.exitSheet()
})
alert_Exit.addAction(action_No)
alert_Exit.addAction(action_Yes)
present(alert_Exit, animated: true, completion: nil)
} else {
exitSheet()
}
}
You are not dismissing the alert. Insert that line in your Yes button.
let action_Yes = UIAlertAction(title: "Yes", style: .default, handler: { [unowned self]
(action) in
alert_Exit.dismiss(animated: true)
self.exitSheet()
})
When you create the alert a second time, it probably results in a conflict with previous alert hidden somewhere in your view that wasn't dismissed. How loaded the VC is, it probably doesn't make any difference.
Style .cancel will remove itself anyway, whilst .default will require you to remove the alert.

How to display a dialog with (multiselect listview) before displaying "app would like to send you push notifications" dialog box

I would like to display a pop-up where I'll ask the user about their preferences related to the push notifications and for that one, I want to display a list of options to the user. User can select more than one options.
I think that I'll have to display a tableview inside the UIAlertView, but it is deprecated now. So, how can I display a pop (with some small message + multiple select list ) before the APN system permissions dialog in Swift.
Any help will be appreciated.
You can use this code:
let alert = UIAlertController(title: title, message:message, preferredStyle: .Alert)
let action = UIAlertAction(title: "OK", style: .Default) { _ in
acceptNotification = true //code to execute when the user taps that OK
}
alert.addAction(action)
//you can add more actions
self.presentViewController(alert, animated: true){ // this part if provided, will be invoked after the dismissed controller's viewDidDisappear: callback is invoked.
}

Do I have to hard-code / localise myself the "OK" button when generating an alert in swift?

I have a convenience method in Swift (iOS 8+) for displaying an error message. So far it looks like this:
// Supplied with a category code and an error code, generates an error dialogue box.
// Codes rather than strings because this needs to be localisable.
func showErrorDialogue(categoryCode: String, _ errorCode: String) -> () {
// Fetch the actual strings from the localisation database.
let localisedCategory = NSLocalizedString(categoryCode, comment: categoryCode)
let localisedError = NSLocalizedString(errorCode, comment: errorCode)
// Create an alert box
let alertController = UIAlertController(
title: localisedCategory,
message: localisedError,
preferredStyle: .Alert
)
alertController.addAction(
UIAlertAction(
title: "OK", // FIXME: why isn't this localised?
style: .Default,
handler: { (action) in return }
)
)
self.presentViewController(alertController, animated: true) { return }
}
It seems odd that I can't just say "I'm only adding one button to this alert box, so please assume it's going to be the locale-default OK button". The best solution I've found so far with limited Googling appears to be Steal them from the System and hope which is more than a little dodgy.
As far as I know, there are no better ways to do this than stealing it from the system like you found.
Though it's not the hardest thing to localize, I agree it would be great if Apple made that automatic in a future version of the OS.

How can I add an alert view [duplicate]

This question already has answers here:
How would I create a UIAlertView in Swift?
(36 answers)
Closed 7 years ago.
I would like to be able to add an alert view to my login screen that says Incorrect Username/ Password when the info submitted is incorrect. I am using Swift 2. I'm still new to learning code and I am unsure how to add this action.
Just use this code to display the alert:
func wrongLogin(input: String) {
var alert = UIAlertController(title: "Incorrect", message: "The \(input) is wrong. Try again.", preferredStyle: UIAlertControllerStyle.Alert)
//add a cancel button
alert.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.Cancel, handler: nil))
// Show it
showViewController(alert, sender: self)
}
Then when calling it just pass in if it's the username or the password.
You can do that like this wrongLogin("password") or wrongLogin("username")
For further info see this answer.
Hope that helps, Julian.

UIAlertView And UIAlertController

I am using SWIFT in xcode 6 to develope my application AND needs to support the APP for iphone 4 and later versions... So have selected the 'Deploment Target' as 7.1. In simple words needs to support iOS7, iOS8 AND iOS9...
When using Alert View I came across in many places discussing now we have to use newly introduced 'UIAlertController' rather than the old 'UIAlertView'...
By reading got to know UIAlertView is deprecated from ios 8.
But in my case as I have to support for ios 7.1 AND I can NOT only use 'UIAlertController'.
I started using as the following way as many tutorials explains...
if (objc_getClass("UIAlertController") != nil)
{
// Use UIAlertController
}
else
{
// Use UIAlertView
}
But in this way got to write the same code twice and really annoyiong... Either I have to create a custom Alertview combining both or needs to continue coding like this....
but just to test I've used only UIAlertView (ignoring UIAlertController) and the app runs fine even in ios 8 simulators... But the document says UIAlertView is deprecated from iOS 8.0...
So my question is, Like to hear what the best practice to continue my app with these API changes... Is it alright if I ignore 'UIAlertController' and work with old 'UIAlertView' until stop support for iOS7 one day... Will that effect in to my app in any way bad? Thanks
I've written a wrapper around these two classes which uses appropriate classes according to iOS version.
find it here
https://github.com/amosavian/ExtDownloader/blob/master/Utility%2BUI.swift
to show a simple alert use this:
Utility.UI.alertShow("message", withTitle: "title", viewController: self);
to show an action view: use this code:
let buttons = [Utility.UI.ActionButton(title: "Say Hello", buttonType: .Default, buttonHandler: { () -> Void in
print("Hello")
})]
Utility.UI.askAction("message", withTitle: "title", viewController: self, anchor: Utility.UI.AnchorView.BarButtonItem(button: uibarbuttontapped), buttons: buttons)
In case you are presenting action in iPhone, it will add a cancel button automatically. But you can have custom cancel button by defining a button with type of ".Cancel"
If you want show a modal alert view, use this code:
let cancelBtn = Utility.UI.AlertButton(title: "Cancel", buttonType: .Cancel, buttonHandler: nil)
let okBtn = Utility.UI.AlertButton(title: "OK", buttonType: .Default, buttonHandler: { (textInputs) -> Void in
print("OK button pressed")
})
Utility.UI.askAlert("message", withTitle: "title", viewController: self, buttons: [cancelBtn, okBtn], textFields: nil)
As you can see, the text fields is set to nil above. You can set it if you want ask something from user, like this:
let cancelBtn = Utility.UI.AlertButton(title: "Cancel", buttonType: .Cancel, buttonHandler: nil)
let okBtn = Utility.UI.AlertButton(title: "OK", buttonType: .Default, buttonHandler: { (textInputs) -> Void in
print("You entered" + textInputs[0])
}
})
let textFields = [Utility.UI.AlertTextField(placeHolder: "enter secret text here", defaultValue: "", textInputTraits: TextInputTraits.secretInput())]
Utility.UI.askAlert("Enter password", withTitle: "title", viewController: self, buttons: [cancelBtn, okBtn], textFields: textFields)
You can customize almost everything. e.g by defining custom textInputTrait you can customize text inputs easily. also you have not to deal with delegates, instead simple closures are there. Please read code to find out more.

Resources