UIButtons in UIScrollView stop it scrolling, how to fix? - iphone

I have a problem with adding a set of UIButtons to a UIScrollView. With the buttons added it seems that the scroll command is not being passed to the scroll view as the buttons cover the whole surface. I've read various posts on this but still can't figure it out. I'm pretty new to iPhone programming so there's probably something obvious I've missed. I've tried things like canCancelContentTouches and delaysContentTouches to TRUE and FALSE.
Here is the code I use to add the buttons to the UIScrollView. The UIScrollView was created in IB and is passed in to the function:
-(void)drawCharacters:(NSMutableArray *)chars:(UIScrollView *)target_view {
//NSLog(#"CLEARING PREVIOUS BUTTONS");
//clear previous buttons for parts/kanji
if(target_view == viewParts){
for (UIButton *btn in parts_buttons) {
[btn removeFromSuperview];
}
[parts_buttons removeAllObjects];
} else if(target_view == viewKanji){
for (UIButton *btn in kanji_buttons) {
[btn removeFromSuperview];
}
[kanji_buttons removeAllObjects];
}
//display options
int chars_per_line = 9; //change this to change the number of chars displayed on each line
if(target_view == viewKanji){
chars_per_line = 8;
}
int char_gap = 0; //change this to change the margin between chars
int char_dimensions = (320/chars_per_line)-(char_gap*2);
//set starting x, y coords
int x = 0, y = 0;
//increment y coord
y += char_gap;
//NSLog(#"ABOUT TO DRAW FIRST BUTTON");
for(NSMutableArray *char_arr in chars){
//increment x coord
x += char_gap;
//draw at x and y
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(x, y, char_dimensions, char_dimensions); // position in the parent view and set the size of the button
[myButton setTitle:[char_arr objectAtIndex:0] forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1.0] forState:UIControlStateDisabled];
myButton.titleLabel.font = [UIFont boldSystemFontOfSize:22];
if(target_view == viewKanji){
myButton.titleLabel.font = [UIFont boldSystemFontOfSize:30];
}
// add targets and actions
if(target_view == viewParts){
[myButton addTarget:self action:#selector(partSelected:) forControlEvents:UIControlEventTouchUpInside];
} else if(target_view == viewKanji){
[myButton addTarget:self action:#selector(kanjiSelected:) forControlEvents:UIControlEventTouchUpInside];
[myButton setTag:(int)[char_arr objectAtIndex:1]];
}
//if the part isnt in the current list of kanji, disable and dim it
if(target_view == viewParts){
if([kanji count] > 0){
[myButton setEnabled:NO];
}
bool do_break = NO;
for(NSMutableArray *arr in kanji){
//NSLog([NSString stringWithFormat:#"CHECKING PARTS AGAINST %d KANJI", [kanji count]]);
for(NSString *str in [[arr objectAtIndex:2] componentsSeparatedByString:#" "]){
if(([myButton.titleLabel.text isEqualToString:str])){
//NSLog(#"--------------MATCH!!!-----------------");
[myButton setEnabled:YES];
for(NSString *str1 in parts_selected){
if(([myButton.titleLabel.text isEqualToString:str1])){
[myButton setEnabled:NO];
break;
}
}
do_break = YES;
break;
}
if(do_break) break;
}
if(do_break) break;
}
}
// add to a view
[target_view addSubview:myButton];
//update coords of next button
x += char_dimensions+ char_gap;
if (x > (320-char_dimensions)) {
x = 0;
y += char_dimensions + (char_gap*2);
}
//add button to global array to be removed from view next update
if(target_view == viewParts){
[parts_buttons addObject:myButton];
} else if(target_view == viewKanji){
[kanji_buttons addObject:myButton];
}
}
//NSLog(#"FINISHED DRAWING ALL BUTTONS");
}
Any help on this would be greatly appreciated. I just need to fix this to finish my app.

You cannot expect the scrollview to receive events if the buttons cover the whole surface.(They are going to be first in the responder chain). The quick fix would be to make the buttons slightly smaller than the scrollview.
On first read of this problem I was thinking that you wanted to add buttons to the scrollview's superview -- but then I realized you actually want the buttons in the scrollview.

Related

How to change image of button

I am using this code for displaying Images in scroll view... In this buttons are created with the help of for loop... and then set image for every buttons... Now i want to select multiple images... I want when i click on particular button, its image replace with "tick image" and when i again pressed on it, replace with original image means show unchecked..
for(int i=0; i<[imageArray count]; i++)
{
if((i%4) == 0 && i!=0)
{
horizontal = 8.0;
vertical = vertical + 70.0 + 8.0;
}
buttonImage = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonImage setFrame:CGRectMake(horizontal, vertical, 70.0, 70.0)];
[buttonImage setTag:i];
[buttonImage setImage:[arrayOfImages objectAtIndex:i] forState:UIControlStateNormal];
[buttonImage addTarget:self action:#selector(buttonImagePressed:) forControlEvents:UIControlEventTouchUpInside];
[myScrollView addSubview:buttonImage];
horizontal = horizontal + 70.0 + 8.0;
}
I tried this code for image changed on state change in (buttonImagePressed) method...
[buttonImage setImage:[UIImage imageNamed:#"Checkmark.png"] forState:UIControlStateSelected];
but it doesn't work... and it change only image of last button every time instead of particular clicked button.... I also tried to hide the button but it again hide only last button. where i m doing wrong???
there is any another way to change it??? please help me
Instead of UIControlStateSelected you should use UIControlStateNormal and keep some bool value which shows whether that button is previously selected or not or you can set selected property of button to true or false based on selection say:
-(void) buttonImagePressed:(UIButton*)sender
{
UIButton *button = sender;
if(button.selected) //already selected so now it should be deselected
{
[button setImage:[UIImage imageNamed:#"UnCheckmark.png"] forState:UIControlStateNormal];
button.selected = false;
}
else
{
[button setImage:[UIImage imageNamed:#"Checkmark.png"] forState:UIControlStateNormal];
button.selected = true;
}
}
For this, Take a global flag variable as BOOL Flag = NO;
Set tag for each button also.
In the method
-(IBAction)buttonImagePressed:(UIButton *)sender{
if(flag==NO){
flag=YES;
[sender setImage:[UIImage imageNamed:#"Checkmark.png"] forState:UIControlStateNormal];
}
else{
flag=NO;
[sender setImage:[arrayOfImages objectAtIndex:sender.tag] forState:UIControlStateNormal];
}
}

UIButton and actions problem

I am creating some UIbutton dynamically. And a user click any one of the buttons will display something (ex: different views). So I successfully made the buttons, get the tags. But in the IBAction method below, because all these buttons are dynamically created. So I can't use if, else if statement to show the view based on the tag number. I am thinking of using loops? any ideas?
Here is my code:
NSMutableArray *buttonsArray = [[NSMutableArray alloc] initWithObjects:nil];
for(int i = 0; i < [someArray count]; i++)
{
button = [[UIButton alloc] initWithFrame:CGRectMake(btnX,btnY,btnW,btnH)];
button.tag = i;
[buttonsArray addObject:button];
[[buttonsArray objectAtIndex:i] addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
button.titleLabel.text = [NSString stringWithFormat:#"Click it"];
[self.view addSubview:button];
btnY = btnY + 120;
}
`-(IBAction) buttonPressed:(id)sender `
{
UIButton *btn = (UIButton *)sender;
NSLog(#"%ld", btn.tag);
//Don't know the number of buttons, so this is not gonna work
if( btn.tag == 1)
{
//do something
}
if( btn.tag == 2)
{
//do something
}
if( btn.tag == 3)
{
//do something
}
if( btn.tag == 4)
{
//do something
}
}
loop is not the perfect solution here i think...agree with Jennis...use switch instead of if else statement...if you want to use loop try:
-(IBAction) buttonPressed:(id)sender{
UIButton *selectedbtn = (UIButton *)sender;
for (UIButton *bttn in buttonsArray) {
if ([bttn.tag == selectedbtn.tag ) {
//do something
}
}
}
you get Multiple UIbutton ? b'coz your X and Y are same for all..
All is OK in your code except two things.
(1) Change the coordinates of buttons so you get different position for all the buttons in the view.
(2) Change the following code:
[[buttonsArray objectAtIndex:i] addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
with this code:
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
Let me know if any further help required.

how to creating dynamic buttons iphone xcode?

I want to create uibuttons dynamically. so i for loop to create button with tag and added to view.
all buttons performs the same action with respect to its tag value... but i want to change the propery of the button. Therefore i need to get the uibutton using the tag value...
my code...
UIButton *button2;
//view did load
int width = 30;
for(int i = 1; i <= 5; i++)
{
button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 addTarget:self action:#selector(ratingAction:) forControlEvents:UIControlEventTouchUpInside];
[button2 setBackgroundImage:[UIImage imageNamed:#"star1.png"] forState:UIControlStateNormal];
button2.tag = i;
button2.backgroundColor = [UIColor clearColor];
button2.frame = CGRectMake(width, 78, 15, 15);
[self.view addSubview:button2];
width = width +30;
}
-(void)ratingAction:(id*)sender
{
// here using the tag value i want to get the button and change the background image….
// for example i want to change the background for tag values 1,3,6,7 ,8…
}
Use viewWithTag function of UIView to access your UIButton using the tag value.
See in Documentation viewWithTag
Use it as below.
UIButton* myButton = (UIButton*)[mySuperView viewWithTag:1];
UIButton* myButton = (UIButton*)[mySuperView viewWithTag:3];
UIButton* myButton = (UIButton*)[mySuperView viewWithTag:6];
.........
-(void)ratingAction:(id*)sender
{
// here using the tag value i want to get the button and change the background image….
// for example i want to change the background for tag values 1,3,6,7 ,8…
if ([sender isKindOfClass:[UIButton class]])
{
UIButton *temp=(UIButton*)sender;
if ([temp tag]==1 || [temp tag]==3 || [temp tag]==6 || [temp tag]==7 )
{
[temp setBackgroundColor:[UIColor redColor]];
}
}
}
Check this code
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
int y = 100;
for (int i=0; i<=5; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80, y, 160, 40);
[button setTitle:[NSString stringWithFormat:#"Button-%i", i] forState:UIControlStateNormal];
// [button set
[self.view addSubview:button];
y=y+60;
}
}
-(void)aMethod:(id)sender{
UIButton *button = sender;
int y = 100;
int i = 5;
for (int x=0; x<=i; x++) {
NSString *frameString = [NSString stringWithFormat:#"{{80, %i}, {160, 40}}", y];
if ([NSStringFromCGRect(button.frame) isEqualToString:frameString]) {
NSLog(#"button %i clicked..", x);
}
y= y+60;
}
}

how do I only allow one selected button at a time? / make button know if i click somewhere else

how do i make these buttons so that only one can be used at a time? Im not getting any errors right now when i run btw. Im just looking for a solution to my challenge. Thanks for any help
they are generated in a for loop like this:
for (int l=0; l<list.length; l++) {
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton setTag:l];
CGRect buttonRect = CGRectMake(11+charact*20, -40 + line*50, 18, 21);
aButton.frame = buttonRect;
[aButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[aButton setTitle:#" " forState:UIControlStateNormal];
[gameScroll addSubview:aButton];
}
And then the action for when a button is clicked is:
- (void) buttonClicked:(UIButton *)sender {
int tag = sender.tag;
if (sender.selected == TRUE) {
[sender setSelected:FALSE];
[sender setBackgroundColor:[UIColor clearColor]];
}
else if (sender.selected == FALSE) {
[sender setSelected:TRUE];
[sender setBackgroundColor:[UIColor redColor]];
}
}
right now everything works but i want it to know if theres already a button selected and deselect that other button, or else to automatically deselect any time the user clicks outside of the range of that button
thanks in advance
I would suggest to put all ur buttons to Array in your button initialisation
NSMutableArray* buttons = [NSMutableArray arrayWithCapacity: list.length];
for (int l=0; l<list.length; l++) {
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton setTag:l];
CGRect buttonRect = CGRectMake(11+charact*20, -40 + line*50, 18, 21);
aButton.frame = buttonRect;
[aButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[aButton setTitle:#" " forState:UIControlStateNormal];
[gameScroll addSubview:aButton];
[buttons addObject: aButton];
}
and every time buttonClicked triggered, then do your logic:
for (UIButton* button in buttons)
{
if (button != sender)
{
[button setSelected: FALSE];
[button setBackgroundColor:[UIColor redColor]];
}
}
int tag = sender.tag;
if (sender.selected == TRUE) {
[sender setSelected:FALSE];
[sender setBackgroundColor:[UIColor clearColor]];
}
else if (sender.selected == FALSE) {
[sender setSelected:TRUE];
[sender setBackgroundColor:[UIColor redColor]];
}
hope helps :)
You can store currently selected button in a separate variable and deselect it in buttonClicked: method:
- (void) buttonClicked:(UIButton *)sender {
int tag = sender.tag;
currentButton.selected = NO;
if (currentButton != sender){
currentButton = sender;
currentButton.selected = YES;
}
else{
currentButton = nil;
}
}
Also you can specify background colour for each state in button itself so you actually don't need to change it each time manually
If you also want to deselect your button when user just touches the screen you can implement touchesEnded:withEvent: in your view controller and reset currentButton in it (that method will get called if no other control intercept the touch event - so it may not be sufficient in all cases)
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event{
currentButton.selected = NO;
currentButton = nil;
}

Setting Property on UIButton in Loop

I'm spinning in circles on the best way to implement this, so maybe you can help me out:
How can I make it so when the UIButton responds to the selector highlightImage, it also can set the desired challengeId = the index of my appDelegate.availableArray?
In my cellForRowAtIndexPath method, I'm creating a UIScrollView with buttons:
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, tv.frame.size.width, 78)];
[scrollView setContentSize:CGSizeMake(500, 78)];
[self createScrollButtons];
[[cell contentView] addSubview:scrollView];
My createScrollButtons loops and creates buttons in the scrollView as so, looping over the number of elements in my availableArray
- (void) createScrollButtons
{
SuperHeroGameAppDelegate *appDelegate = (SuperHeroGameAppDelegate *)[[UIApplication sharedApplication] delegate];
int x = 0;
int y = 0;
NSLog(#"Challenge array = %#",appDelegate.availableArray);
for (int i = 0; i < [appDelegate.availableArray count]; i++) {
NSLog(#" ID= %d", [[[appDelegate.availableArray objectAtIndex:i]valueForKey:#"id"] intValue]);
[self createButtonAtX:x AndY:y withChallenge:[[appDelegate.availableArray objectAtIndex:i]valueForKey:#"id"]];
x += 90;
}
}
createButtonAtX andY withChallenge is called for each element inavailableArray`, positioning them correctly along the scrollView.
- (void)createButtonAtX:(int) x AndY:(int) y withChallenge:(id)challengeId
{
CGRect buttonRect = CGRectMake(x, y, 80, 78);
ChallengeButton *capeButton = [ChallengeButton buttonWithType:UIButtonTypeCustom];
[capeButton setBackgroundColor:[UIColor clearColor]];
[capeButton setFrame:buttonRect];
[capeButton addTarget:self action:#selector(highlightImage:) forControlEvents:UIControlEventTouchUpInside];
UIImage *capeImage = [UIImage imageNamed:#"CapePower.png"];
[capeButton setBackgroundImage:capeImage forState:UIControlStateNormal];
[scrollView addSubview: capeButton];
}
highlightImage is called as each buttons selector, to determine which button is pressed. (and ideally with element from the availableArray that button is associated with)... I've subclasses UIButton and added an int value to be able to call [sender setChallengeId:]
- (void) highlightImage:(id)sender
{
if([sender isSelected] == NO){
[sender setSelected:YES];
[sender setChallengeId:selectedChallenge];
NSLog(#"sender = %d",[sender challengeId]);
UIImage *selectedImage = [UIImage imageNamed:#"PowerBarHand.png"];
[sender setBackgroundImage:selectedImage forState:UIControlStateSelected];
}
else {
[sender setBackgroundImage:[UIImage imageNamed:#"HandstandPower.png"] forState:UIControlStateNormal];
[sender setSelected:NO];
selectedChallenge = 0;
}
}
My problem is I cannot figure out how to get the appDelegate's availableArray element INTO my highlightImage, as UIButton's [capeButton addTarget:challengeId action:#selector(highlightImage:) forControlEvents:UIControlEventTouchUpInside] will not allow me to pass the challengeId from the loop earlier in the flow.
I am able to pass any integer value to setChallengeId, and it is handling as expected. I just need to get the appDelegate.availableArray id value from the loop.
Any direction would be great!:)
You get availableArray into highlightImage: the same way you got it into createScrollButtons:
SuperHeroGameAppDelegate *appDelegate = (SuperHeroGameAppDelegate *)[[UIApplication sharedApplication] delegate];
and then access appDelegate.availableArray.
I think that's not the question you actually meant to ask, though. I think what you want is to call setChallengeId: on each ChallengeButton as you create it in createButtonAtX:AndY:withChallenge:. Then in highlightImage: do selectedChallenge = [sender challengeId] rather than [sender setChallengeId:selectedChallenge].