I have this action for the "Touch Up Inside" event of a UIButton that should create a new UIButton right below the sender:
-(IBAction) cloneMe: (id) sender{
if (!currentY) {
currentY = [sender frame].origin.y;
}
UIButton *clone = [UIButton buttonWithType:UIButtonTypeRoundedRect];
CGRect cloneFrame = [sender frame];
cloneFrame.origin.y += currentY + cloneFrame.size.height + 30;
clone.frame = cloneFrame;
currentY = cloneFrame.origin.y + cloneFrame.size.height;
}
It doesn't work and the new button is never displayed. Anybody knows what's going on?
You are forgetting [sender.superview addSubview:clone];
You create button but don't add it to view hierarchy - you missing
[self.view addSubView: clone];
call or similar
Related
I have a UIButton that has only an UIImage set, when Clicked (long press) the images changes automatically with a shadow, What I want is that even if the button gets clicked but not with a long press the images should have the shadow. I already tried from IB to set the UIButton properties Reverse on Highlight and Shows Touch On Highlight but with no results
This is about UIControl Class. It's default to be with shadow. So if you wanna customize it, you should addTarget to your button to every action on UIButton like tapping,long press,touchUpInside and I think it s not about long press it's just you can't realize the difference on tapping.
in one of my project I customize my button actions like this:
buyButton = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width*3/4-2, 0, self.frame.size.width/4+4, self.frame.size.height)];
[buyButton addTarget:self action:#selector(buyButtonClicked:) forControlEvents:UIControlEventTouchDown];
[buyButton addTarget:self action:#selector(buyButtonNormal:) forControlEvents:UIControlEventTouchUpInside];
[buyButton addTarget:self action:#selector(buyButtonNormal:) forControlEvents:UIControlEventTouchUpOutside];
and methods of this like:
- (void) buyButtonClicked:(UIButton *) sender {
if ([self.backgroundColor isEqual:[Util UIColorForHexColor:#"fede32"]]) {
self.backgroundColor = [Util UIColorForHexColor:#"fdca2e"];
}
else if([self.backgroundColor isEqual:[Util UIColorForHexColor:#"cfd3dc"]]) {
self.backgroundColor = [Util UIColorForHexColor:#"c0c5d0"];
}
}
- (void) buyButtonNormal:(UIButton *) sender {
if ([self.backgroundColor isEqual:[Util UIColorForHexColor:#"fdca2e"]]) {
self.backgroundColor = [Util UIColorForHexColor:#"fede32"];
}
else if([self.backgroundColor isEqual:[Util UIColorForHexColor:#"c0c5d0"]]) {
self.backgroundColor = [Util UIColorForHexColor:#"cfd3dc"];
}
[delegate purchaseOffer:selectedOffer];
}
If you need more information about UIButton actions there are a lot of question on this site.
I have a grid of buttons in one page. I want the buttons to lead to another view when clicked. What should I add to the following action to change the view?
-(void)buttonPressed:(UIButton *)button {
NSLog(#"button %u -- frame: %#", button.tag, NSStringFromCGRect(button.frame));
}
The grid of buttons is created with:
{
int rows = 13, columns = 4;
UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0.f, 0.f, 80*columns, 32*rows)];
int currentTag = 0;
for (int y = 0; y < rows; y++) {
for (int x = 0; x < columns; x++) {
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
// [button.layer setBorderWidth:1.0];
// [button.layer setBorderColor:UIColor blackColor]];
button.backgroundColor=[UIColor colorWithRed: 201.0/255.0 green: 201.0/255.0 blue:201.0/255.0 alpha: 1.0];
button.tag = currentTag;
currentTag++;
[button.layer setBorderColor: [[UIColor blackColor] CGColor]];
[button.layer setBorderWidth: 1.0];
[button setTitle:[NSString stringWithFormat:#"%d",currentTag] forState:UIControlStateNormal];
button.frame = CGRectMake(80*x, 32*y, 80, 32);
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[buttonView addSubview: button];
}
}
// Center the view which contains your buttons
CGPoint centerPoint = buttonView.center;
centerPoint.x = self.view.center.x;
buttonView.center = centerPoint;
[self.view addSubview:buttonView];
}
Maybe you want to connect an action?
[button addTarget:self
selector:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchUpInside];
(check syntax, this is by memory...)
Rinju's answer would work, but it needs a little more explanation. I'm not sure why exactly you would like to create this grid of buttons (more info would be nice), but if you want each button to show a new view, then tags are important!
If you want to open the same view, but with different properties shown on that view, you'd need to create a new view controller class. Let's call this DetailViewController.
Now, in your buttonPressed: method, you're going to need to instantiate this view controller and set a property of it!
#interface DetailViewController : UIViewController
#property (nonatomic, assign) int buttonTag;
...
//other methods, properties, etc. here
...
#end
Now, in your view controller where the buttons are, you can do this (for the sake of simplicity, I will assume you are using storyboards. If not, it can still be easily accomplished similar to what Rinju has done).
-(void)buttonPressed:(UIButton*)button {
[self performSegueWithIdentifier:#"DetailView" sender:button];
}
Now, you can implement the prepareForSegue method, which is called just before the segue fires:
-(void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender {
if( [[segue identifier] isEqualToString:#"DetailView"] ) {
DetailViewController *detailView = [segue destinationViewController];
detailView.buttonTag = ((UIButton*)sender).tag;
}
}
Now that the tag is set for your new detail view controller, you can probably use switch statements (or better yet, a model object which is passed this buttonTag) in viewDidLoad: of the DetailViewController to set up the UI there. Basically what we are doing is using the tag of the button to distinguish which button was pressed, and based on this create a new view!
What you may want to do is create a typedef enum structure to make names for the tags instead of using raw integers. This will increase the readability of your code, and also will help you in not getting confused ;)
-(void)buttonPressed:(UIButton *)button
{
NSLog(#"button %u -- frame: %#", button.tag, NSStringFromCGRect(button.frame));
Cart *crtObj=[[Cart alloc]initWithNibName:#"Cart" bundle:nil];
[self.navigationController pushViewController:crtObj animated:YES];
[crtObj release];
}
I have button created programmatically in the UIImageView. This view was also created programmatically. When image in this view is tapped the button is created, now I want to capture when this button is tapped but I'm unable to here is my relevant code :
- (void)imageTapped:(UIGestureRecognizer *)sender{
MyImageView *myView = (MyImageView *)sender.view;
NSLog(#"Image tapped is => %#", myView.currentImageName);
//add button to view
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(pressedPlay:)
forControlEvents:UIControlEventTouchDown];
//button image
UIImage *backgroundImage = [UIImage imageNamed:[NSString stringWithFormat:#"playbutton.png"]];
button.frame = CGRectMake(85.0, 175.0, backgroundImage.size.width, backgroundImage.size.height);
myView.userInteractionEnabled = YES;
button.userInteractionEnabled = YES;
[button setImage:backgroundImage forState:(UIControlStateNormal)];
[myView addSubview:button];
}
Now this code should handle the tap but for some reason it doesn't :
- (void)pressedPlay:(UIGestureRecognizer *)sender{
//MyImageView *senderView = (MyImageView *) sender.view;
NSLog(#"%#", #"I Presed button");
}
Any reason why this is not working?
Your code suggests that you are using a UIGestureRecognizer (most likely a UITapGestureRecognizer) to detect the initial tap. The problem is that UIGestureRecognizer is being greedy and stopping the UIButton from receiving the touch.
You will need to do some shifting to disable the gesture recognizer after the initial detection and then reenable it after the play has been tapped.
UIGestureRecognizer has the property
#property(nonatomic, getter=isEnabled) BOOL enabled
or you can choose to implement UIGestureRecognizerDelegate and provide an implementation of
gestureRecognizer:shouldReceiveTouch:
a very simple attempt could look like:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
{
return ![touch.view isKindOfClass:[UIButton class]];
}
and don't forget to set the delegate on the gesture recognizer
myGesture.delegate = self;
set the button frame from:
button.frame = CGRectMake(85.0, 175.0, backgroundImage.size.width, backgroundImage.size.height);
to:
button.frame = CGRectMake(0,0, backgroundImage.size.width, backgroundImage.size.height);
it looks like you are setting button frame outside the imageview's frame and hence the button is not actually getting any interaction.
The Button frame is likely setting the button outside the bounds of the UIImageView, frames are always relative to the superviews coordinate in this case the UIImageView.
[button setExclusiveTouch:YES];
I have a button that should move 2 pixels down when pressed down and jump back to the original location when the press ended. I've tried touchesBegan and touchesEnded but those don't work with a button. I've also tried exchanging the button with an IUView and calling the button's method in touchesEnded but that gave the button a weird delay before moving down. Any ideas?
Did you try using the Touch Down and Touch Up events? Try moving the button location on Touch Down and restoring it to its initial value on Touch Up (Inside and Outside).
EDIT: Here's a code example:
- (IBAction)moveButtonDown:(id)sender
{
UIButton *btn = (UIButton *)sender;
btn.frame = CGRectMake(btn.frame.origin.x, btn.frame.origin.y + 2, btn.frame.size.width, btn.frame.size.height);
}
- (IBAction)moveButtonUp:(id)sender
{
UIButton *btn = (UIButton *)sender;
btn.frame = CGRectMake(btn.frame.origin.x, btn.frame.origin.y - 2, btn.frame.size.width, btn.frame.size.height);
}
Assign moveButtonDown: to the buttons "Touch Down" event and moveButtonUp: to both its "Touch Up Inside" and "Touch Up Outside" events. Tested and working, here at least ;)
EDIT: Thanks fichek, here's a solution with CGRectOffset instead of CGRectMake:
- (IBAction)moveButtonDown:(id)sender
{
UIButton *btn = (UIButton *)sender;
btn.frame = CGRectOffset(btn.frame, 0, 2);
}
- (IBAction)moveButtonUp:(id)sender
{
UIButton *btn = (UIButton *)sender;
btn.frame = CGRectOffset(btn.frame, 0, -2);
}
I am creating some buttons and putting them inside a UIScrollView like this:
int i = 0;
while (i != numberOfButtons ) {
int updatetY = 160*i;
CGRect buttonFrame = CGRectMake(updatetY, 0, 160, 60);
UIButton *button = [[UIButton alloc] initWithFrame:buttonFrame];
UIImage *buttonImage = [UIImage imageNamed:#"bg.png"];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button setTitle:[buttonsArray objectAtIndex:i] forState:UIControlStateNormal];
[button addTarget:self action:#selector(MYPROBLEM) forControlEvents:UIControlEventTouchUpInside];
[menuScrollView addSubview:button];
i++;
}
Now I need to build a method to capture which button is being touched, and run a specific bunch of code (see (MYPROBLEM).
Anybody knows what I can do to "trace" which button is being touched and then run a specific function?
something like:
-(void) buttonfunction
{
case ...
doThis
case...
doThat
}
thanks in advance.
You need to use the tag property of the uibutton control. It's a numeric id for the button. You can set a tag on the button based on the i variable in your loop, for instance:
button.tag=i;
Then in your button's action message, you would simply add the id of the button as a parameter and check its tag like so:
-(void) buttonFunction:(id)sender {
switch (sender.tag)
{
//code goes here
}
}
Hope this helps.