Search code examples
iphoneobjective-ctagsuibuttonwallpaper

set iphone wallpaper with array of buttons objective c


Hi. I am developing an app and I would like to set the background in the app itself. Now I created an array of buttons using a for loop and setting tags, but for some reason when I click the buttons nothing happens.

This is where the buttons are created:

-(void)showSettings {
  [[objc_getClass("DreamBoard") sharedInstance] hideAllExcept:mainView];
  [UIView beginAnimations:nil context:nil];
  [UIView setAnimationDuration:.5];

  if(toggled){
    photos = [[NSArray arrayWithObjects:   
                [UIImage imageWithContentsOfFile:@"/var/mobile/Library/iZoon/Images/Wallpapers/Wallpaper1.png"],
                [UIImage imageWithContentsOfFile:@"/var/mobile/Library/iZoon/Images/Wallpapers/Wallpaper2.png"],
                [UIImage imageWithContentsOfFile:@"/var/mobile/Library/iZoon/Images/Wallpapers/Wallpaper3.png"],
                [UIImage imageWithContentsOfFile:@"/var/mobile/Library/iZoon/Images/Wallpapers/Wallpaper4.png"],
                 nil] retain];

    w=0;
    for ( UIImage *image in photos )
    {                          
        for (l = 0; l<(int)photos.count; l++) 
        {
            wallpaperButton = [[UIButton alloc] initWithFrame:CGRectMake(5,130+150*w,150,150)];

            wallpaperButton.tag = l;

            [wallpaperButton setImage:image forState:UIControlStateNormal];

            [wallpaperButton addTarget:self action:@selector(setWallpaper) forControlEvents:UIControlEventTouchDown];

            [settingsMenu addSubview: wallpaperButton];
            [wallpaperButton release];

        }

        w++;
    }

Here is the function to set the wallpaper:

-(void)setWallpaper {

UIImageView *bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];

if ([wallpaperButton tag] == 1) {
  bgView.image = [UIImage imageWithContentsOfFile:@"/var/mobile/Library/iZoon/Images/Wallpapers/Wallpaper1.png"];
}
else if ([wallpaperButton tag] == 0) {
  bgView.image = [UIImage imageWithContentsOfFile:@"/var/mobile/Library/iZoon/Wallpapers/Wallpaper2.png"];   
}
else if ([wallpaperButton tag] == 2) {
  bgView.image = [UIImage imageWithContentsOfFile:@"/var/mobile/Library/iZoon/    Wallpapers/Wallpaper3.png"];  
}
else if ([wallpaperButton tag] == 3) {
  bgView.image = [UIImage imageWithContentsOfFile:@"/var/mobile/Library/iZoon/Wallpapers/Wallpaper4.png"]; 
}

[mainView insertSubview:bgView atIndex:0];
[bgView release];  

}

Now the buttons display correctly but they don't do anything. Any help is appreciated. Thanks.


Solution

  • Add an NSLog statement to see if the setWallpaper method is being called. If it is, then i think this might be a fix:

    If you just add the images to your XCode Project, you can just get them by their name. Make sure you check the "copy files to project folder if needed" box when you drag the images into the project. Then to get the image you can just do this:

    if ([wallpaperButton tag] == 1) {
        bgView.image = [UIImage imageNamed:@"Wallpaper1.png"];
    }
    .
    .
    .
    [self.view addSubview:bgView];
    

    Also, you should be aware that unless your app is hiding the status bar (the one with the battery and clock) then your images should actually have a height of 460 because 20px are used up by that.