UIButton setImage didn't change the image of button immediately - ios

I am using setImage() to set image on UIButton, but it doesn't change the image immediately. To show the image, I need to click the button or other button again. How to fix this problem?
block.setImage(UIImage(named: "Oimage.png"), for: UIControl.State.normal)

Problem is not how you set image, but where you set image. You probably set it inside IBAction for button so you will have to wait then button will be pressed.
One way is to set it inside viewDidLoad of your view controller
override func viewDidLoad() {
...
block.setImage(UIImage(named: "Oimage.png"), for: .normal)
}
... or if you're using storyboard, you can set it via storyboard. If you're creating your button programmatically, set it inside initializating lazy variable

You can set image in button action like this:
#IBAction buttonTapped(_ sender: UIButton) {
let testImage = UIImage(named:"imageName")
block.setImage(testImage, for: .normal)
}

just change Type of your button to custom

Related

Using a UIButton into Navigation Bar using StoryBoard

Essentially I have a UIButton created in StoryBoards that I would like to drag into the navigation bar. This UIButton has an action associated with it when touched but all touch events stop working when I drag this UIButton into the navigation bar.
Is there additional code I need to the IBAction item when doing this?
One of the main reasons I want to add a UIButton instead of a BarButtonItem to the navigation bar is simply because it allows me to add a background color and curve the edges of the button.
I don't think you can do it in the storyboard. You have to do it in code, using the init(customView:) initialiser.
let myCustomButtonWithBackgroundsAndStuff = UIButton(type: .custom)
...
// this is the equivalent of connecting the IBAction, in code form, in case you didn't know
myCustomButtonWithBackgroundsAndStuff.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
let barButtonItem = UIBarButtonItem(customView: myCustomButton)
navigationItem.rightBarButtonItem = barButtonItem
// or append to rightBarButtonItems if you have other bar button items
...
#objc func buttonAction() {
...
}

Linking up a button through a segue when using material.io

Im a beginner in swift and I am creating a login screen using Material.io in Swift and my UI looks like this .
However i dont see anything on my main.storyboard
I am used to creating a segue manually and then using
performSegue(withIdentifier: <#T##String#>, sender: <#T##Any?#>)
to use the segue but now since i dont see the button i'm unable to use the segue on clicking the 2 buttons Login and Register.
Also, usually from main.storyboard you have an IBOutlet or IBAction block to trigger certain features such as do something on a button click.
How does that work when you use MDCButton for instance. How do you link up the button to an action such as trigger a segue or update a text field.
Thank you.
I have declared my button like this
let nextButton: MDCButton = {
let nextButton = MDCButton()
let containerScheme = MDCContainerScheme()
nextButton.applyTextTheme(withScheme: containerScheme)
nextButton.setTitleColor(.white, for: .normal)
nextButton.translatesAutoresizingMaskIntoConstraints = false
nextButton.setTitle("CREATE NEW ACCOUNT", for: .normal)
nextButton.addTarget(self, action: #selector(didTapNext(sender:)), for: .touchUpInside)
return nextButton
}()
And i am calling it like this
#objc func didTapNext(sender: Any) {
self.performSegue(withIdentifier: "toRegistration", sender: self)
self.dismiss(animated: true, completion: nil)
}
I have a segue linking the 2 view controllers.
I can't see your code and don't have enough rep to comment, but it looks like you are building your view programmatically. To see when the button receives touchUp as you would from an outlet, you need to add a target. Try something like this:
func functionWhereYouCreateTheButton() { (PROBABLY YOUR VIEW DID LOAD)
let loginButton = UIButton()...
...
...
...
loginButton.addTarget(self, action: #selector(self.buttonTapped), for: .touchUpInside)
}
func buttonTapped() {
self.performSegue(withIdentifier: "SEGUEID", sender: self)
}

Swift 4 - How to activate segue programmatically from button nested in UICollectionViewCell

I am trying to activate a segue between a UICollectionView, subview of a UIViewController, and a separate ViewController (called RegisterController). The CollectionView is all set up programmatically, and there is a button on the last cell. However, I can't do the typical performSegue(_:withIdentfier) because the button is in the CollectionView. So, I have set up a method in the ViewController holding the CollectionView to performSegue(_:withIdentifier). I set up an instance in the CollectionViewController to call the method:
lazy var registerButton: UIButton = {
let btn = UIButton(type: .system)
btn.setTitle("Don't have an account?", for: .normal)
btn.setTitleColor(lighterOrange, for: .normal)
btn.addTarget(self, action: #selector(registerScreenAppear), for: .touchUpInside)
return btn
}()
#objc func registerScreenAppear(){
let vc: ViewController = ViewController()
_ = vc.toRegister()
}
The method in the ViewController is
func toRegister(){
performSegue(withIdentifier: "toRegister", sender: self)
}
I am getting the call stack:
NSInvalidArgumentException', reason: 'Receiver (<audible.ViewController: 0x7fe780724770>) has no segue with identifier 'toRegister''
I know I'm probably doing something completely wrong, but I've searched and searched and can't find a solution. Any help is appreciated, thanks
(Yes, I have configured the segue and gave it an identifier in the storyboard, but the reason I have to do this through code is because the CollectionView and the button are created programmatically, so I can't use the Interface Builder)
call performSegue in registerScreenAppear method or call self.toRegister() like
#objc func registerScreenAppear(){
self.toRegister()
}
OR
#objc func registerScreenAppear(){
performSegue(withIdentifier: "toRegister", sender: self)
}
Note: use later one as it will remove the extra function call i.e. toRegister().

UIButton cannot clicked after adding to another uiview

I try to make the slideshow using uiscrollview, it will be triggered by slide event from uiscrollview.
Everything works fine except I try to add uibutton to the view, the button can be added and shown in the view but when I try to click the button, it does not triggered the function that I have stated in the addTarget function.
Anyone knows how to triggered the function assigned to the button?
First Create func:
func messagePrint(_ sender:UIButton) {
print("function called")
}
Then addTarget into UIButton:
btnDemo.addTarget(self, action: #selector(messagePrint(_:)), for: .touchUpInside)
In Above code btnDemo is UIButton Outlet.

Show viewController swift

I have a tableView and i made its cells custom and added a UIButton to the cell
i want when i press the button to popOver a new ViewController and in it the text in the cell i clicked the button in it. I don't want to use didSelectRowAtIndexPath func, i want to use the event of the button in each cell.
I tried to connect the button with the new viewController using popOver segue but the build fails because it is dynamic.
Please help and make the answer in swift.
Thanks in advance
You shouls set target to button inside the cell in cellForRowAtIndexPath function
cell.button.addTarget(self, action: "showPopupPage:", forControlEvents: UIControlEvents.TouchUpInside)
create a function
func showPopupPage(sender: UIButton) {
let view = button.superview!
let cell = view.superview as! custom cell
// you can get cell contents here
// call your popupview pass the value
}
Example=>
Swift - UIPopoverController in iOS 8

Resources