IOS UIButton Target doesnot working - ios

here is my code
for (NSString *message in self.messages) {
label = [[UIButton alloc] initWithFrame:CGRectZero];
[label setTitle:message forState:UIControlStateNormal];
//label.titleLabel.text = message;
label.tag = i;
[label addTarget:self action:#selector(gototest:) forControlEvents:UIControlEventTouchDown];
CGSize size = [message sizeWithFont:label.titleLabel.font];
CGFloat width = size.width + kPADDING;
label.frame = CGRectMake(xPos, 0.0, width, self.frame.size.height);
[self addSubview:label];
i++;
xPos += width;
}
-(void)gototest:(UIButton*)sender
{
int tag = sender.tag;
NSLog(#"test %#",#"ccc ");
}
So nothing happen when I click on the buttons.
Note that the class is extend from UIScrollView and also I have to create a marquee with different news and when I click in any of this news, I have to go to the detail of the chosen new.
Please help me

UIControlEventTouchDown should not be the problem, it works too. Can you try the following
label = [UIButton buttonWithType:UIButtonTypeCustom];
instead of
label = [[UIButton alloc] initWithFrame:CGRectZero];
Too I suspect the following line too
label.frame = CGRectMake(xPos, 0.0, width, self.frame.size.height);
the frame you are setting confirm it is correct, in normal usage it may appear that your UIButton is visible, but it may not. Try put
label.clipsToBounds=YES;
and check, if your UIButton is actually visible, that might be the reason your action not working..
UPDATE
Ok, As per my understanding, as you are saying, you are trying to create Marquee, and the original implementation had UILabel and you are trying to change that to UIButton. Let change few things as the code below,
for (NSString *message in self.messages) {
label = [[UILabel alloc] initWithFrame:CGRectZero];
label.text = message;
label.tag = i;
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(gototest:)];
tapGestureRecognizer.numberOfTapsRequired = 1;
[label addGestureRecognizer:tapGestureRecognizer];
label.userInteractionEnabled = YES;
CGSize size = [message sizeWithFont:label.font];
CGFloat width = size.width + kPADDING;
label.frame = CGRectMake(xPos, 0.0, width, self.frame.size.height);
[self addSubview:label];
i++;
xPos += width;
}
And your action
-(void)gototest:(UITapGestureRecognizer*)sender
{
int tag = sender.view.tag;
NSLog(#"test %#",#"ccc ");
}

Use touch event "UIControlEventTouchUpInside" intends of "UIControlEventTouchDown".

It will works fine :
[label addTarget:self action:#selector(grottiest:)
forControlEvents:UIControlEventTouchUpInside];
Edit :
label = [[UIButton alloc] initWithFrame:CGRectMake(xPos, 0.0, 100, 100)];
[label setTitle:message forState:UIControlStateNormal];
//label.titleLabel.text = message;
label.tag = i;
[label addTarget:self action:#selector(gototest:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:label];

Your code is working fine with single button. The problem is somewhere else..
UIButton *label = [[UIButton alloc] initWithFrame:CGRectZero];
[label setTitle:#"Click" forState:UIControlStateNormal];
//label.titleLabel.text = message;
label.tag = 0;
[label addTarget:self action:#selector(gototest:) forControlEvents:UIControlEventTouchDown];
label.frame = CGRectMake(0,0,100,50);
[self addSubview:label];

Replace:
label = [[UIButton alloc] initWithFrame:CGRectZero];
with
UIButton *label = [[UIButton alloc] initWithFrame:CGRectZero];

Related

UIButton Selector method not working

I am creating a UIIimageView programtically than I create the UIView and in uiview I added two uibutton programtically but button selector method not working . I tried many thing but nothing happened . Here is my code
-(void) pagingFunc{
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat width = CGRectGetWidth(screen);
CGFloat height = CGRectGetHeight(screen);
//ScrollView Size and Contents
CGSize mxSize = CGSizeMake( [imgesArry count]*width , height-114);
[scrlView setContentSize : mxSize];
self.customView.translatesAutoresizingMaskIntoConstraints =YES;
self.customView.frame = CGRectMake(0, 0, mxSize.width, mxSize.height);
int incX = 0 ;
for( int i = 0; i < [imgesArry count]; i++)
{
PFObject *imageObject = [imgesArry objectAtIndex:i];
PFFile *file;
if (height == 480) {
file = [imageObject objectForKey:#"image4s"];
}
else{
file = [imageObject objectForKey:#"image6s"];
}
NSString * imgFile = [file url];
UIImageView* imagView = [[UIImageView alloc]initWithFrame : CGRectMake(incX,0,width ,height)];
imgView.userInteractionEnabled = YES;
[imagView sd_setImageWithURL:[NSURL URLWithString:imgFile] placeholderImage:[UIImage imageNamed:#"UserUpdationImageCell.png"]];
btnView = [[UIView alloc] initWithFrame:CGRectMake(incX, imagView.frame.size.height-60, width, 50)];
btnView.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.8];
UIButton *bckBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[bckBtn addTarget:self
action:#selector(aMethod)
forControlEvents:UIControlEventTouchUpInside];
[bckBtn setTitle:#"Back" forState:UIControlStateNormal];
bckBtn.frame = CGRectMake(0 ,0, 160.0, 40.0);
UIButton *downloadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[downloadBtn addTarget:self
action:#selector(aMethod)
forControlEvents:UIControlEventTouchUpInside];
[downloadBtn setTitle:#"Download" forState:UIControlStateNormal];
downloadBtn.frame = CGRectMake(160 ,0, 160.0, 40.0);
[btnView addSubview:bckBtn];
[btnView addSubview:downloadBtn];
[self.customView addSubview:imagView];
[self.customView addSubview:btnView];
incX+= width;
}
[scrlView setContentOffset:CGPointMake(width*selectedIndex, 0)];
}
May be you can set : self.customView.userInteractionEnabled=YES;
I write a simple demo you describe and it works:
// imagView -> view1 -> bckBtn
UIImageView *imagView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
imagView.userInteractionEnabled = YES;
imagView.backgroundColor = [UIColor grayColor];
[self.view addSubview:imagView];
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
view1.backgroundColor = [UIColor yellowColor];
[imagView addSubview:view1];
UIButton *bckBtn = [UIButton buttonWithType:UIButtonTypeCustom];
bckBtn.frame = CGRectMake(10, 10, 50, 50);
bckBtn.backgroundColor = [UIColor redColor];
[bckBtn addTarget:self
action:#selector(aMethod)
forControlEvents:UIControlEventTouchUpInside];
[bckBtn setTitle:#"Back" forState:UIControlStateNormal];
[view1 addSubview:bckBtn];
-(void)aMethod{
NSLog(#"button click");
}
Hope it helps.
Well 1st thing, make sure you don't have any overlays over buttons.
Also you might try using GestureRecognizers.
Here's a Swift snippet, I'm pretty sure it can be translated to obj-c
let xButton = UIButton(frame: CGRect(x: 0, y: 0, width: 160, height: 40))
xButton.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(aMethod)))

how to set different image for dynamic buttons in ios?

I am creating 20 buttons dynamically in my apps project now i want to set images for each button like not same image will be different for each buttons. I try lot of solution but not getting proper result. I am implemented following code for this......
int yPossion = 150, xPossion = 44; int temp = 0;
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];
[self.view addSubview:scrollView];
for (int i = 0; i < [objectName count]; i++) {
SMSCat1 *cat = [objectName objectAtIndex:i];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setTranslatesAutoresizingMaskIntoConstraints:YES];
//Label Dynamic Code
UILabel *label = [[UILabel alloc] init];
[label setText:cat.Name];
[label setTextColor:[UIColor brownColor]];
label.font = [UIFont systemFontOfSize:12];
[label sizeToFit];
[label setFrame:CGRectMake(5, 44, 70, 60)];
[scrollView addSubview:label];
[aButton addSubview:label];
[aButton setBackgroundColor:[UIColor blackColor]];
[aButton setBackgroundImage:[UIImage imageNamed:#"icon-menu.png"]
forState:UIControlStateNormal];
[aButton setTitle:[NSString stringWithFormat:#"%d", i] forState:UIControlStateNormal];
[aButton setFrame:CGRectMake(xPossion, yPossion, 70, 60)];
aButton.highlighted = YES;
[scrollView addSubview:aButton];
xPossion += aButton.frame.size.width + 35;
temp++;
if (temp == 3) {
yPossion = aButton.frame.origin.y + aButton.frame.size.height + 20;
temp = 0;
xPossion = 44;
yPossion += aButton.frame.size.width - 15;
[scrollView setContentSize:CGSizeMake(scrollView.frame.size.width, yPossion - 50)];
}
}
}
Please help me to complete that task. I added 20 images on image folder but how to set that image for each buttons its like (can i use settag for this).
Don't be afraid to ask if you are new. Looking at other peoples code and learning is how we all get better at what we do. You can find the answer to your question here - how to set auto layout for dynamically created buttons? Hope this helps you

Infinite horizontal UIScrollView with UIButtons

here is what I do at the moment.
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(56, 205, 400, 3000)];
scrollView.delegate = self;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 3.0f,scrollView.frame.size.height * 3.0f);
scrollView.maximumZoomScale = 3.0f;
UIView *zoomView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, scrollView.contentSize.width, scrollView.contentSize.height)];
zoomView.backgroundColor = [UIColor whiteColor];
for (NSInteger index = 0; index < 100; index++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake((scrollView.frame.size.width / 2.0f) - 50.0f, 10.0f + (50.0f * (CGFloat)index), 100.0f, 30.0f);
button.tag = index;
[button setTitle:[NSString stringWithFormat:#"Button %ld", ((long)index + 1)] forState:UIControlStateNormal];
[button addTarget:self action:#selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];
[zoomView addSubview:button];
}
[scrollView addSubview:zoomView];
[self.view addSubview:scrollView];
The problem is. It's not inifinite. I would like the number 101 to be the number 1, hope you understand. And how could i create them dynamically, with a webservice telling me how much button i will have, what background i should apply to each button, and what text to put under the button. Also the code above is scrolling vertically, but at the end it would be a non zooming horizontal scrolllview. Thanks in advance.
Just set content size of your scrollview
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(56, 205, 400, 320)]; // it is for iphone. set it's resolution if you want to set it for ipad
scrollView.delegate = self;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 3.0f,scrollView.frame.size.height * 3.0f);
scrollView.maximumZoomScale = 3.0f;
UIView *zoomView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, scrollView.contentSize.width, scrollView.contentSize.height)];
zoomView.backgroundColor = [UIColor whiteColor];
for (NSInteger index = 0; index < 100; index++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake((scrollView.frame.size.width / 2.0f) - 50.0f, 10.0f + (50.0f * (CGFloat)index), 100.0f, 30.0f);
button.tag = index;
[button setTitle:[NSString stringWithFormat:#"Button %ld", ((long)index + 1)] forState:UIControlStateNormal];
[button addTarget:self action:#selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];
[zoomView addSubview:button];
}
[scrollView addSubview:zoomView];
[self.view addSubview:scrollView];
scrollView.contentSize= CGSizeMake((zoomView.frame.size.width) , zoomView.frame.size.height);
set it after your zoom view has been added into scrollView.
I hope it helps you to understand.

How can I know which view I am clicking? And why the button no reaction?

I am designing a viewcontroller which have several button bar, each bar canbe clicked and show a content view.like:
http://i.stack.imgur.com/m5V4Q.png
When I click the buttonbar, it's frame will expand bigger, and you can see the content in it(which is a button).
First, a bar button(320*30 size) and a contentView is a set, they are subviews of listview. And several listview makes the whole view.
Second, when I click it, the list view will expand from 320*30 to 320*180, the contentview in it will expand from 300*0 to 300 * 130(20 pixel of margin). and I set the clipsToBounds of contentview to Yes to make sure the content in contentview won't show when the frame height is 0.
Now,the click can show the contentview exactly as I want. But here is a problem: I can't click the button in it, and I tried to set their userInteractionEnabled to yes . And even I set the contentview as user-defined,which I set the view's userInteractionEnabled to yes and overwrite the touchbegin function to show a alertView. Those test are failed, no reaction found. And I checked and sure that there shouldn't be any view covered it.
What's the reason might be?
The code of setup the list view is:
NSArray *array = [titleArray objectAtIndex:i];
NSString *ShenSuoTitle = [array objectAtIndex:0];
NSString *ShenSuoContent = [array objectAtIndex:1];
UIView *listView = [[UIView alloc] initWithFrame:CGRectMake(0, totalHeight, 320, ShenSuoViewHeight)];
[listView setBackgroundColor:[UIColor blackColor]];
[listView setTag:[[NSString stringWithFormat:#"%d",(i+1)] intValue]];
[self.scrollView addSubview:listView];
totalHeight = totalHeight + 1 + ShenSuoViewHeight;
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, ShenSuoViewHeight)];
[titleView setBackgroundColor:[UIColor whiteColor]];
[titleView setTag:[[NSString stringWithFormat:#"%d1",i+1] intValue]];
[listView addSubview:titleView];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTag:[[NSString stringWithFormat:#"%d2",i+1] intValue]];
[btn setFrame:CGRectMake(0, 0, 320, ShenSuoViewHeight)];
[btn addTarget:self action:#selector(reSetFrame:) forControlEvents:UIControlEventTouchUpInside];
[titleView addSubview:btn];
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(5, 12, 30, 30)];
img.image = [UIImage imageNamed:#"list_ico.png"];
[img setTag:[[NSString stringWithFormat:#"%d3",i+1] intValue]];
[titleView addSubview:img];
NSLog(#"img:%f,%f",img.frame.origin.y,img.frame.size.height);
UILabel *labTitle = [[UILabel alloc] initWithFrame:CGRectMake(35, 15, 100, 25)];
labTitle.textColor = [UIColor blackColor];
labTitle.backgroundColor = [UIColor clearColor];
labTitle.font = [UIFont boldSystemFontOfSize:15];
labTitle.text = ShenSuoTitle;
[titleView addSubview:labTitle];
NSLog(#"labTitle:%f,%f",labTitle.frame.origin.y,labTitle.frame.size.height);
//add a label for selected
UILabel *selectedLabel = [[UILabel alloc] initWithFrame:CGRectMake(214, 14, 86, 21)];
selectedLabel.textColor = [UIColor grayColor];
//selectedLabel.alpha = 0.75;
selectedLabel.backgroundColor = [UIColor clearColor];
selectedLabel.text = #"All";
selectedLabel.font = [UIFont boldSystemFontOfSize:15];
[selectedLabel setTag:[[NSString stringWithFormat:#"%d5",i+1] intValue]];
[titleView addSubview:selectedLabel];
NSLog(#"selectedLabel:%f,%f",selectedLabel.frame.origin.y,selectedLabel.frame.size.height);
UIView *content = [[UIView alloc] initWithFrame:CGRectMake(10, 10 + ShenSuoViewHeight, 300, 0)];
content.backgroundColor = [UIColor grayColor];
UILabel *testLa = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
testLa.text = #"Label";
UIButton *testBut = [UIButton buttonWithType:UIButtonTypeRoundedRect];
testBut.frame = CGRectMake(0, 40, 100, 30);
testBut.titleLabel.textColor = [UIColor greenColor];
testBut.titleLabel.text = #"Button";
content.tag = [[NSString stringWithFormat:#"%d4",i+1] intValue];
[content addSubview:testLa];
[content addSubview:testBut];
content.clipsToBounds = YES;
[testLa release];
[listView addSubview:content];
[content release];
[labTitle release];
[img release];
[titleView release];
[listView release];
the code handle the click to make the list view expands is:
UIView *titleView = (UIView *)[self.view viewWithTag:[[NSString stringWithFormat:#"%d1",i+1] intValue]];
titleView.backgroundColor = [UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:0.9];
UIView *listView = (UIView *)[self.view viewWithTag:[[NSString stringWithFormat:#"%d",i+1] intValue]];
listView.frame = CGRectMake(0, totalHeight, 320, ShenSuoViewHeight);
totalHeight = totalHeight + 1 + 20 + 180 + ShenSuoViewHeight;
UIView *content = [self.view viewWithTag:[[NSString stringWithFormat:#"%d4",i+1] intValue]];
content.frame = CGRectMake(10, 10 + ShenSuoViewHeight, 300, 180);
UIImageView *img = (UIImageView*)[self.view viewWithTag:[[NSString stringWithFormat:#"%d3",i+1] intValue]];
img.image = [UIImage imageNamed:#"list_ico_d.png"];
I'm not sure I follow your situation exactly, and some code would be nice. To answer your question though, the reason is most likely that the touch is being intercepted elsewhere. It may also be that you just forgot to connect an action method to the button. Try using breakpoints or NSLogs to see what methods are being triggered and which aren't.
Thanks a lot. It is due to my fault. The code of expanding the bar which setting the listview's frame is wrong, it should add in the expanded height. This leads the visual is ok but the clicking not be handled.
So this problem closed.

UIScrollView with a large number of UIButtons

What I want is a UIView with lots of UIButtons in it. They get placed and arranged according to data stored in an NSArray. Because there are quite a lot of buttons they don't fit on the screen all at once. The user should be able to zoom out to see all the buttons or to zoom in to see details (the label on them) and to easily select them.
I tried two different approaches:
1) I constructed a UIView subclass, put the buttons in it and an instance of this View inside a UIScrollview.
Effect: I can access all Buttons via their tag and scrolling and zooming works fine. BUT I can't get the buttons to handle any events (press on them)...
2) I wrote a UIViewController with exactly the same functionality and added an instance of it to the UIScrollView.
Effect: I can press the buttons now, but scrolling and zooming have stopped to work.
Here the relevant Code of the View:
- (UIView *)initWithArray:(NSArray *)nArray{
self = [super init];
if (self) {
int count = [nArray count];
for (int i=0; i<count; i++) {
UIButton *button = [[UIButton alloc]
initWithFrame:(__some Code to place the Button__);
button.tag = i+1;
NSString *title = [[NSString alloc] initWithFormat:__code for generating Title__];
[button setTitle:title forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:14];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[self addSubview:button];
}
}
return self;
}
And the Code for the matrixController:
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *nArray = [[NSMutableArray alloc] __some code___];
int count = [nArray count];
for (int i=0; i<count; i++) {
UIButton *button = [[UIButton alloc]
initWithFrame:CGRectMake(__some Code to place the Button__];
button.tag = i+1;
NSString *title = [[NSString alloc] initWithFormat:__code for generating Title__];
[button setTitle:title forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:14];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:button];
}
}
And the code for the ScrollViewController:
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 768, 970)];
[self.view addSubview:scrollView];
[scrollView setBackgroundColor:[UIColor blackColor]];
//Zooming
[scrollView setMinimumZoomScale:0.25];
[scrollView setMaximumZoomScale:4.0];
[scrollView setDelegate:self];
// constructing the view
[scrollView addSubview:chartView];
[scrollView bringSubviewToFront:chartView];
OR
[scrollView addSubview:[matrixController view]];
How can I get this to work??
I'm able to get a scroll view containing multiple buttons to pan and zoom just fine, with the buttons still handling touch events:
- (void)didTapButton:(UIButton *)button
{
NSLog(#"Button %ld", (long)button.tag);
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
scrollView.delegate = self;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 3.0f, scrollView.frame.size.height * 3.0f);
scrollView.maximumZoomScale = 3.0f;
UIView *zoomView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, scrollView.contentSize.width, scrollView.contentSize.height)];
zoomView.backgroundColor = [UIColor whiteColor];
for (NSInteger index = 0; index < 100; index++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake((scrollView.frame.size.width / 2.0f) - 50.0f, 10.0f + (50.0f * (CGFloat)index), 100.0f, 30.0f);
button.tag = index;
[button setTitle:[NSString stringWithFormat:#"Button %ld", ((long)index + 1)] forState:UIControlStateNormal];
[button addTarget:self action:#selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];
[zoomView addSubview:button];
}
[scrollView addSubview:zoomView];
[self.view addSubview:scrollView];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return [scrollView.subviews objectAtIndex:0];
}
EDIT: I said not to rely on tag in my comment below, yet I'm doing in here. :) It's merely so I could log the button number, so that part should be ignored.
for (int i=0; i<10; i++)
{
UIButton *scrollingbutton_outlet = [[UIButton alloc] init];
scrollingbutton_outlet = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *img = [UIImage imageNamed:#"box_normal.png"];
scrollingbutton_outlet.frame = CGRectMake(0, 100, img.size.width, img.size.height);
[scrollingbutton_outlet setTitle:[NSString stringWithFormat:#"%d",i+1] forState: UIControlStateNormal];
scrollingbutton_outlet.tag=i+1;
[buttonArray addObject:[NSString stringWithFormat:#"%d",i+1]];
buttonArray = [[NSMutableArray alloc] init];
[scrollingbutton_outlet setBackgroundImage:img forState:UIControlStateNormal];
[scrollingbutton_outlet addTarget:self
action:#selector(scrollbuttonpress:)
forControlEvents:UIControlEventTouchUpInside];
scrollingbutton_outlet.frame = CGRectMake(img.size.width*i,0, img.size.width, scrollviewoutlet.frame.size.height);
[scrollviewoutlet addSubview:scrollingbutton_outlet];
width = scrollingbutton_outlet.frame.size.width;
height = scrollingbutton_outlet.frame.size.height;
scrollviewoutlet.contentSize = CGSizeMake(width*i+scrollingbutton_outlet.bounds.size.width+5, height);
}

Resources