iOS Set image Button Toggle

204

macrumors newbie
Original poster
Jun 6, 2013
1
0
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");

    }
}
 

Duncan C

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
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");

    }
}

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.
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
8,574
4,005
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.)
 

Duncan C

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
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.)
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.