UIButton handling in array - iphone

I created array of buttons, but i am not see, that buttons handles touch events ( buttonEvent: not calls)
This my code - is it not correct ?
- (void)loadView{
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:screenRect];
[backgroundImageView setImage:[UIImage imageNamed:#"background.png"]];
backgroundImageView.opaque = YES;
self.view = backgroundImageView;
[backgroundImageView release];
CGRect brandRect = CGRectMake(90, 25, 140, 70);
UIImageView *brandImageView = [[UIImageView alloc] initWithFrame:brandRect];
[brandImageView setImage:[UIImage imageNamed:#"brand.png"]];
[self.view addSubview:brandImageView];
buttons = [NSMutableArray array];
int y = 100;
for ( int i = 0 ; i < 5; i++ ){
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(20, y, 280, 50)];
if ( i == 0 ){
[button setBackgroundImage:[UIImage imageNamed:#"select_active.png"] forState:UIControlStateNormal];
} else {
[button setBackgroundImage:[UIImage imageNamed:#"select_passive.png"] forState:UIControlStateNormal];
}
[button setTitle:[NSString stringWithFormat:#"Object%d",i] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonEvent:) forControlEvents:UIControlEventAllEvents];
button.tag = 1000 + i ;
[self.view addSubview:button];
y += 60;
}
-(void)buttonEvent:(id)sender {
NSLog(#"new button clicked!!!");
}

You are adding buttons to uiimageview, which has interaction disabled.
Also, you don't have an array of buttons, because buttons is autoreleased, and you never add object into buttons array.

Related

Created Table Cell With UIView as subview Not Displaying in iOS7

I have created UIView on top of the UITable Cell but its not displaying in iOS7 which is working fine in iOS6. Kindly provide me the solution.
We are adding the More button # the end of the table.
Added code for the same:
UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 768, 40)];
footerView.backgroundColor = [UIColor grayColor];
UIButton *btnLoadMore = [UIButton buttonWithType:UIButtonTypeCustom];
btnLoadMore.frame = CGRectMake(-10, 0, 768, 30);
btnLoadMore.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;
[btnLoadMore setTitle:#"Sample" forState:UIControlStateNormal];
[btnLoadMore setBackgroundColor:[UIColor redColor]];
[btnLoadMore setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnLoadMore addTarget:self action:#selector(loadMore) forControlEvents:UIControlEventTouchUpInside];
btnLoadMore.userInteractionEnabled=YES;
[btnLoadMore.titleLabel setFont:[UIFont fontWithName:#"Helvetica" size:17.0]];
[footerView addSubview:btnLoadMore];
[footerView setHidden:YES];
[footerView setTag:999];
[cell addSubview: footerView];
for (id subView in [cell subviews]) {
[subView setHidden:NO];
}
UIView *lastRow = [cell viewWithTag:999];
[lastRow setHidden:YES];
if(indexPath.section == [arSearch count] && isLoadMore){
for (id subView in [cell subviews]) {
[subView setHidden:YES];
}
cell.backgroundView = nil;
[lastRow setHidden:NO];
Check your code. Why you have written following line :
[footerView setHidden:YES];
I think you are hiding view from cell.
Instead of
[cell addSubview: footerView];
use
[cell.contentView addSubview: footerView];
Also remove
[footerView setHidden:YES];
For loadmore, I always do below.
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 65)];
v.backgroundColor = [UIColor clearColor];
int mySiz = 0;
// keep counter how many times load more is pressed.. initial is 0 (this is like index)
mySiz = [startNumberLabel.text intValue]+1;
// i have 15 bcz my index size is 15.
if ([feeds count]>=(15*mySiz)) {
NSLog(#"showing button...");
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(10, 10, 296, 45)];
[button setBackgroundImage:[UIImage imageNamed:localize(#"loadmore")] forState:UIControlStateNormal];
[button addTarget:self action:#selector(loadMoreData:) forControlEvents:UIControlEventTouchUpInside];
[v addSubview:button];
mainTableView.tableFooterView = v;
} else {
mainTableView.tableFooterView = nil;
}
}
[mainTableView reloadData];

how to set the scroll view contentsize dynamically?

I had an application in which i am setting the content size of the scroll view dynamically according to the number of buttons which i am creating from an array.like this `
for(int i =0;i<[sarray count];i++)
{
NSMutableDictionary *dicttable=[sarray objectAtIndex:i];
NSString *head=[dicttable objectForKey:#"cat"];
btn= [UIButton buttonWithType:UIButtonTypeCustom];
int j=i+1;
btn.frame = CGRectMake((j-1)*87,0,87, 44);
[btn setBackgroundImage:[UIImage imageNamed:#"bar.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"bar_hvr.png"] forState:UIControlStateSelected];
btn.backgroundColor = [UIColor clearColor];
btn.titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:14];
btn.titleLabel.textColor = [UIColor whiteColor];
[btn setTitle:head forState:UIControlStateNormal];
btn.tag = i;
[btn setShowsTouchWhenHighlighted:YES];
[Scroller addSubview:btn];
[btn addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
if(btn.tag==0)
{
sendActionsForControlEvents:UIControlEventTouchUpInside];
}
else
{
btn.selected=NO;
}
}
}
[Scroller setContentSize:CGSizeMake([sarray count]*85, 44)];
`
but here the problem is after the last button also the scroll view scrolls .it need not to be happend,i need the scrolling ends with last button on the view,can anybody help me on this
set scrollview bounces to NO
scrollView.bounces = NO;
yes as you said it looses the smoothness. But no other choice. If you found any sol please update the answer.
hey i am sending you complete example to make button dynamicay and seting the content size scrollview on number of count ,i had just given u one example code u have to set the size of frame of button as u requried
UIScrollView *scroll_View=[[UIScrollView alloc]initWithFrame:self.view.bounds];
[self.view addSubview:scroll_View];
int y=0;
for(int i =1;i<10;i++)
{
// NSMutableDictionary *dicttable=[sarray objectAtIndex:i];
// NSString *head=[dicttable objectForKey:#"cat"];
UIButton* btn= [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(y,89,100, 50);
[btn setBackgroundImage:[UIImage imageNamed:#"bar.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"bar_hvr.png"] forState:UIControlStateSelected];
btn.backgroundColor = [UIColor clearColor];
btn.titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:14];
NSString *str=[NSString stringWithFormat:#"%d",i];
btn.titleLabel.textColor = [UIColor whiteColor];
[btn setTitle:str forState:UIControlStateNormal];
btn.tag = i;
[btn setShowsTouchWhenHighlighted:YES];
[scroll_View addSubview:btn];
[btn addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
y=110*i;
}
[scroll_View setContentSize:CGSizeMake(y, 400)];
enjoy coding
You just declare one yMargin variable of type CGFloat and initialize it with some required value as 0. Now every time whenever you are adding a new subview vertically do y+=lbl.frame.size.height;
Now setContentSize:CGSizeMake([sarray count]*85, y margin+someRequiredMargin)];
(someRequiredMargin - you want to add below )

Strange Behaviour of UIScrollview

I need to draw dynamic images on UIScrollView for this i use, MKHorizion menu (A subclass of Scroll view). Now i add Subview images on scrollview. Loop worked perfectly for adding subview on scrollview. Now in My parent class I need to touch scroll view for updated data. If i didn't touch then it is not showing updated data. Below is code
-(void) reloadData
{
[self deleteAlliTem];
self.itemCount = [dataSource numberOfImagesForMenu:self];
self.backgroundColor = [dataSource backgroundColorForImage:self];
UIFont *buttonFont = [UIFont boldSystemFontOfSize:15];
int buttonPadding = 0;
int tag = kButtonBaseTag;
int xPos = kLeftOffset;
for(int i = 0 ; i < self.itemCount; i ++)
{
NSLog(#"*************************************************************");
NSMutableDictionary *dictData=[dataSource horizMenuImage:self dictForItmeAtIndex:i];
NSString *title=[dictData valueForKey:#"name"] ? [dictData valueForKey:#"name"] : #"";
UIImage* imageItem= [UIImage imageWithData:[Base64 decode:[dictData valueForKey:#"img"]]];
int buttonWidth = 95;
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customButton setTitle:title forState:UIControlStateNormal];
customButton.titleLabel.font = buttonFont;
[customButton setBackgroundImage:[UIImage imageNamed:#"addFriendStrip.png"] forState:UIControlStateNormal];
[customButton setBackgroundImage:[UIImage imageNamed:#"addFriendStrip.png"] forState:UIControlStateSelected];
customButton.tag = tag++;
[customButton addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
customButton.frame = CGRectMake(xPos, 70, buttonWidth + buttonPadding, 25);
customButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
customButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
customButton.titleEdgeInsets =UIEdgeInsetsMake(0, 10,0, 0);
customButton.titleLabel.font = [UIFont fontWithName:#"Overlock-Bold" size:16];
[customButton setTitleColor:[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateNormal];
[customButton setTitleColor:[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateSelected];
UIImageView* itemImageView = [[UIImageView alloc] initWithImage:imageItem];
itemImageView.tag = 200 + customButton.tag;
[itemImageView setFrame:CGRectMake(xPos, 0, buttonWidth + buttonPadding, 95)];
[self addSubview:itemImageView];
[itemImageView release];
itemImageView = nil;
[self addSubview:customButton];
xPos += buttonWidth;
xPos += buttonPadding;
if (i != self.itemCount-1){
xPos += 2.5; //5; // Richa
}
}
self.contentSize = CGSizeMake(xPos, 95);
NSLog(#"############################################################################ - %d",[[self subviews] count]);
[self scrollRectToVisible:CGRectMake(1, 0, self.frame.size.width, self.frame.size.height) animated:YES];
}
Please Help me to sort out this. Why do i needed to touch scroll view ? Do i need to override other methods ?
Did you check it is on main thread ? may be you are using GCD Queue thats why it is not updating till touch.
just try to bring your scrollview on top.I am not getting your question correctly ,but it may help.
Let me know if it is working or not..!!!
Happy Coding.!!!!

UIButton selector not working on mainwindow view

I am adding a custom view on main window then on the view i am creating a button and button is showing but the action is not performing on button tapped.
on a button tap i want to open a photogallery.
and in a loop the buttons are created.
code that i am using is there...
UIImage *image=[[UIImage alloc] initWithData:data cache:NO];
UIImageView *iv=[[UIImageView alloc] initWithFrame:CGRectMake(200, y, 75, 75)];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(200, y, 75, 75)];
[button setAlpha:0];
[button setTag:i];//change this to your loop counter
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
iv.image=image;
// iv.tag=#"bigimage.jpg";
iv.userInteractionEnabled=YES;
button.userInteractionEnabled = YES;
[iv addSubview:button];
[button release];
y=y+145;
[view1 addSubview:iv];
[iv release];
And also try this code but no improvements are there.....
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(200, y, 75, 75);
UIImage *img = [[UIImage alloc] initWithData:data];
[button setBackgroundImage:img forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
y = y+145;
[view1 addSubview:button];
The frame of your button should not have the same origin x and y as your imageview. If you want it inside the image view it should have origin x:0 y:0 and the same width and height as your imageview.
The origin x,y is relative to it's parent. In your case the imageview.
Hello Shivangi,
I think you want to set image on button so try this code its will help you
//function to create button dynamically on scrollview
-(void)createButton
{
int Y = 20;
indexRow = 0;
for (int i = 0; i < [prow.noOfPhotos count]; i++)
{
thumbNailButton = [UIButton buttonWithType:UIButtonTypeCustom];
int X = 4 + (78 * (i % 4));
NSLog(#"X = %d",X);
NSLog(#"Start Y = %d",Y);
if (i % 4 == 0)
{
NSLog(#"%d",i);
Y = (70 * verticalImageRowCount);
NSLog(#"Y = %d",Y);
verticalImageRowCount++;
NSLog(#"VerticalImageRowCount= %d");
}
CGRect rect = myScrollView.frame;
rect.size.width = 100;
rect.size.height = Y + 77;
myScrollView.contentSize = CGSizeMake(rect.size.width,rect.size.height);
thumbNailButton.frame = CGRectMake(X,Y, 62,65);
view = [[UIImageView alloc] init];
prow.photo = [prow.noOfPhotos objectAtIndex:indexRow];
imagePath = prow.photo;
asyncImageView = [[[AsyncImageView alloc] initWithFrame:CGRectMake(4, 0, 62, 65)] autorelease];
asyncImageView.backgroundColor = [UIColor clearColor];
[view addSubview:asyncImageView];
NSURL *url = [NSURL URLWithString:imagePath];
[asyncImageView loadImageFromURL:url];
[view setImage:[UIImage imageNamed:[prow.noOfPhotos objectAtIndex:i]]];
thumbNailButton.tag = i;
thumbNailButton.backgroundColor=[UIColor clearColor];
[thumbNailButton addTarget:self action:#selector(imageClicked:) forControlEvents:UIControlEventTouchUpInside];
[thumbNailButton addSubview:view];
[myScrollView addSubview:thumbNailButton];
indexRow+= 1;
}
}
Try putting an NSLog in your buttonPressed: and see if that method is getting called. Maybe the method is getting called but the Photo Gallery app isn't launching for some other reason.
The user cannot click the button if you set the button's alpha to 0. This means the event will not be fired.
[button setAlpha:0];
This has the same effect as setting button.hidden = YES.
If you want an invisible clickzone using the button, set the button type to UIButtonTypeCustom and it should do the trick.

Can I add OBject of button to NSMutableArray?

I am trying to create number of buttons on view programatically and for that I need array, so can I add button object in NSMutableArray?
Yes you can do that see the following code
// Create buttons for the sliding menu. For simplicity I create 5 standard buttons.
NSMutableArray *buttonArray = [[NSMutableArray alloc] init];
for(NSInteger i = 0; i < [self.slideMenuArray count]; i++)
{
// Rounded rect is nice
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
NSString *title=[slideMenuArray objectAtIndex:i];
[btn setFrame:CGRectMake(0.0f, 4.0f, 90.0f, 20.0f)];
[btn setTitle:[NSString stringWithString:title] forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor clearColor]];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[[btn titleLabel] setFont:[UIFont systemFontOfSize:12]];
[btn addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
UIImage *backgroundView;
if(i==0)
backgroundView= [UIImage imageNamed:#"btnclk.png"];
else
backgroundView= [UIImage imageNamed:#"btn.png"];
[btn setBackgroundImage:backgroundView forState:UIControlStateNormal];
[buttonArray addObject:btn];
}
You can retrieve the added button objects like this-
for(int i = 0; i < [buttonArray count]; i++)
{
UIButton *btn = [buttonArray objectAtIndex:i];
// Move the buttons position in the x-demension (horizontal).
CGRect btnRect = btn.frame;
btnRect.origin.x = totalButtonWidth;
[btn setFrame:btnRect];
}
sure you can, and remember that when you do, every button or other object will be retained bu the array and released when you remove it from the array...
Yes, NSMutableArray can hold any objects