When I used third party SCLAlertView there was a problem actually there is a problem that is I want to perform some action when the button will pressed but there is just the customization properties but I am wondering for the action scope can someone help me out?
you can use this
let appearance = SCLAlertView.SCLAppearance(
showCloseButton: false // if you dont want the close button use false
)
let alertView = SCLAlertView(appearance: appearance)
alertView.addButton("Ok Pressed") {
print("Ok button tapped")
}
alertView.showSuccess("Success", subTitle: "")
you get the detail example for add buttons and hide default close buttons property in SCLAlertView
I never used this library, however if we take a look at the Github repo of the project (https://github.com/vikmeup/SCLAlertView-Swift) we will see the following example:
alert.addButton("Show Name") {
print("Text value: \(txt.text)")
}
Where print("Text value: \(txt.text)") gets executed after clicking the button.
Related
We all know how to make a simple tap on a bar button item present a menu (introduced on iOS 14):
let act = UIAction(title: "Howdy") { act in
print("Howdy")
}
let menu = UIMenu(title: "", children: [act])
self.bbi.menu = menu // self.bbi is the bar button item
So far, so good. But presenting the menu isn't the only thing I want to do when the bar button item is tapped. As long as the menu is showing, I need to pause my game timers, and so on. So I need to get an event telling me that the button has been tapped.
I don't want this tap event to be different from the producing of the menu; for example, I don't want to attach a target and action to my button, because if I do that, then the menu production is a different thing that happens only when the user long presses on the button. I want the menu to appear on the tap, and receive an event telling me that this is happening.
This must be a common issue, so how are people solving it?
The only way I could find was to use UIDeferredMenuElement to perform something on a tap of the menu. However, the problem is that you have to recreate the entire menu and assign it to the bar button item again inside the deferred menu element's elementProvider block in order to get future tap events, as you can see in this toy example here:
class YourViewController: UIViewController {
func menu(for barButtonItem: UIBarButtonItem) -> UIMenu {
UIMenu(title: "Some Menu", children: [UIDeferredMenuElement { [weak self, weak barButtonItem] completion in
guard let self = self, let barButtonItem = barButtonItem else { return }
print("Menu shown - pause your game timers and such here")
// Create your menu's real items here:
let realMenuElements = [UIAction(title: "Some Action") { _ in
print("Menu action fired")
}]
// Hand your real menu elements back to the deferred menu element
completion(realMenuElements)
// Recreate the menu. This is necessary in order to get this block to
// fire again on future taps of the bar button item.
barButtonItem.menu = self.menu(for: barButtonItem)
}])
}
override func viewDidLoad() {
super.viewDidLoad()
let someBarButtonItem = UIBarButtonItem(systemItem: .done)
someBarButtonItem.menu = menu(for: someBarButtonItem)
navigationItem.rightBarButtonItem = someBarButtonItem
}
}
Also, it looks like starting in iOS 15 there's a class method on UIDeferredMenuElement called uncached(_:) that creates a deferred menu element that fires its elementProvider block every time the bar button item is tapped instead of just the first time, which would mean you would not have to recreate the menu as in the example above.
This seems like maybe a simple question, but I'm looking for a way to make one (or more, I suppose) buttons in an ActionSheet in SwiftUI to be disabled. For example, if something is illegal based on the state of the app, I don't want to let the user click the button - indeed, I might change the text to explain why it's disabled.
Xcode autocomplete isn't really turning anything up for me, and the docs (here and here) aren't giving me anything. Is my best/only option simply not putting the button in the list of buttons in the ActionSheet? (And kinda letting the user infer why it's not there...)
Thanks so much for any advice!
My simple code:
private func getActionSheet() -> ActionSheet {
let buttons: [ActionSheet.Button] = [
.default(Text("This is fine...")) { foo() },
.default(Text("This is forbidden - disable me!")) { foo() },
.cancel()
]
return ActionSheet(title: Text("Do it!"), buttons: buttons)
}
For nondestructive actions you can simply show a Menu. They support the .disabled(true) modifier.
var body: some View {
Menu("Do it!") {
Button("This button works fine", action: { })
Button("This one is disabled", action: { })
.disabled(true)
}
}
I have a view in the storyBoard with a button saying "You are cool". After calling that view with performSegue the view appears but the button text is not there. When I programmatically set the value of the text to "You are cool" in viewWillAppear still nothing happens. But if the text which I define programatically does not match with that in the MainStoryBoard the text is display. What could cause that strange behaviour ?
EDIT:
I encountered with the same problem again. Here is my code snippet I am using :
#IBAction func loginAction(_ sender: Any) {
print("loginIsExecuting")
Server.Login(email: inputEmail.text!, password: inputPassword.text!, handlerDone:{
print("Logged in successfully")
}, handlerFailed: {
print("===Failed to log in ")
self.view.backgroundColor = UIColor.red
})
}
After handlerFailed the text appears in the console, but the background is changed after some time (generally 30 seconds)
please first check your Button Font Color and Button Background Color.. If this will not helpful then share your code..
I feel button title is not properly set. Please make sure you are using setLabel for button title set.
Ex :[btnName setTitle:#"Test" forState:UIControlStateNormal];
Using DispatchQueue.main.sync solved my problem
Server.Login(email: inputEmail.text!, password: inputPassword.text!, handlerDone:{
print("Logged in successfully")
}, handlerFailed: {
print("===Failed to log in ")
DispatchQueue.main.sync {
self.view.backgroundColor = UIColor.rer
}
})
I have been using the EasyTipView swift library for a couple of days, however i am not able to make a particular tip dismiss, i can only make the tip disappear if i tap on it.
This is my sample code:
// Mostramos el tooltip al usuario
EasyTipView.show(forItem: self.buttonRefresh, text: "Refresh Button Tip".localized)
The tip will appear below the "buttonRefresh" element in my navigation bar, what i would like to accomplish is that tapping on that same button, the tip disappears.
You can find the library here: https://github.com/teodorpatras/EasyTipView
Thanks in advance
Alex
EasyTipView has a member function called dismiss. But to be able to use that you need to have an EasyTipView member variable in your class.
Once you have that , you can call dismiss to remove the EasyTipView window.
var easyTipView : EasyTipView!
...
...
func handleRefresh()
{
if self.easyTipView == nil
{
self.easyTipView = EasyTipView(text: "Hello There")
self.easyTipView.show(animated: true, forView: self.buttonRefresh!, withinSuperView: nil)
}
else
{
self.easyTipView.dismiss()
self.easyTipView = nil
}
}
Simply, you can do something like this:
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.tipView?.dismiss()
self.tipView = nil
}
I have an app with some buttons, when those buttons are pressed the image on them should change. I assume that the TouchUpInside runs when you tap and remove the finger while still holding inside the area of the element, however it only works rarely and I'm not sure why.
The reason I use TouchUpInside instead of TouchDown is because I want the user to be able to cancel the action.
I'm sorry if I've misunderstood anything about those events and if this has already been asked. I couldn't find an answer to my problem searching the web.
//The IBAction is set to trigger on TouchUpInside
#IBAction func action11(sender: UIButton) {
setTile(sender)
}
func setTile(sender: UIButton) {
if turn {
print("O's turn")
sender.setImage(xTile, forState: .Normal)
turn = false
}
}
EDIT: Added the necessary code
There are some properties of UIButtons which you can use to achieve what you want.
You can use Default and selected state of uibutton to set two different images.
In XIB select state "Default" and assign default image to that state again select state to "Selected" and assign image which you want after button section.
and add following line in button selection method.
-(IBAction)buttonTapped:(UIButton *)sender{
sender.selected = !sender.selected;
}
Your understanding is correct, you need to use touchUpInside.
I assume you are trying to create a button that has a toggle function. On one touch you want the button to have the value Say "X" and when touched again the button has a value "O".
Take a look at this code below, this should do the job.
class ViewController: UIViewController {
var isButtonPressed = false{
// Adding a Property Observer, that reacts to changes in button state
didSet{
if isButtonPressed{
// Set the Value to X.
}else{
// Set the Value to O.
}
}
}
#IBAction func changeButtonValue(sender: UIButton) {
// Toggle the button value.
isButtonPressed = !isButtonPressed
}
}
If you don't set turn=true after the first time, this code is executed it will be executed only one.
if turn {
print("O's turn")
sender.setImage(xTile, forState: .Normal)
turn = false
}
Check if the button frame is large enough to get finger touch.
Apple says at least 35x35 pixel.