I am adding an image in IBAction, I want to restore the old image back once the button is tapped.
It is not the image in the UIButton, it's the image in the UIImageView
- (IBAction)findMe:(id)sender
{
[self.imageView setImage:[UIImage imageNamed:#"image2"]];
}
I want to restore#"image1". immediate once button is tapped
some thing i need is some thing similar to
button setImage:[UIImage imageNamed:#"clicked.png"] forState:UIControlStateSelected | UIControlStateHighlighted];
but i don't want to change Button Image, but change UIImageView Image when clicked
You can use the following code
[yourButton addTarget:self action:#selector(touch2:) forControlEvents:UIControlEventTouchDown];
[yourButton addTarget:self action:#selector(touch3:) forControlEvents:UIControlEventTouchUpInside];
Now the selectors
-(void)touch2:(UIButton *)send
{
imageView.image= [UIImage imageNamed:#"image1"];
}
-(void)touch3:(UIButton *)send
{
imageView.image= [UIImage imageNamed:#"image2"];
}
Try it out:
- (IBAction)findMe:(id)sender
{
if (self.imageView.image) {
// store your old image here
NSLog(#"%#", self.imageView.image)
}
[self.imageView setImage:[UIImage imageNamed:#"image2"]];
}
Try this first create gesture for UIButton
UILongPressGestureRecognizer *btn_LongPress_gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handleBtnLongPressgesture:)];
[buttonFindMe addGestureRecognizer:btn_LongPress_gesture];
When you press and release the button it calls this method.
-(void)handleBtnLongPressgesture:(UILongPressGestureRecognizer *)recognizer{
//as you hold the button this would fire
if (recognizer.state == UIGestureRecognizerStateBegan) {
//Change your image 1
}
//as you release the button this would fire
if (recognizer.state == UIGestureRecognizerStateEnded) {
//Change your image 2
}
}
Related
I have a button that has a selected and non-selected state.
My target action code for the button is this:
NSArray *targets = [myButton actionsForTarget:self forControlEvent:UIControlEventTouchUpInside];
if ([targets count] == 0) {
[myButton addTarget:self action:#selector(save:) forControlEvents:UIControlEventTouchUpInside];
When the button is pressed, it goes to selected state in the method.
If the button is selected, a different action should be called. Is there any unintended consequence to doing it this way or is there a way to add actions with UIControlState instead?
if (myButton.selected == NO){
[myButton addTarget:self action:#selector(save:) forControlEvents:UIControlEventTouchUpInside];
}
else{
[myButton addTarget:self action:#selector(delete:) forControlEvent:UIControlEventTouchUpInside];
}
-(IBAction)myButton:(id)sender
{
UIButton *btn = (UIButton*)sender;
btn.selected = !btn.selected;
if(btn.selected)
{
//Do whatever you want when button is selected
[self delete];
}
else
{
//Do whatever you want when button isDeselected
[self save];
}
}
Your approach might be confusing. You can use like this.
[myButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
- (void)buttonPressed:(UIButton *)sender
{
if(sender.selected)
[self save];
else
[self delete];
}
-(void)save
{
// your code
}
-(void)delete
{
// your code
}
i believe that you are wanting something like toggle button. if i am right use yourbutton.currentTitle to follow the functionality. i.e. if [youtbutton.currentTitle isEqualToString: #"save]" then you should perform save and set that to delete and vice verse
My button is disabled but I want to show a message box when user clicks on it. How can I do it? Also I would like to mention that UIbutton is added on UITableview, and I tried the code below, but it always goes to else in DisableClick() function.
In cellForRowAtIndexPath:
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(180, 6, 30, 30)];
[button setTag:4000];
[button addTarget:self action:#selector(click:event:) forControlEvents:UIControlEventTouchDown];
UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(DisableClick:)];
[self.view addGestureRecognizer:gesture];
[gesture release];
[cell.contentView addSubview:button];
In DisableClick()
- (void)DisableClick:(UIButton*)sender {
UITapGestureRecognizer *recognizer = (UITapGestureRecognizer *)sender;
CGPoint pt = [recognizer locationOfTouch:0 inView:market];
if (CGRectContainsPoint(market.bounds, pt)) {
NSLog(#"Disabled button tapped");
}
else
{
NSLog(#"go out"); // it's always go to here
}
}
Rather than desabling the button keep it enable. Change the image of button like disable image. Put a flag variable when button is tapped use this flag variable to run the code of enabled button and disabled button
BOOL btnFlg;//initialize it true or false on cellForRowAtIndex... method
- (void)DisableClick:(UIButton*)sender
{
if(btnFlg)
{
btnFlag=NO;
NSLog("button disabled");
}
else
{
btnFlag=YES;
NSLog("button enabled");
}
}
Why you are initialize a tap gesure for this button. You set the selector in ur addTarget
Maintain a btn boolean for the button status . Change the status whenever you want to disable or enable.
BOOL btnEnabled;
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(180, 6, 30, 30)];
[button setTag:4000];
[button addTarget:self action:#selector(DisableClick:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
Make the target function as
- (void)DisableClick:(UIButton*)sender {
//set the function that you needed when the user tap on this button here.
if(btnEnabled) {
// here invoke the functions you needed when the button enabled
} else {
UIAlertView *msg = [[UIAlertView alloc]initWithTitle:#"Alert" message:#"Your button is disabled" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[msg show];
}
}
You will need to subclass UIButton and implement
touchesBegan:withEvent: and touchesEnded:withEvent: methods for your purpose.
I'm using an iCarousel where i display some buttons. But how do I get to another view when the button is tapped?
I have this code already:
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
return 10;
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIButton *button = (UIButton *)view;
if (button == nil)
{
UIImage *image = [UIImage imageNamed:#"page.png"];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setBackgroundImage:image forState:UIControlStateNormal];
button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
}
return button;
}
- (void)buttonTapped:(UIButton *)sender
{
NSInteger index = [carousel indexOfItemViewOrSubview:sender];
//Action
}
How do I get to another view when the button is tapped? Can I use the prepareForSegue method here as well?
Thanks for your help!!
Sure, you just have to drag and create a segue from the first viewController to the second, and name it "segue0" for instance.
-(void)buttonTapped:(UIButton*)sender
{
NSInteger *index = [carousel indexOfItemViewOrSubview:sender];
if(index = 0)
{
[self performSegueWithIdentifier:#"segue0", sender:self];
}
//else if etc.., or better yet, ditch the if, do the checking in 'prepare' as explained below
}
-(void)prepareForSegue:(UIStoryboardSegue*) segue sender:(id)sender
{
YourNextViewController *nextView = [segue destinationViewController];
//Do stuff to your next view with "nextView"
}
If they are all going to the same viewController, but by sending different data in prepareForSegue, then you can ditch the if-statement, and send sender instead of self as the sender in [self performSegue..], and check in prepareForSegue.. which button is the sender(or moving your NSInteger *index... line down here)
I have three views and three buttons each button toggles the view (hidden = yes/no)
- (IBAction)switchOne:(id)sender {
[_firstPage setHidden:NO];
[_secondPage setHidden:YES];
[_thirdPage setHidden:YES];
}
- (IBAction)switchTwo:(id)sender {
[_firstPage setHidden:YES];
[_secondPage setHidden:NO];
[_thirdPage setHidden:YES];
}
- (IBAction)switchThree:(id)sender {
[_firstPage setHidden:YES];
[_secondPage setHidden:YES];
[_thirdPage setHidden:NO];
}
I want to set the background of the button depending if the view is hidden or not.
I have tried this but without results:
if (_firstPage.hidden == NO)
{
UIImage *buttonImage = [UIImage imageNamed:#"currentPage.png"];
[_pageOneButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:_pageOneButton];
} else if (_firstPage.hidden == YES) {
[_pageOneButton setBackgroundImage:nil forState:UIControlStateNormal];
}
The _pageOneButton keeps the background even with the view hidden.
I leave an image of the menu as is now:
The point is: when Página 2 is active (hidden == NO) the Página 1 button should be without the background.
To few details here, but in the meantime you could play a bit with the following suggestions.
First, move your
[self.view addSubview:_pageOneButton];
outside the method. For example, in viewDidLoad method you add that button to a superview.
Then you can control its state (through a reference to it, as you did) like
// set the image
[_pageOneButton setImage:image forState:UIControlStateNormal];
// remove the image
[_pageOneButton setImage:nil forState:UIControlStateNormal];
I have a simple button with two images for each UIControlStates.
I want this button to behave as a control and as indicator so that I can press it down - "activate" and press again "deactivate", While some other functions can cause it to be turned off ("deactivate").
My question is how can I change it's state from selected to not-selected?
Changing the selected property would not do the trick :)
You can disable the button so it can't be press able.
yourButton.enabled = NO;
and When you want it back to be press able, then you can enabled it
yourButton.enabled = YES;
Simplest way would be consider button as switch and change it's state according switch on/off. For example use BOOL variable which upon button touch gets its value YES or NO while according to it button gets its image.
- (void) buttonTouched:(id)sender
{
switchOn = !switchOn;
UIImage *buttonImage = nil;
if (switchOn == YES)
{
buttonImage = [UIImage imageNamed:#"on.png"];
}
else
{
buttonImage = [UIImage imageNamed:#"off.png"];
}
[myButton setImage:buttonImage forState:UIControlStateNormal];
[myButton setImage:buttonImage forState:UIControlStateSelected];
}
if you need to programmatically set button "disabled":
- (void) setButtonDisabled
{
switchOn = YES; //calling buttonTouched: will turn it to NO
[self buttonTouched:nil];
}