2012-07-11 90 views
1

您好的阵列。我正在开发一个应用程序,我想在应用程序本身设置背景。 现在我创建使用for循环和设置标签按钮的排列,但由于某些原因,当我点击按钮,没有任何反应。设置iphone壁纸与按钮目标C

这就是按钮创建:

-(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++; 
    } 

这里是设置壁纸的功能:

-(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]; 

}

现在的按钮显示正确,但他们不这样做做任何事情。 任何帮助表示赞赏。谢谢。

回答

2

添加的NSLog语句,看看是否setWallpaper方法被调用。如果是的话,我认为这可能是一个修复:

如果你只是将图像添加到您的Xcode项目,你可以通过它们的名字让他们。将图像拖放到项目中时,请确保选中“将文件复制到项目文件夹”(如果需要)复选框。然后,让你可以做这样的形象:

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

此外,你应该知道,除非你的应用程序就隐藏状态栏(在一个与电池和时钟),那么你的形象实际上应该有一个高度460,因为20px被用完了。

+0

谢谢!在添加了NSLog之后,我能够调整并找出错误发生的位置。我之前从未真正使用它。谢谢! – 2012-07-12 00:24:37