2012-07-19 134 views
1

在我的應用程序中,當按下按鈕時n自定義imagePicker出現,它包含我的圖像NSDocumentDirectory,我想要的是當我選擇一個圖像時,它將:HIDE,REMOVE,SET as SELECTED,或任何會考慮它選擇的東西。設置UIButton爲

我試過的是給它分配一個整數,所以下次我提出imagePicker它會做上面的任一個,但是,沒有運氣。

你能建議我該如何解決我的問題或任何建議。謝謝。

這裏是我的代碼:

- (void)viewDidLoad { 

    // Create view 
    UIScrollView *view = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f,0.0f,1024.0f,768.0f)]; 

    int row = 0; 
    int column = 0; 
    for(int i = 0; i < _thumbs.count; ++i) { 

     UIImage *thumb = [_thumbs objectAtIndex:i]; 
     UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom]; 
     button.frame = CGRectMake(column*140+24, row*150+10, 100, 100); 
     [button setImage:thumb forState:UIControlStateNormal]; 
     [button addTarget:self 
        action:@selector(buttonClicked:) 
     forControlEvents:UIControlEventTouchUpInside]; 
     button.tag = i; 


     [view addSubview:button]; 

     if (column == 6) { 
      column = 0; 
      row++; 
     } else { 
      column++; 
     } 

    } 

    [view setContentSize:CGSizeMake(1024, (row+1) * 150 + 10)]; 
    self.view = view; 

    [super viewDidLoad]; 
} 

這裏是我在它選擇的設置嘗試:

- (void) viewDidAppear:(BOOL)animated{ 
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
    NSInteger selectedImage = [prefs integerForKey:@"selected"]; 
    if (selectedImage){ 
     [myButton removeFromSuperview]; 
     [myButton setBackgroundImage:[UIImage imageNamed:@"Checkmark.png"] forState:UIControlStateSelected]; 
     [myButton setSelected:YES]; 
     [myButton setHidden:YES]; 
     [myButton setHighlighted:YES]; 
     myButton.selected = YES; 
    }  
} 

點擊時:

- (IBAction)buttonClicked:(id)sender { 
    self.myButton = (UIButton *)sender; 
    self.selectedImage = [_images objectAtIndex:myButton.tag]; 

    [[NSUserDefaults standardUserDefaults] setInteger:myButton.tag forKey:@"selected"]; 


    [[NSUserDefaults standardUserDefaults] synchronize]; 

    [self dismissModalViewControllerAnimated:NO]; 
} 

調用imagePicker

- (IBAction)chooseCustomImageTapped:(id)sender { 
    _imagePicker1 = [[CustomImagePicker alloc] init]; 
    _imagePicker1.title = @"Choose Custom Image"; 
    for(int i = 0; i <= 30; i++) 
    { 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDir = [paths objectAtIndex:0]; 

     NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Images%d.png", i]]; 
     if([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]){ 
      [_imagePicker1 addImage:[UIImage imageWithContentsOfFile:savedImagePath]]; 

     } 
    } 

    [self presentModalViewController:_imagePicker1 animated:NO]; 

} 
+0

什麼是myButton的在viewDidAppear的?你想根據selectedImage得到這個按鈕不是? – Eyal 2012-07-19 08:30:21

+0

不只是整數,我的按鈕是我實例化的按鈕 – Bazinga 2012-07-19 08:41:38

+0

如果我理解正確,你的CustomImagePicker有一個按鈕的網格,每個按鈕都有一個圖像。所以在viewDidAppear你應該得到所選按鈕的索引並獲得相關的按鈕(你已經在viewDidLoad中創建了),爲什麼你要創建一個新的按鈕? – Eyal 2012-07-19 08:48:32

回答

0

嘗試這樣做是爲了改變按鈕看起來選擇:

- (void)turnButtonToSelected 
{ 
    UIImage *bgImage = [UIImage imageNamed:@"Checkmark.png"]; 
    [self.button setBackgroundImage:bgImage forState:UIControlStateNormal]; 
    [self.button setBackgroundImage:bgImage forState:UIControlStateHighlighted]; 
    [self.button setBackgroundImage:bgImage forState:UIControlStateDisabled]; 
} 

但我必須說,我不明白這是什麼按鈕,在那裏做u到位了嗎?
這是網格上的按鈕之一嗎?

在viewDidAppear將這個,而不是你的代碼

- (void) viewDidAppear:(BOOL)animated{ 
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
    NSInteger selectedImage = [prefs integerForKey:@"selected"]; 
    if (selectedImage){ 
     for (UIView *subview in self.view.subviews) { 
      if ([subview isKindOfClass:[UIButton class]] && subview.tag == selectedImage) { 
       [self turnButtonToSelected]; 
      } 
     } 
    }  
} 
+0

帶圖像的按鈕 – Bazinga 2012-07-19 09:18:14

+0

但在viewDidLoad中,您將創建所有按鈕並將它們放置在視圖中,然後在viewDidAppear中創建一個新按鈕並將其放置在您已有按鈕的相同位置??(按鈕在viewDidLoad中創建) – Eyal 2012-07-19 09:21:44

+0

我只是想當它被選中時,下一次我彈出選擇器,它仍然預覽爲選中,這就是爲什麼我已經做到了這一點。每次我彈出選取器我想選擇鞋子,它被選中 – Bazinga 2012-07-19 09:32:57