I'm trying to check if an image is assigned to a button. So far I use this:
if MyButton.currentImage.isEqual(UIImage(named: "Black")) {
//do something here
}
But I get error message:
Use of unresolved identifier"MyButton"
even though I set the label of the button to MyButton.
Seems like you do not have MyButton defined in your code. You should create a variable of type UIButton in which you store the button (connect it with outlet in storyboard or initialize it with code). Setting a label to the button as you mentioned will not do anything.
Related
I was trying to make a simple tic-tac-toe app in Swift, so I set up 9 buttons with tags from 1 to 9 and call setImage to set noughts or crosses. This is working as intended.
The problem comes when trying to reset the board, where I call this piece of code:
for i in 1..<10 {
if let button = view.viewWithTag(i) as? UIButton {
button.setImage(nil, for: .normal)
}
}
This should remove the image from the button, but it does nothing. The button is set to Custom in the storyboard, and the tags are indeed assigned as they should. I also tried getting an outlet for one of the buttons and calling setImage(nil, for: .normal) in that one and it didn't work either.
I even created a new project with just a button where I call setImage, and it is indeed working for non-nil images but not with nil as value.
Has Apple changed the way of removing images? Another question(When button pressed, button.setImage(nil) doesn't work) seems to have the same problem, but if I work with isHidden I can no longer click on the buttons, and it should not be the workaround, looks very hacky to me.
Is this a bug on iOS? Did the implementation change or something?
Thank you.
This seems like it may be a bug in iOS 15, but it still works if use the new UIButton Configuration API you set its configuration's image instead:
button.configuration?.image = nil
Be sure to also configure your button's image through its configuration property when you want it to have an image on iOS 15, too
I'm making a button via code.
I have the following line of code to trigger a method when the button is clicked:
[imagesButton addTarget:self action:#selector(photoClicked:) forControlEvents:UIControlEventTouchDown ];
The problem is that I can't pass data to the method through the #selector;
If the button had a background image name "background.png" how would I go about sending the name of the background image to the method when the button is clicked?
This all HAS to be through code.
Thanks!
-Shredder
there must be a way to comment on an answer, but I don't know what it is. Anyway, Gobot above me forgot to write (id) before sender in the method declaration. Otherwise Gobot's example is ok.
Well if you're trying to change a property of the button whose sending the message, your selector should have a parameter of sender, which is a pointer to the object that called it, which is your button in this case. For example:
- (void)photoClicked:(id)sender {
UIImage bg = [sender currentBackgroundImage]
}
In my iPhone app,
I am working with XIB file.
There is a segmented control,
But there is no IBAction associate with any field,
Whenever I am selecting or touching an segment which method from my code will be called ?
Can I trace or see it in the xcode.
Actually I am looking through some complex code and could not catch the methods by breakpoints ....
Like this Question of stackoverflow.
How to print or see method call stack in xcode?
If you want to see the target method you can right click the controller and see the the method associated with segmented control. If you want to add add some target method with segment controller then do this -
Right click the controller
drag the plus button of desired action(like valueChanged ) to .h file,one popup will come
enter the action name click connect
now go to .m file and write your logic in action
First of all, you do not associate any IBAction with the segmented control's "fields". You associate an IBAction with the control itself.
So, to get you started:
Create an IBAction like :
-(IBAction)segmentChanged:(UISegmentedControl *)sender;
Bind that action with the "Value Changed" event of your UISegmentedControl object
Now every time the segment changes, this method will be fired.
To get the selected index use:
uint selectedIndex = [sender selectedSegmentIndex];
I have a Button1 which has IBAction. Also I set target and action for my button
- (void)setTarget:(id)target action:(SEL)action {
[self.Button1 addTarget:target action:action
forControlEvents:UIControlEventTouchUpInside];
}
So when I pressed the button firstly IBAction did what he should, and than action that I set to button. Is that order always be like that ?
If you are loading you view or view controller from a nib file then yes the pattern will always be the IBAction even first followed by the target you have added to the button.
In effect adding an IBAction in Interface Builder is really just telling IB to call ["UIControl" addTarget:"id" forControlEvents:"UIControlEvent"], and you can add multiple targets to a UIButton.
In effect your code will load everything from the NIB file first (if you are using initWithNib:named:), so this will call the addTarget function on the button first with the action you have specified in Interface Builder, then at some later point the setTarget function you have above will get called, which will add another target action to the button. A UIControls targets are stored in an array which is accessed in order and will trigger if control events are met in the order they were created in.
If you look in the header file for UIControl (the super class for UIButton) you will see that NSMutableArray* _targetActions is an array. So the order is guaranteed to fire like this unless you reorder this array after it is created at some point.
I have a toolbar, in which is placed a UIBarButtonItem. The selector is targeted at a custom view of mine; a method with this signature:
-(IBAction)pop{code}
However, clicking it does not cause any action to occur. The buttonitem doesn't appear to respond to the click either, it just stays gray.
Linking a UIButton's TouchUpInside event to the pop method is fine, it operates the method and displays the popover. But as soon as I connect the BarButtonItem's selector to it instead, it stops responding.
Make sure the selector has no colon after it - #selector(pop). If you use #selector(pop:) it expects a (void)pop:(id)sender { ... } function.