Alertview错误

时间:2011-10-04 07:17:54

标签: iphone

    UIAlertView *theAlert = [[[UIAlertView alloc] initWithTitle:@"Share"
                                                        message:message
                                                       delegate:delegate
                                              cancelButtonTitle:@"Cancel"
                                              otherButtonTitles:@"Notes", @"Bookmark", @"Facebook",@"Twitter",@"Email", nil] autorelease];
    [theAlert show];




    //Set View of Button Cancel at index 1 in AlertView.
    //[[[theAlert subviews] objectAtIndex:1] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"button_Popup.png"]]];
    //[[[theAlert subviews] objectAtIndex:1] setBackgroundColor:[UIColor redColor]];
    //[[[theAlert subviews] objectAtIndex:1] setShadowColor:[UIColor clearColor]];

    //Set View of Button at index 2 in AlertView.
    [[[theAlert subviews] objectAtIndex:1] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"button_Popup.png"]]];
    //[[[theAlert subviews] objectAtIndex:2] setBackgroundColor:[UIColor blueColor]];
    [[[theAlert subviews] objectAtIndex:1] setShadowColor:[UIColor clearColor]];
    //[[[theAlert subviews] objectAtIndex:2] setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

    //Set View of Button at index 3 in AlertView.
    [[[theAlert subviews] objectAtIndex:2] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"button_Popup.png"]]];
    //[[[theAlert subviews] objectAtIndex:3] setBackgroundColor:[UIColor orangeColor]];
    [[[theAlert subviews] objectAtIndex:2] setShadowColor:[UIColor clearColor]];
    //[[[theAlert subviews] objectAtIndex:3] setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

    //Set View of Button at index 4 in AlertView.
    [[[theAlert subviews] objectAtIndex:3] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"button_Popup.png"]]];
    //[[[theAlert subviews] objectAtIndex:4] setBackgroundColor:[UIColor greenColor]];
    [[[theAlert subviews] objectAtIndex:3] setShadowColor:[UIColor clearColor]];
    //[[[theAlert subviews] objectAtIndex:4] setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

    //Set View of Button at index 5 in AlertView.
    [[[theAlert subviews] objectAtIndex:4] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"button_Popup.png"]]];
    //[[[theAlert subviews] objectAtIndex:5] setBackgroundColor:[UIColor greenColor]];
    [[[theAlert subviews] objectAtIndex:4] setShadowColor:[UIColor clearColor]];
    //[[[theAlert subviews] objectAtIndex:5] setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

    //Set View of Button at index 6 in AlertView.
    [[[theAlert subviews] objectAtIndex:5] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"button_Popup.png"]]];
    //[[[theAlert subviews] objectAtIndex:6] setBackgroundColor:[UIColor greenColor]];
    [[[theAlert subviews] objectAtIndex:5] setShadowColor:[UIColor clearColor]];
    //[[[theAlert subviews] objectAtIndex:6] setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

    UILabel *theTitle = [theAlert valueForKey:@"_titleLabel"];
    [theTitle setFont:[UIFont fontWithName:@"Helvetica" size:16]];
    [theTitle setTextAlignment:UITextAlignmentLeft];

    UIImage *theImage = [UIImage imageNamed:@"Popup.png"];
    CGSize theSize = [theAlert frame].size;

    UIGraphicsBeginImageContext(theSize);
    [theImage drawInRect:CGRectMake(0, 0, theSize.width, theSize.height)];
    theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    [[theAlert layer] setContents:[theImage CGImage]];              
}

这是我用来显示alertview的代码,但是用于显示alertview的noproblem,但我无法在此alertview中设置按钮的操作,因为deligate设置为nil.my问题是当我设置deligate时自我错误表明自己未申报。如何解决这个问题? 关于nipin

2 个答案:

答案 0 :(得分:0)

为什么不直接将委托传递给方法:

void DisplayAlert(NSString* title, NSString* message, id delegate) {

    UIAlertView *theAlert = [[[UIAlertView alloc] initWithTitle:@"Share"
                                                        message:message
                                                       delegate:delegate
                                              cancelButtonTitle:@"Cancel"
                                              otherButtonTitles:@"Notes", @"Bookmark", @"Facebook",@"Twitter",@"Email", nil] autorelease];
    [theAlert show];
}

答案 1 :(得分:0)

您显示警报的代码看起来很好。检查您是否错误地将关键字self用作某个变量名称。