Set image Button Toggle

Discussion in 'iOS Programming' started by 204, Jun 6, 2013.

  1. 204 macrumors newbie

    Joined:
    Jun 6, 2013
    #1
    hi I would like to ask how I can set the image of the various button2 created from users on my uiview . please help me!!
    Code:
    
    
    Code:
    - (void)CREATEBUTTON:(id)sender {
         i++;
    UIButton *button2 =[[UIButton alloc] initWithFrame:CGRectMake(100, 80, 55 , 55)];
            NSString* buttonTitle = [NSString stringWithFormat:@"Button%d", i];
            [button2 setTitle:buttonTitle forState:UIControlStateNormal];
            NSString *selectorName = [NSString stringWithFormat:@"SwitchButton%d:", i];
            [button2 addTarget:self action:NSSelectorFromString(selectorName) forControlEvents:(UIControlEvents)UIControlEventTouchDown];
    [button2 setBackgroundImage:[UIImage imageNamed:@"OFF.png"]
                            forState:UIControlStateNormal];
     [button2 setBackgroundImage:[UIImage imageNamed:@"ON.png"] forState:UIControlStateSelected];
        buttonToggle = NO;
            [self.view addSubview:button2];
    
    }
    
    - (void)SwitchButton1:(UIButton*)sender
    {
    	buttonToggle = !buttonToggle;
    
        if (buttonToggle == YES)
        {
            NSLog(@"1ON");
     }
        else
        {
    NSLog(@"1OFF");
    
        }
    }
    - (void)SwitchButton2:(UIButton*)sender
    {
    	buttonToggle = !buttonToggle;
    
        if (buttonToggle == YES)
        {
            NSLog(@"2ON");
     }
        else
        {       
    NSLog(@"2OFF");
    
        }
    }
    
    
     
  2. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #2

    You can't ask a coherent question on iPhone Dev SDK, so you come here instead? You need to learn to communicate better and people will help you.
     
  3. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #3
    I would suggest making a stately subclass of UIButton and setting the image when it's clicked, based on the state (which should also change on the click.)
     
  4. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #4
    I have a project that does exactly what this guy wants on github, but he can't communicate clearly enough to either understand what he's being told or ask intelligible questions.

    WTCheckBox Demo project on github

    He also won't use proper code tags over on iPhone Dev SDK, so the code he posts is illegible.
     

Share This Page