#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    1
    Rep Power
    0

    Set Image Button Toggle


    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:
    - (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. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    Dallas, TX
    Posts
    50
    Rep Power
    2
    I should be able to help with this, but I'm not 100% sure what you're trying to do. Are you simply trying to toggle the buttons on and off when you tap on either of them (i.e. changing out the background image to ON.PNG and OFF.png, and vice versa when they continue tapping)?

IMN logo majestic logo threadwatch logo seochat tools logo