menuView= [[UIImageView alloc] initWithFrame:CGRectMake(0,411, 320,49)];
UIButton *test = [UIButton buttonWithType:UIButtonTypeCustom ];
test.frame = CGRectMake(0, 0, 80, 49);
[test addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[menuView addSubview:test];
[self.view addSubview:menuView];
I touch up inside (test ) button. but is not working . why??
Maybe you mixed buttonPressed: with buttonPressed.
If you implement a method like
- (void)buttonPressed
{
}
You need use
[test addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
Note: Pay attention on the colon.
But depending on what you have done, you need implement a method like
- (void)buttonPressed:(id)sender
{
}
for a cleaner solution you can try HMUIButtonOnTouch
source: https://github.com/HelloMihai/HMUIButtonOnTouch
[self.myButton onTouchInside:^{
NSLog(#"button tap");
}];
Related
I have an Xcode 5 button problem.
I create a number of button programatically on a UIViewController and then want to register clicks on these buttons. I use NSLog to track the clicks but it seems that no clicks are registered in the log.
Can anyone help please?
The ultimate aim to to segue to next UIViewController on each button click, but was just testing to see if button clicks were registered first.
Here is how I create the buttons in viewDidLoad (only 1 shown).
NOTE: the if-statement used only to check which buttons were pressed in previous view.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *myString = [NSString stringWithFormat:#"%d", self.tagNumber ];
if (self.tagNumber == 1)
{
hotelButton = [UIButton buttonWithType:UIButtonTypeCustom];
hotelButton.tag = 1;
hotelButton.frame = CGRectMake(60, 60, 300.f, 100.f);
UIImage *buttonImage1 = [UIImage imageNamed:#"buttonImage1.png"];
[hotelButton setBackgroundImage:buttonImage1 forState:UIControlStateNormal];
[self.view addSubview:hotelButton];
// Add targets and actions
[hotelButton addTarget:self action:#selector(buttonClicked) forControlEvents:UIControlEventTouchUpOutside];
}
}
And here is how I check for button clicks
- (void) buttonClicked
{
NSLog(#"Button %i clicked", hotelButton.tag);
}
NOTE: I also tried the following:
- (void) buttonClicked:(UIButton *) button
{
NSLog(#"Button %ld clicked", (long int)[button tag]);
}
But that gave an error "undeclared selector buttonClicked" in my viewDidLoad pointing to action#selector):
[hotelButton addTarget:self action:#selector(buttonClicked) forControlEvents:UIControlEventTouchUpOutside];
Use this:
[hotelButton addTarget:self action:#selector(buttonClicked)
forControlEvents:UIControlEventTouchUpInside];
From Apple Docs:
UIControlEventTouchUpInside
A touch-up event in the control where the finger is inside the bounds of the control.
UIControlEventTouchUpOutside
A touch-up event in the control where the finger is outside the bounds of the control
And keep this method:
- (void) buttonClicked
{
NSLog(#"Button %i clicked", hotelButton.tag);
}
try and change outside to inside,
[hotelButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
Try this,
[hotelButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
and pass the sender,
- (void) buttonClicked:(UIButton*)sender
{
NSLog(#"Button %i clicked", [sender tag]);
}
Have modified your if condition. Please follow :-
if (self.tagNumber == 1)
{
hotelButton = [UIButton buttonWithType:UIButtonTypeCustom];
hotelButton.tag = 1;
hotelButton.frame = CGRectMake(60, 60, 300.f, 100.f);
UIImage *buttonImage1 = [UIImage imageNamed:#"buttonImage1.png"];
[hotelButton setBackgroundImage:buttonImage1 forState:UIControlStateNormal];
hotelButton.showsTouchWhenHighlighted = YES;
hotelButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
[hotelButton addTarget:self action:#selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:hotelButton];
}
I am creating a custom button with its own image, and assigning it to the rightbarbutton of a nabvigationcontroller I have. The problem is that when the user clicks on the button I get an exceptoin about my controller not recognizing that selctor??
UIImage* image1 = [UIImage imageNamed:#"someImage.png"];
CGRect imgRect = CGRectMake(0, 0, image1.size.width/1.8, image1.size.height/1.8);
UIButton *myButton = [[UIButton alloc] initWithFrame:imgRect];
[myButton setBackgroundImage:image1 forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(nextScreenButtonAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButton =[[UIBarButtonItem alloc] initWithCustomView:myButton];
[self.navigationItem setRightBarButtonItem:myButton];
self.navigationController.navigationBarHidden = false;
May be this is the problem nextScreenButtonAction change it to
[myButton addTarget:self action:#selector(nextScreenButtonAction:) forControlEvents:UIControlEventTouchUpInside]
In your method definition you be passing and argument like
-(IBAction) nextScreenButtonAction:(id)sender
{
}
so in the addTarget you need to specify a colon(:) to indicate parameter
In response to below code,
[myButton addTarget:self action:#selector(nextScreenButtonAction) forControlEvents:UIControlEventTouchUpInside];
Make sure that you have create method in the same class like,
- (void) nextScreenButtonAction
{
// write your logic here...
}
Hi,
i'm trying to animate the image of a UIButton between two different image.
Let's say i have two square images, on RED, on BLACK.
When i touch my Button, i want the image of the uibutton to loop from both images, in a smooth animation.
the loop animation must start whenever the user touch down the button and canceled when he touch Up inside or drag exit.
Here is what i do now :
-(void)startAnimation:(UIButton *)obj {
[UIView animateWithDuration:1 delay:0 options:(UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat) animations:^{
[obj setAlpha:0];
[obj setImage:[UIImage imageNamed:#"animated_on"] forState:UIControlStateNormal];
[obj setImage:[UIImage imageNamed:#"animated_on"] forState:UIControlStateHighlighted];
[obj setImage:[UIImage imageNamed:#"animated_on"] forState:UIControlStateDisabled];
[obj setImage:[UIImage imageNamed:#"animated_on"] forState:UIControlStateSelected];
} completion:^(BOOL finished) {
[obj setAlpha:1];
[obj setImage:nil forState:UIControlStateNormal];
[obj setImage:nil forState:UIControlStateHighlighted];
[obj setImage:nil forState:UIControlStateDisabled];
[obj setImage:nil forState:UIControlStateSelected];
}];
}
-(void)stopAnimation:(UIButton *)obj {
[obj.layer removeAllAnimations];
}
- (void)touchDownAnimation:(id)obj {
[self startAnimation:obj];
}
- (void)dragEnterAnimation:(id)obj {
[self startAnimation:obj];
}
- (void)dragExitAnimation:(id)obj {
[self stopAnimation:obj];
}
- (void)touchUpInsideAnimation:(id)obj {
[self stopAnimation:obj];
}
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = CGRectMake(110, 180, 100, 100);
UIImageView *animatedButtonBackground = [[UIImageView alloc] initWithFrame:frame];
[animatedButtonBackground setImage:[UIImage imageNamed:#"animated_off"]];
[self.view addSubview:animatedButtonBackground];
UIButton *animatedButton = [UIButton buttonWithType:UIButtonTypeCustom];
[animatedButton setFrame:frame];
[animatedButton addTarget:self action:#selector(touchDownAnimation:) forControlEvents:UIControlEventTouchDown];
[animatedButton addTarget:self action:#selector(dragEnterAnimation:) forControlEvents:UIControlEventTouchDragEnter];
[animatedButton addTarget:self action:#selector(dragExitAnimation:) forControlEvents:UIControlEventTouchDragExit];
[animatedButton addTarget:self action:#selector(touchUpInsideAnimation:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:animatedButton];
}
it's kinda works, but involving an UIImageView in the process looks like a bad trick to do what i want...
i'm looking for a cleaner solution.
Wonder if you could use something simpler:
button.imageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"image1"],
[UIImage imageNamed:#"image2"],
nil];
button.imageView.animationDuration = 1;
[button.imageView startAnimating];
I have a iOS5 project using storyboard and with ARC on.
I have a view in storyboard with the class thisViewController and in there I got a smaller subview which I dragged in and gave it the class thisView.
thisView has a custom drawRect function, that works and draws what I want nicely.
But I also want to add buttons dynamically, so I'll add them in the initWithFrame method of thisView like so:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSLog(#"This is called <3");
UIButton *btn= [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 50, 50);
btn.backgroundColor = [UIColor redColor];
[btn setTitle:#"Play" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn];
[self bringSubviewToFront:btn]; // last thing I tried, didn't work, thought z-index was to low or something
}
return self;
}
The function is being called since the NSLog displays, but the button is nowhere to be found
--EDIT--
Extra information:
ThisView.h
#interface RadarView : UIView <CLLocationManagerDelegate>{
CLLocation *currentLocation;
}
Dont add the SubView in the init method.
Add the SubView in the - (void)layoutSubviews.
- (void)layoutSubviews {
[super layoutSubviews];
if (![self viewWithTag:12345]) {
UIButton *btn= [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 50, 50);
btn.tag = 12345;
btn.backgroundColor = [UIColor redColor];
[btn setTitle:#"Play" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn];
}
}
I found what the problem was thanks to some comments and the chat
There was an init in the radarviewcontroller, which triggered initwithframe, which made the NSLog display, and the initwithcoder wasn't used so I didn't see anything, that was the problem and got stuff mixed up. Thanks for commenting and helpin!
This code results in an "invalid selector" error when the button I create is pressed. Where is the test function fetched from?
Main.m
mainScreen = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
[self.view addSubview:mainScreen];
TaskButtons *tB = [[TaskButtons alloc] init];
[mainScreen addSubview:[tB TaskStart]];
TaskButtons.m
- (UIButton*)TaskStart {
CGRect buttonFrame = CGRectMake(500, 206, 400, 35);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = buttonFrame;
[button setTitle:#"Task Button" forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
button.titleLabel.textAlignment = UITextAlignmentLeft;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
[button addTarget:self action:#selector(test) forControlEvents:UIControlEventTouchUpInside];
return button;
}
- (void)test{
NSLog(#"test line");
}
It seems that the test function isn't being called. Doesn't setting the button's target to self here mean it should look in the TaskButtons class for the function called test?
The problem is ARC is releasing the instantiated object too soon. So to solve this I would need to retain it longer.
Main.h
#import "TaskButtons.m"
#interface ViewController : UIViewController {
TaskButtons *tB;
}
#property (nonatomic, retain) TaskButtons *tB;
[button addTarget:self action:#selector(test:) forControlEvents:UIControlEventTouchUpInside];
- (void)test:(id)sender{
NSLog(#"test line");
}
Syntax problem :) In your code replace these lines.