所以我有一個UIScrollview
與UIImageView
集的一個按鈕,我希望能夠每當圖像被點擊,如果選擇YES,然後該圖像將在NSDocumentDirectory
被刪除的alertView
將彈出。我設法使alertView出現,但圖像不刪除,因爲我認爲發送了錯誤的sender
或button.tag。這裏是我的代碼:alertView沒有響應
//我滾動視圖
UIScrollView *scrollView1 = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f,0.0f,300.0f,134.0f)];
[self.view addSubview:scrollView1];
int row = 0;
int column = 0;
for(int i = 0; i < _thumbs1.count; ++i) {
UIImage *thumb = [_thumbs1 objectAtIndex:i];
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(column*60+10, row*60+10, 60, 60);
[button setImage:thumb forState:UIControlStateNormal];
[button addTarget:self
action:@selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
[scrollView1 addSubview:button];
if (column == 4) {
column = 0;
row++;
} else {
column++;
}
//按鈕
- (IBAction)buttonClicked:(id)sender {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSInteger slotBG = [prefs integerForKey:@"integerKey"];
if(slotBG == 1){
UIAlertView *deleteMessage = [[UIAlertView alloc] initWithTitle:@""
message:@"DELETE?"
delegate:self
cancelButtonTitle:@"NO"
otherButtonTitles:@"YES", nil];
[deleteMessage show];
}
//我AlertView
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:@"YES"]){
// I KNOW THIS IS SOMEWHAT WRONG BECAUSE OF THE SENDER having errors with it
UIButton *button = (UIButton *)sender;
[button removeFromSuperview];
[_images objectAtIndex:button.tag];
[_images removeObjectAtIndex:button.tag];
[_images insertObject:[NSNull null] atIndex:button.tag];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"oneSlotImages%lu.png", button.tag]];
[fileManager removeItemAtPath: fullPath error:NULL];
NSLog(@"image removed");
}
感謝您的幫助。
第一:'如果([標題isEqualToString:@ 「YES」]){'是不必要的開銷。爲什麼不簡單地使用'if(buttonIndex == 1){'?其次,從哪裏檢索「發件人」?沒有參數alertView:clickedButtonAtIndex:'方法名爲'sender' ... – 2012-06-20 03:38:58
我真的不知道先生,因爲以前我只是使用刪除部分在 - (IBAction)buttonClicked:(id)sender'部分。但我想添加一個alertView,所以有一個確認。現在我有點搞砸了, – Bazinga
但它甚至編譯?我懷疑。 – 2012-06-20 03:45:39