UIViewController text won't be displayed after performSegue - ios

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
}
})

Related

How to make editingDidEnd not trigger when user tap back navigation in swift?

Currently i have an issue in editingDidEnd method of swift textfield.
I have a module inside editingDidEnd func to check some validation in B ViewController. If the validation is wrong then it shows popup / alert.
In this case, while user is typing and still focus on the textField, users tap back on navigation bar. it makes editingDidEnd function is also called. So the page is showing A ViewController and also showing pop up.
Is there any workaround to handle this issue? I don't want the alert is showing when i tap back in navigation bar. My expectation is if user press back on navigation. it's not call editingDidEnd function
Thanks Before
e.g.
B View Controller
extension bviewcontroller: textfielddelegate {
func editingDidEnd(_ value: String, textField: SearchTextField) {
//showingalert
}
}
You can add a guard statement to your editingDidEnd method like below.
func editingDidEnd(_ value: String, textField: SearchTextField) {
guard navigationController?.topViewController is bviewcontroller else {
print("DON'T SHOW ALERT")
return
}
print("SHOW ALERT")
}
I have tested the above code with a simple view controller containing a UITextField. However, your delegate method appears to be something different than the standard UITextFieldDelegate's textFieldDidEndEditing(_:) method.
Otherwise I would have suggested that you could also try experimenting with the textFieldDidEndEditing(_:reason:) method.

SCLAlertView Button

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.

How do I verify and assert if an animation is playing after clicking a button in XCUITest?

I can't seem to find any information when it comes to animation testing on XCUITest.
The scenario that I am testing is that:
When a button is pressed
Then animation will be displayed on the icon
How do I do this?
I recommend setting a meaningful accessibilityValue on the button when it it's animating, which will let voice over users that something is happening too -- then you can check the value property of the corresponding XCUIElement in your test.
// product code
#IBAction func buttonPressed(_: UIButton) {
self.button.accessibilityIdentifier = "MyButton"
self.button.accessibilityValue = "is animating"
// start animating your button
}
// Test code
let button = self.app.buttons["MyButton"]
button.tap()
XCTAssertEqual(button.value as! String, "is animating")

How can I add a Clear Button to my Calculator app in Xcode?

enter image description hereI'm a beginner and made a prototype of a calculator app in Xcode. It works great. But to make it more easier to use and add features I thought of adding a Clear Button too. I need help in doing that with my existing code. Please, if possible, give me all steps in detail.
The Code-
The User Interface-
How can I add a All Clear Button to it so the displayed number is 0?
Please try the given below code
#IBAction func ClearButtonAction(sender: AnyObject)
{
your_TextfieldName.text = ""
}
"where Textfield is Outlet Of Your Textfield "
#IBAction func Tap_Save(_ sender: UIButton)
{
TextField.text = " "
}

Weird UIButton behaviour in Today Widget

I added a today extension to my app. I edited the vanilla widget a little bit and made it look like this:
Please notice the UIButton titled "Went 1st". When it gets pressed (touched up inside to be exact), it triggers this action:
#IBAction func coinChanged(sender: UIButton) {
if sender.titleLabel?.text == "Went 1st" {
coin = true
sender.titleLabel!.text = "Went 2nd"
} else {
coin = false
sender.titleLabel!.text = "Went 1st"
}
}
It basically alters between two states, changing its title and a variable accordingly.
Here is the problem though - when I press it, it indeed changes its title, but immediately changes it back, ending up on the same title as it had initially. My first thought was the action gets called twice after a press, but when I checked with print I found out it gets called only once. Sometimes the prints didn't even show up in console, but that's a different story.
So, that's one problem. There's one more, though - when I press the button, the whole widget gets misplaced. To know what I mean, look at the first picture (that's the widget before any presses) and now on this one (after the button gets pressed):
You can see that the borders are now on the very edge of TodayView. For reference, here are the constraints of the first segmented control:
Edit: Here are the constraints for "Went 1st/2nd" button:
Edit 2: Be sure to tell me what's wrong if you downvote, so I can avoid making the same mistakes next time
The problem is you should not be setting the button text like that. The title label is primarily used to set text size, font, color, etc. To set the title use something like this:
sender.setTitle("Button Title", forState: UIControlState.Normal)
So the new ib action should look like:
#IBAction func coinChanged(sender: UIButton) {
if sender.titleLabel?.text == "Went 1st" {
coin = true
sender.setTitle("Went 2nd", forState: UIControlState.Normal)
} else {
coin = false
sender.setTitle("Went 1st", forState: UIControlState.Normal)
}
}

Resources