首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >alertView没有响应

alertView没有响应
EN

Stack Overflow用户
提问于 2012-06-20 11:30:47
回答 2查看 152关注 0票数 0

所以我有一个UIScrollviewUIImageView设置了一个按钮,我希望能够每当一个图像被点击时,alertView将弹出如果选择是,那么该图像将在NSDocumentDirectory中被删除。我设法使alertView出现,但图像没有删除,因为我认为发送了错误的sender或button.tag。下面是我的代码:

//我的scrollView

代码语言:javascript
复制
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++;
    }

// Button

代码语言:javascript
复制
- (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

代码语言:javascript
复制
- (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");
    }

谢谢你的帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-06-20 12:07:31

在clickedButtonAtIndex函数中,您不能从单击的按钮获取任何引用,因为它是来自UIAlertView的回调。在这个函数内部你能得到的都是与点击的UIAlertView本身相关的。

如果你想删除选中的图片,你可以先在buttonClicked函数中存储被点击按钮的指针或标签。

代码语言:javascript
复制
- (IBAction)buttonClicked:(id)sender {
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSInteger slotBG = [prefs integerForKey:@"integerKey"];

    if(slotBG == 1){
        //  Get the pointer or tag of the clicked button
        _clickedButton = (UIButton *)sender;
        UIAlertView *deleteMessage = [[UIAlertView alloc] initWithTitle:@""
                                                              message:@"DELETE?"
                                                             delegate:self
                                                    cancelButtonTitle:@"NO"
                                                    otherButtonTitles:@"YES", nil];
        [deleteMessage show];        
    }  
}

然后,您可以在clickedButtonAtIndex函数中使用此指针/标记。

代码语言:javascript
复制
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

    if([title isEqualToString:@"YES"]){
        UIButton *button = _clickedButton;

        [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");
    }

    //  Remember to set it to nil when you finish
    _clickedButton = nil;
}
票数 1
EN

Stack Overflow用户

发布于 2012-06-20 13:08:35

代码语言:javascript
复制
- (IBAction)buttonClicked:(id)sender {
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSInteger slotBG = [prefs integerForKey:@"integerKey"];

    if(slotBG == 1){
        //  Get the pointer or tag of the clicked button
        _clickedButton = (UIButton *)sender;
        UIAlertView *deleteMessage = [[UIAlertView alloc] initWithTitle:@""
                                                              message:@"DELETE?"
                                                             delegate:self
                                                    cancelButtonTitle:@"NO"
                                                    otherButtonTitles:@"YES", nil];
deleteMessage.tag=1;

        [deleteMessage show];        
    }  
}

/

代码语言:javascript
复制
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

   if (alertView.tag==1) {
    if (buttonIndex==1) {
        UIButton *button = _clickedButton;

        [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");
    }
}
    //  Remember to set it to nil when you finish
    _clickedButton = nil;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11112432

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档