2012-07-11 92 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