I have this code:
var bt = {}
bt = {
text: "OK",
click: function () {
// Deletefunction here...
$(this).remove()
}
}, {
text: "Cancel",
click: function () {
$(this).remove()
}
}
$("#test").dialog({
title: "Confirm deletion",
width: "300px",
buttons: [bt]
})
Why do I only see the OK button, and not the Cancel button?? JSfiddle here.
That's because your assignment to bt does not do what you think it does. It ends up applying the comma operator and is equivalent to:
bt = {
text: "OK",
click: function () {
// Deletefunction here...
$(this).remove();
}
};
// Stray, ignored object literal.
{
text: "Cancel",
click: function () {
$(this).remove();
}
};
If you want bt to be an array, then make it so:
var bt = [{
text: "OK",
click: function () {
// Deletefunction here...
$(this).remove();
}
}, {
text: "Cancel",
click: function () {
$(this).remove();
}
}];
$("#test").dialog({
title: "Confirm deletion",
width: "300px",
buttons: bt
});
Related
initially i am showing one alert when click on yes it will show second alert, in second alert when user press on reset i want show progress view and call service in this progress view is not showing only service call is happening i want to show progress view also, so how how can i show progress view after press ok in second alert?
func makeServiceCall()
{
Utility.showGlobalProgressHUD(withTitle: "Loading...")
HTTPRequest.serviceReq(newPin: "129354") { (ResetModel) in
Utility.dismissGlobalHUDInMainThread()
Utility.showAlertMessageInMainThread(title: " Reset", msg: "Your request to reset has been successfully completed.")
}
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
Utility.confirmationActionSheetMsg(title: "First Alert", msg: "alertMsg", okActionTitle: "Yes", cancelActionTitle: "Cancel", success:
{
Utility.showAlertViewWithTextField(title: "Second Alert", msg: "alertMsg", okActionTitle: "Reset", cancelActionTitle: "Cancel", success: {
self.makeServiceCall()
}, cancel: {
print("Canceled by user")
})
}
}) {
print("cancel")
}
}
utility file is -
class Utility{
static func showGlobalProgressHUD(withTitle title: String?) -> MBProgressHUD?
{
let window: UIWindow? = UIApplication.shared.windows.last
let hud = MBProgressHUD.showAdded(to: window, animated: true)
hud?.labelText = title
hud?.dimBackground = true
hud?.cornerRadius = 7
hud?.margin = 30
hud?.detailsLabelFont = UIFont.boldSystemFont(ofSize: 15)
window?.isUserInteractionEnabled = true
hud?.isUserInteractionEnabled = true;
return hud
}
static func confirmationActionSheetMsg(title: String, msg: String, okActionTitle:String, cancelActionTitle:String, success: (() -> Void)? , cancel: (() -> Void)?)
{
let alert = UIAlertController(title: title, message: msg, preferredStyle: UIAlertController.Style.actionSheet)
let successAction: UIAlertAction = UIAlertAction(title: okActionTitle, style: .destructive)
{
action -> Void in
success?()
}
let cancelAction: UIAlertAction = UIAlertAction(title: cancelActionTitle, style: .cancel)
{
action -> Void in
cancel?()
}
alert.addAction(successAction)
alert.addAction(cancelAction)
UIApplication.shared.keyWindow?.rootViewController?.present(alert, animated: true, completion: nil)
}
static func showAlertViewWithTextField(title: String, msg: String, okActionTitle:String, cancelActionTitle:String, success: (() -> Void)? , cancel: (() -> Void)?)
{
let alert = UIAlertController(title: title, message: msg, preferredStyle: UIAlertController.Style.alert)
alert.addTextField { (textField) in
textField.placeholder = "Enter new pin."
}
alert.addTextField { (textField) in
textField.placeholder = "Confirm new pin."
}
let successAction: UIAlertAction = UIAlertAction(title: okActionTitle, style: .destructive)
{
action -> Void in
let textOfFirstTextfield = alert.textFields?[0].text
print(textOfFirstTextfield)
success?()
}
let cancelAction: UIAlertAction = UIAlertAction(title: cancelActionTitle, style: .cancel)
{
action -> Void in
cancel?()
}
alert.addAction(successAction)
alert.addAction(cancelAction)
UIApplication.shared.keyWindow?.rootViewController?.present(alert, animated: true, completion: nil)
}
}
it worked when i gave delay while showing hud instead of giving delay while dismissing hud
func makeServiceCall()
{
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1){
Utility.showGlobalProgressHUD(withTitle: "Loading...")
HTTPRequest.serviceReq(newPin: "129354") { (ResetModel) in
Utility.dismissGlobalHUDInMainThread()
Utility.showAlertMessageInMainThread(title: " Reset", msg: "Your request to reset has been successfully completed.")
}
}
}
How can I create a popup menu like the one present in WhatsApp?
Sorry for the dumb question, but I don't even know what to search. I'm pretty sure it's not a UIPickerView.
This is an action sheet. Here's the documentation about it in the iOS Human Interface Guidelines.
You can make one like this:
SwiftUI (iOS 15 and above)
Use confirmationDialog(). Here is the official documentation for it and here are some real-world examples, which are partially the source of the example code.
#State private var shouldShowActionSheet = false
<custom view>
.confirmationDialog("", isPresented: $shouldShowActionSheet) {
Button("Option 1") {
<handler>
}
Button("Option 2") {
<handler>
}
Button("Cancel", role: .cancel) { }
}
SwiftUI (iOS 13 and 14)
#State private var shouldShowActionSheet = false
[...]
<custom view>
.actionSheet(isPresented: $shouldShowActionSheet) {
ActionSheet(
title: Text(""),
buttons: [
.default(Text("Option 1")) {
<handler>
},
.default(Text("Option 2")) {
<handler>
},
.cancel()
]
)
}
UIKit
let alert = UIAlertController(
title: nil,
message: nil,
preferredStyle: .actionSheet
)
alert.addAction(
.init(title: "Action 1", style: .default) { _ in
<handler>
}
)
alert.addAction(
.init(title: "Action 1", style: .default) { _ in
<handler>
}
)
present(alert, animated: true)
Its UIAlertController with preferredStyle - UIAlertControllerStyle.actionSheet
https://developer.apple.com/documentation/uikit/uialertcontroller
I have a custom UIView controller that I init like so:
inputPhoneNumberView.frame = CGRect(x: 0, y: 0, width: 286, height: 73)
let alert = SAAlertView(title: "Enter Your Phone Number", message: "Enter or update your phone number and we will send you a verification code via SMS.", customView: inputPhoneNumberView)
alert.addAction("Confirm", style: .default, dismissAfterAction: false, hasLoadingIndicator: true) { () -> Void in
print("Hello!") //Testing code
}
alert.addAction(NSLocalizedString("cancel", comment: ""), style: .cancel, actionBlock: nil)
present(alert, animated: true, completion: nil)
Actions can be added to the view controller that are associated to UIButtons inside it. e.g. alert.addAction(NSLocalizedString("cancel", comment: ""), style: .cancel, actionBlock: nil
An action is a struct defined as so:
struct Action {
var title: String
var style: ActionStyle
var actionBlock: (() -> Void)?
var dismissAfterAction: Bool
var hasLoadingIndicator: Bool
}
The code block in an action is handled in this method:
fileprivate dynamic func doAction(_ sender: CustomButton) {
// Make sure the action should be allowed for default only.
let action = actions[sender.tag]
guard sender.tag >= 0 && sender.tag < actions.count else {
print("No action at that index.", logType: .Error)
return
}
if let block = action.actionBlock {
if action.dismissAfterAction {
dismiss(animated: true, completion: {
block()
})
} else {
block() // The point the crash occurs!
}
} else {
dismiss(animated: true, completion: nil)
}
}
There is an option when creating an action to dismissAfterAction i.e. auto dismiss the view controller then perform the code block. However if dismissAfterAction is false the app will crash with a malloc error. It doesn't always crash but if I repeatedly tap the button associated with that action it will eventually. Not sure what's going on here. Has anyone come across anything like this before? Seems to be a problem with the code block.
I've had likely issue, some time ago. As a fix you can change your code in this way:
//we already checked action block, so
action.actionBlock!()
//block() The point the crash occurs!
i find void return alert but string alert return not found.
trying Void to String
// return Void Alert
func returnAlert(title: String!, message: String! ,success: (() -> Void)? , cancel: (() -> Void)?) {
dispatch_async(dispatch_get_main_queue(), {
let alertController = UIAlertController(title:title,
message: "",
preferredStyle: UIAlertControllerStyle.Alert)
self.newQtyField = UITextField()
self.newQtyField.keyboardType = .NumberPad
func addTextField(textField: UITextField!){
// add the text field and make the result global
let row = self.array[Int(message)!]
textField.text = String(row.pbQty!)
self.newQtyField = textField
}
let cancelLocalized = NSLocalizedString("cancel", tableName: "activity", comment:"")
let okLocalized = NSLocalizedString("ok", tableName: "Localizable", comment:"")
let cancelAction: UIAlertAction = UIAlertAction(title: cancelLocalized,
style: .Cancel) {
action -> Void in cancel?()
}
let successAction: UIAlertAction = UIAlertAction(title: okLocalized,
style: .Default) {
action -> Void in success?()
}
alertController.addTextFieldWithConfigurationHandler(addTextField)
alertController.addAction(cancelAction)
alertController.addAction(successAction)
self.presentViewController(alertController, animated: true, completion: nil)
})
}
// bottom call fun | i think success: { () to success: { textfield , but not working how... need your help
returnAlert("title", message: "msg", success: { () -> Void in
})
{ () -> Void in
print("user canceled")
}
Very hard to understand what you are saying in your question.
You should change the method definition to this:
func returnAlert(title: String!, message: String! ,success: ((text:String) -> Void)? , cancel: (() -> Void)?) {
and change the successAction to return the value of the UITextField:
let successAction: UIAlertAction = UIAlertAction(title: okLocalized,
style: .Default) { action in
success?(text: self.newQtyField.text!)
}
I am trying add a nullable completion block to a custom function
func disPlayAlertMessage(titleMessage:String, alertMsg:String, completion: (() -> Void)? = nil){
AlertMessage.alertMessageController = UIAlertController(title: titleMessage, message:
alertMsg, preferredStyle: UIAlertControllerStyle.Alert)
AlertMessage.alertMessageController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default,handler: nil))
if completion == nil {
controller.presentViewController(AlertMessage.alertMessageController, animated: true, completion: nil)
} else {
controller.presentViewController(AlertMessage.alertMessageController, animated: true, completion: {
completion!()
})
}
return
}
When I am trying to call the above function like below
AlertMessage(controller: self).disPlayAlertMessage(CustomAlertMessages.AlertTitle, alertMsg: CustomAlertMessages.DOANoUpdate, completion: { () -> Void in
{
self.navigationController?.popViewControllerAnimated(true)
}
})
The completion block is always nil.
Here is how you define a nil'able completion
func function(completion: (Void -> Void)? = nil) {
completion?()
}
There are a few different ways in which you can call it
function() //without any argument
function({ //with parens and braces
print("I will get called")
})
function() { //with parens and braces
print("I will get called")
}
function { //without parens
print("I will get called")
}
Edit: Tested with Swift 2.0 only..
You should change your completion parameter.
Example:
func Test( completion: () -> () = {_ in }) {
completion()
}
This function can be called in two different ways:
Test() // Nothing happens
Test({ print("Completed") }) // Prints Completed
Hope this helps :)
This works for me
typealias CompletionHandler = (_ success:Bool) -> Void
func yourCompletionBlockName(completionHandler: CompletionHandler) {
//code
let flag = true
completionHandler(flag)
}
call completion block whenever you need
yourCompletionBlockName(completionHandler: { (success) -> Void in
if success {
} else {
}
})