如何在iOS中不使用委托来显示不同的警报?

时间:2013-12-04 05:38:54

标签: ios6

我已经创建了一个添加到按钮的图像视图,即图像视图被添加到图像视图中,该图像被添加到循环中并且按钮相同,图像居中并且启用了分页,因此现在,图像显示在带有按钮的序列中,这些图像在启用滚动时添加到scorllview中,通过选择每个图像,警报应显示为第一个图像,第二个图像等。我的问题是:但是如果不使用警报视图委托,警报应该以不同方式显示,如何通过选择每个图像来显示不同的警报?

int buttonXPostion = 0;
for(int i = 0; i < [_array count]; i++)
{
    buttonXPostion  = i*frameWidth;
    wallPaperButton = [[UIButton alloc] initWithFrame:CGRectMake(buttonXPostion,7,frameWidth, frameheight)];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,frameWidth,frameheight)];
    imageView.contentMode = UIViewContentModeCenter;
    imageView.image=[UIImage imageNamed:[_array objectAtIndex:i]];
    [wallPaperButton addTarget:self action:@selector(imageSelected:) forControlEvents:UIControlEventTouchUpInside];
    [wallPaperButton addSubview:imageView];
    [scrollImage addSubview:wallPaperButton];
}

2 个答案:

答案 0 :(得分:0)

试试这段代码可能会对你有帮助..

-(IBAction)imageSelected:(id)sender
{
    if(sender.tag==0)
    {
         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"First Image Button Pressed." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
         [alert show];
         alert.tag=sender.tag;
    }
    else if(sender.tag==1)
    {
          UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"Second Image Button Pressed." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
         [alert show];
         alert.tag=sender.tag;
    }

    ........   ///   implement if condition as per tag you require
}

答案 1 :(得分:0)

是的,我已经完成了!通过设置按钮的标签,

    wallPaperButton.tag=i;

-(IBAction)imageSelected:(id)sender
{
    UIButton *button = sender;
    switch (button.tag) {
        case 0:{
            NSString *message=@"FirstImage";
            alert=[[UIAlertView alloc]initWithTitle:@"" message:message delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
            break;}
        case 1:{
            NSString *message=@"SecondImage";
            alert=[[UIAlertView alloc]initWithTitle:@"" message:message delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
            break;}
        case 2:{
            NSString *message=@"ThirdImage";
            alert=[[UIAlertView alloc]initWithTitle:@"" message:message delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
            break;}
        case 3:{
            NSString *message=@"FourthImage";
            alert=[[UIAlertView alloc]initWithTitle:@"" message:message delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
            break;}
        case 4:{
            NSString *message=@"FifthImage";
            alert=[[UIAlertView alloc]initWithTitle:@"" message:message delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
            break;}
        case 5:{
            NSString *message=@"sixthImage";
            alert=[[UIAlertView alloc]initWithTitle:@"" message:message delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
            break;}
        default:
            break;
    }
  [alert show];
}