在我的應用程序中,當按下按鈕時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];
}
什麼是myButton的在viewDidAppear的?你想根據selectedImage得到這個按鈕不是? – Eyal 2012-07-19 08:30:21
不只是整數,我的按鈕是我實例化的按鈕 – Bazinga 2012-07-19 08:41:38
如果我理解正確,你的CustomImagePicker有一個按鈕的網格,每個按鈕都有一個圖像。所以在viewDidAppear你應該得到所選按鈕的索引並獲得相關的按鈕(你已經在viewDidLoad中創建了),爲什麼你要創建一個新的按鈕? – Eyal 2012-07-19 08:48:32