UISwitch

Discussion in 'iOS Development' started by maxk96, Jul 22, 2010.

  1. maxk96

    maxk96 New Member

    Joined:
    Jul 8, 2009
    Messages:
    37
    Likes Received:
    0
    Device:
    2G iPod touch
    Hi, I have programmed a lot of UISwitches to change the UIImageView image. However they are not working. When I switch views on the App it brings up all of the switches and when I turn one of the switches on the UIImageView *light does not change.

    If you could tell me what I am doing wrong that would be excellent.

    Here is my code

    LightViewController.h
    [OBJC]
    IBOutlet UIImageView *light;
    }
    - (IBAction)infoButton;
    - (IBAction)color;
    - (IBAction)back; [/OBJC]
    LightViewController.m
    [OBJC]
    @implementation iLightViewController
    - (IBAction)infoButton
    {
    InfoVC *info = [[InfoVC alloc] initWithNibName:nil bundle:nil];
    info.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:info animated:UIModalTransitionStyleCoverVertical];
    [info release];
    }
    - (IBAction)color
    {
    SwitchVC *switchv = [[SwitchVC alloc] initWithNibName:nil bundle:nil];
    switchv.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:switchv animated:YES];
    [switchv release];
    } [/OBJC]
    SwitchVC.h
    [OBJC]
    IBOutlet UIImageView *light;
    }
    - (IBAction)infoButton;
    - (IBAction)color;
    - (IBAction)back;
    [/OBJC]
    SwitchVC.m
    [OBJC]
    #import "SwitchVC.h"
    #import "iLightViewController.h"
    #import "InfoVC.h"


    @implementation SwitchVC
    - (IBAction)infoButton
    {
    InfoVC *info = [[InfoVC alloc] initWithNibName:nil bundle:nil];
    info.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:info animated:UIModalTransitionStyleCoverVertical];
    [info release];
    }
    - (IBAction)color
    {
    SwitchVC *switchv = [[SwitchVC alloc] initWithNibName:nil bundle:nil];
    switchv.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:switchv animated:YES];
    [switchv release];
    }
    - (IBAction)back
    {
    [self dismissModalViewControllerAnimated:UIModalTransitionStyleCrossDissolve];
    }
    - (void)switchswitchRed {
    if (switchRed.on) {
    light.image = [UIImage imageNamed

    Please Register or Log in to view images

    "Red.png"];
    }
    }
    - (void)switchswitchDarkBlue {
    if (switchDarkBlue.on) {
    light.image = [UIImage imageNamed

    Please Register or Log in to view images

    "Dark Blue.png"];
    }
    }
    - (void)viewDidLoad {
    [light setImage:[UIImage imageNamed

    Please Register or Log in to view images

    "light.png"]];;
    [switchRed addTarget:self action

    Please Register or Log in to view images

    selector(switchswitchRed) forControlEvents:UIControlEventValueChanged];
    [switchDarkBlue addTarget:self action

    Please Register or Log in to view images

    selector(switchswitchDarkBlue) forControlEvents:UIControlEventValueChanged];
    [switchGreen addTarget:self action

    Please Register or Log in to view images

    selector(switchswitchGreen) forControlEvents:UIControlEventValueChanged];
    [switchLightBlue addTarget:self action

    Please Register or Log in to view images

    selector(switchswitchLightBlue) forControlEvents:UIControlEventValueChanged];
    [switchOrange addTarget:self action

    Please Register or Log in to view images

    selector(switchswitchOrange) forControlEvents:UIControlEventValueChanged];
    [switchPink addTarget:self action

    Please Register or Log in to view images

    selector(switchswitchPink) forControlEvents:UIControlEventValueChanged];
    [switchPurple addTarget:self action

    Please Register or Log in to view images

    selector(switchswitchPurple) forControlEvents:UIControlEventValueChanged];
    [switchYellow addTarget:self action

    Please Register or Log in to view images

    selector(switchswitchYellow) forControlEvents:UIControlEventValueChanged];
    [switchLight addTarget:self action

    Please Register or Log in to view images

    selector(switchswitchLight) forControlEvents:UIControlEventValueChanged];
    [super viewDidLoad];
    } [/OBJC]
  2. lauNchD

    lauNchD Well-Known Member

    Joined:
    Jan 27, 2008
    Messages:
    1,844
    Likes Received:
    261
    Device:
    iPhone 5 (Black)
    1. Please use [NOPARSE][OBJC]<your code>[/OBJC][/NOPARSE] and indent your code properly next time. It's _incredibly_ hard to read right now.

    2. Another flashlight app? SERIOUSLY?

    3. Not related to the problem, but the animated: argument in your info button method takes a BOOL, not a transition style. I wonder why you have the correct argument (YES) in your "color" method.

    4. In both your info and color methods, you're passing nil as the nib name, which means no nib will be loaded for them, and thus, none of your IBOutlets get connected and none of your IBActions will ever be called. I don't understand how your switches even show up; there must be something you're now showing. How many nib files do you even have?

    5. Even if your objects in the nib are instantiated correctly, how are you connecting them? Unless everything is in the same nib file (then you shouldn't create new view controllers, but it's a bad practice anyway), you somehow share objects and variables, and/or implement delegation/notifications, how do you know one object is adressing the correct one?

    6. Generally, you should really rethink your GUI. Having a bunch of switches on screen implies, for example, that you have red, blue, yellow, green and purple on at the same time, although in reality, only one (or even zero, if gets switched off; the color still stays there) state is used; the others just "sit there" and are ignored.
    You should at least consider using UIButtons, but a table view or a custom grid would make for a more *cough* aesthetically pleasing flashlight app.

    Did I miss something?
  3. maxk96

    maxk96 New Member

    Joined:
    Jul 8, 2009
    Messages:
    37
    Likes Received:
    0
    Device:
    2G iPod touch
    thanks, goanna check it out nnow.
    ------------------double post merged------------------
    My main problem is that light (the uiimageview) is undeclared in SwitchVC. Once that is fixed initially it should work.

Share This Page