I would like to print a page in my app made up of textfields and 3 labels onto paper. The issue is I have would like to hide the "print" button when it's pressed so that it doesn't show up on paper.
Can someone show me some example code for this?
[button addTarget:self action:#selector(printPage:) forControlEvents:UIControlEventTouchUpInside];
- (void)printPage:(id)sender
{
// Your print command code
// If you want to set the button visible again, you can implement completion handler callback using block implementation
((UIButton *)sender).hidden = YES;
}
Related
I'm trying to get back into iOS programming, but it's been a while so I've kind of lost it.
I'm trying to make a simple single view game. I have four buttons. Button 1, button 2, button 3, and button 4.
What I need to happen is: When I press a button, the button gets selected, and then I want the user to be able to press any other button, which will then lead to an if statement that I'm gonna code later.
Also, I want the user to be able to unselect the previously selected button by just clicking it again. So that he or she can select another button of their choice, to start with.
I'd really appreciate this simple setup of code, since I have no clue how I'm gonna do it.
The idea is to change the state of the other buttons in the selector (IBAction) for the 1st button after it is pressed and control goes back to the user.
Whatever happens to the 3 button seems to imply that you just want them set or unset then evaluated at the same time - the more appropriate class to use is a UISwitch so you can set - unset them all at once without triggering an action. Of course you may use buttons if you wish but typically each button will trigger a selector instead of your code patiently waiting for the user to make up his mind in setting the options before your code starts again.
In case you forgot:
- (void)viewDidLoad {
[super viewDidLoad];
[Button1 addTarget:self action:#selector(Button1_pressed:)
forControlEvents:UIControlEventTouchUpInside];
[Button2 addTarget:self action:#selector(Button2_pressed:)
forControlEvents:UIControlEventTouchUpInside];
Button2.enabled = NO;
[Button3 addTarget:self action:#selector(Button3_pressed:)
forControlEvents:UIControlEventTouchUpInside];
Button3.enabled = NO;
.... }
-(IBAction) Button1_pressed:(ID) sender {
Button2.enabled = YES;
Button3.enabled = YES;
Button4.enabled = YES;
}
the above functionality can be easily achieved by managing flag to know if button is already selected or not.
set slected=1; when user press the button,
selecting again will change flag to selected=0 will help you to get selected status of button easily.
Take a BOOL variable isSelected.
isSelected = ! isSelected;
if(isSelected){
button.enabled = NO;
}
else{
button.enabled = YES;
}
Is it possible that I can change the background image of some button when I click on another button?
I basically have 10 buttons, and I want the user to know which button is currently clicked. So what happens is each button has 2 images. One for selected and one for not selected.
I want to create a function which will reset the background images of all the buttons. And in the method for each button, I will add this reset function and then change the background image of that specific button.
Can someone suggest how is that possible?
I know how to change the background image of a button when that button is clicked.
This how my buttons look:
- (IBAction)posterButton:(id)sender {}
- (IBAction)colorInvertButton:(id)sender {}
etc..
Look up the documentation for UIButton: configure each button like this:
[button setControlImage:selectedImage forState:UIControlStateSelected];
[button setControlImage:unselectedImage forState:UIControlStateNormal];
^^this can also be done in interface builder btw.
There are also the states UIControlStateNormal | UIControlStateHighlighted and UIControlStateSelected | UIControlStateHighlighted, but this is optional.
A button also has a selected state, inherited from UIControl.
If you want to have only one selected button at a time, try this (_selectedButton should be declared as an instance variable):
- (UIButton *)selectedButton {
return _selectedButton;
}
- (void)setSelectedButton:(UIButton *)b {
if(b != _selectedButton) {
_selectedButton.selected = NO;
b.selected = YES;
_selectedButton = b;
}
}
I have a few UIButtons at the bottom of my app's main view. These buttons intermittently don't highlight when a user taps them but their target methods always get called. I've discovered it's Control Center's gesture recognizer getting in the way of UIButton's highlighting. If I move the containing view up toward the middle of the screen everything functions as designed.
The issue is reported here https://devforums.apple.com/message/865922
As a workaround I've tried setting the highlighted state by hand with the target method. This seems to have the same effect of allowing the UIButton to highlight normally.
Any ideas how to work around this without redesigning these controls to appear elsewhere in the app?
Perhaps I use a standard view and add all the methods for touch interaction by hand? How would I do that? Is it even worth exploring?
I've found a pretty simple workaround for this. Using standard properties like .highlighted = YES and .selected = YES doesn't seem to work within that bottom band. Instead of setting the highlighted state, I just set the background image of the button to the highlighted state with an unperceived delay BEFORE we call the final method.
[self.stopRecordingButton setImage:[UIImage imageNamed:#"stopRecordingButton"] forState:UIControlStateNormal];
[self.stopRecordingButton setImage:[UIImage imageNamed:#"stopRecordingButton-highlighted"] forState:UIControlStateHighlighted];
[self.stopRecordingButton addTarget:self action:#selector(stopRecordingDelay) forControlEvents:UIControlEventTouchUpInside];
-(void)stopRecordingDelay
{
[self.stopRecordingButton setImage:[UIImage imageNamed:#"stopRecordingButton-highlighted"] forState:UIControlStateNormal];
[self performSelector:#selector(stopRecording) withObject:nil afterDelay:0.025f];
}
- (void)stopRecording
{
[self.stopRecordingButton setImage:[UIImage imageNamed:#"stopRecordingButton"] forState:UIControlStateNormal];
//Do real stuff
}
I recently ran into the same problem and search everywhere for an answer. This is what worked for me. It was a combination of two things, the UINavigationController back swipe gesture and the iOS 7 control center gesture (up swipe from bottom of the screen).
Disable the back swipe gesture if on a UINavigationController:
in viewDidLoad:
if ([self.navigationController respondsToSelector:#selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
Set the control center gesture to only show an up arrow instead of showing the control center first. You can do this by overriding the following UIViewController method:
- (BOOL)prefersStatusBarHidden {
return YES;
}
Hope this helps!
I've posted a fix for this issue that brings the highlight back described in this question. The highlight is fixed by subclassing UIButton and overriding pointInside: to catch touch events
If you have a button that's covering the whole bottom of the screen you may run into an issue where only the left part has this delay.
In order to normalize feedback time for the whole button one might use the following solution
(an improved version of Aaron Shekey's):
NSDate *touchDownTime;
- (void)touchDown
{
self.alpha = 0.7;
touchDownTime = [NSDate date];
}
- (void)touchUpInside
{
// basically at least 80ms feedback is guaranteed this way
// note: timeIntervalSinceNow returns negative
NSTimeInterval feedbackTimeLeftToShow =
MAX(0.08 + [touchDownTime timeIntervalSinceNow], 0.001);
[self performSelector:#selector(touchUpInsideAfterFeedback)
withObject:nil
afterDelay:feedbackTimeLeftToShow];
}
- (void)touchUpInsideAfterFeedback
{
self.alpha = 1;
}
note: performSelector may do well with negative delay values, but better be safe than sorry
I have a custom UIButton. To create a custom background color when highlighted I the button (self) as an observer to three events:
[self addTarget:self action:#selector(didTapButtonForHighlight) forControlEvents:UIControlEventTouchDown];
[self addTarget:self action:#selector(didUnTapButtonForHighlight) forControlEvents:UIControlEventTouchUpInside];
[self addTarget:self action:#selector(didUnTapButtonForHighlight) forControlEvents:UIControlEventTouchUpOutside];
The first two work beautifully, and as long as I touch up inside the button the background gets set back to normal. However if I touch up outside the button the method didUnTapButtonForHighlight never gets called and the background remains the highlighted color. My code is a modified version of the code in Ondrej's answer to this question. Why is it not working? Thanks in advance.
You should also test for UIControlEventTouchCancel in case you touch up way outside.
Is there any way I can get hold of which button I have pressed?
The buttons are created programmatically using a for loop.
I have a scroll-view of images (i used buttons for the images) where the images are taken by the user from the camera. So after the user takes a picture, the "new" picture will appear in the scrollview with the "old" pictures. The pictures are shrink into smaller sizes so what I want is that when I click the button (of any image) the image will pop up in another view in the actual size.
The button of images is created using a for loop. However, I do not know how to get hold of which button the user press. For now, when i press the button (regardless of which image/button), the last picture that is taken will always show up.
Thanks for you time.
You can use the tag property of button.
When you create the button, put a tag to each image buttons.
for(int i = 0 ; i < your_no_images ; i++){
UIButton *button = [UIButton <yourbuttontype>];
-----
button.tag = i;
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[yourView addSubView:button];
}
Now in the button click action..
-(void)buttonClicked:(UIButton*)sender{
//if you has an array of UIImage's
UIImage *clickedImage = (UIImage *)[yourImageArray objectAtIndex:sender.tag];
}
IBAction methods, like those triggered upon clicking a button, include a sender argument. sender is the object that triggered the action, for example the button that was clicked. Like this:
- (IBAction)buttonWasClicked:(id)sender
{
NSLog(#"The %# button was clicked", (UIButton *)sender.currentTitle);
}