-1

Having trouble with the following bit of code: I have this LeavemanagementViewController.m

i am extracting and loading datas from xml from server. total number of elements is depends upon the array.

-(void)RejectActionPressed:(id)sender
{
if ([approve1role.text isEqualToString:datarole.text])
{
    UIButton *buttn2=(UIButton*)sender;
    NSLog(@"bttn.tag==%i",buttn2.tag);

    StudentClass *student = (StudentClass *) [arrayitems objectAtIndex:buttn2.tag];
    NSLog(@"student name= %@ \n leavecode %@ \n joining dat=%@",student.employeeName,student.leavecode,student.joiningdate);

    UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Alert" message:@" " delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
    alert1.tag = buttn2.tag;
    alert1.tag = kAlertViewTwo; CGRect frame = CGRectMake(14, 45, 255, 23);
    remarkstext = [[UITextField alloc] initWithFrame:frame];
    remarkstext.placeholder = @"Name";
    remarkstext.backgroundColor = [UIColor whiteColor];
    remarkstext.autocorrectionType = UITextAutocorrectionTypeDefault;
    remarkstext.keyboardType = UIKeyboardTypeAlphabet;
    remarkstext.returnKeyType = UIReturnKeyDone;
    remarkstext.clearButtonMode = UITextFieldViewModeWhileEditing; 
    [alert1 addSubview:remarkstext];
    [alert1 show];
 }
 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
     {
 if(alertView.tag == kAlertViewOne)
{
    if (buttonIndex==1)
       {
         NSLog(@"%@",remarkstext.text);
         remarksoutput.text = remarkstext.text;
         NSLog(@" The Alertview%@",remarksoutput.text);
           [self callWebservice:alertView.tag];
       }
     else if (buttonIndex==0)
      {
       NSLog(@"cancel");
      }
  }
if(alertView.tag == kAlertViewTwo)
{
    if (buttonIndex==1)
    {
        NSLog(@"%@",remarkstext.text);

        remarksoutput.text = remarkstext.text;
         NSLog(@" The Alertview%@",remarksoutput.text);
        [self callRejectWebservice:alertView.tag];
    }
    else if (buttonIndex==0)
    {
        NSLog(@"cancel");
   }
}
   }

  -(void)callRejectWebservice:(int)tag
    {
    if ([approve1role.text isEqualToString:datarole.text])
{
    StudentClass *student = (StudentClass *) [arrayitems objectAtIndex:tag];  // Here is my error *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]'           
    NSLog(@"student name= %@ \n leavecode %@ \n joining dat=%@",student.employeeName,student.leavecode,student.joiningdate);
}
if ([approve2role.text isEqualToString:datarole.text])
{

    StudentClass *student = (StudentClass *) [arrayitems objectAtIndex:tag];
    NSLog(@"student name= %@ \n leavecode %@ \n joining dat=%@",student.employeeName,student.leavecode,student.joiningdate);

}
if ([approve3role.text isEqualToString:datarole.text])
{
    StudentClass *student = (StudentClass *) [arrayitems objectAtIndex:tag];
    NSLog(@"student name= %@ \n leavecode %@ \n joining dat=%@",student.employeeName,student.leavecode,student.joiningdate);

}
 }

Please Help me out from this.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user 1035414
  • 77
  • 11

2 Answers2

0

Try this

StudentClass *student = (StudentClass *) [arrayitems objectAtIndex:tag-1];
  • if i added the above code, the 0 indexpath show value of indexpath 1 value. – user 1035414 Apr 07 '14 at 10:53
  • would you like to tell me what is value of Button2.tag –  Apr 07 '14 at 11:02
  • i added the button in tablecell //Create the button and add it to the cell - Reject button UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button2.tag = indexPath.row; [button2 addTarget:self action:@selector(RejectActionPressed:) forControlEvents:UIControlEventTouchDown]; button2.frame = CGRectMake(720.0f, 85.0f, 100.0f, 25.0f); [cell addSubview:button2]; – user 1035414 Apr 07 '14 at 11:09
  • I think the mistake may be in **alert1.tag = kAlertViewTwo;**, you are going to replace alert1.tag. First you set as **alert1.tag = button2.tag** –  Apr 07 '14 at 11:14
  • No, i declared the seperate buttn alloc, but i tried as per ur way..am getting same error. – user 1035414 Apr 07 '14 at 11:20
  • Before this statement **if ([approve1role.text isEqualToString:datarole.text])** find tag value **NSLog(@"%d",tag);** and use breakpoint to test. –  Apr 07 '14 at 11:26
  • The tag value shows 2, but still i dont know wr am doing mistake? – user 1035414 Apr 07 '14 at 11:30
  • **[self callRejectWebservice:alertView.tag-1];** Try this –  Apr 07 '14 at 11:35
  • Am getting the output value : bttn.tag==0 student name= Vijayakumar Manickam leavecode LP/00000449 joining dat=06 May 2011 The Alertview(null) The Value of Tag= 1 The Array Count is: 2 Array =( "", "" ) student name= Vijay Kolanchinathan leavecode LP/00000453 joining dat=30 Apr 2013 – user 1035414 Apr 07 '14 at 11:48
0

It look like a generic array out of bounds issue.

Your are having an array of two elements and is trying to refer to third element, which is not present in statment below

StudentClass *student = (StudentClass *) [arrayitems objectAtIndex:buttn2.tag];

Just print the array, you will have a better understanding of error

NSLog(@"Array =%@",arrayitems); //to debug
Dileep
  • 2,399
  • 4
  • 26
  • 39
  • the array count is 2 am getting with array value., I need the value same indexpath -(void)callRejectWebservice:(int)tag in this condition – user 1035414 Apr 07 '14 at 10:54
  • Array count 2 means " Array is having two element array[0], array[1]". You are refering to array[2], which is not present. So its giving error. – Dileep Apr 07 '14 at 10:56
  • Can u point out in the above code in your comments. so let me know where am doing mistake – user 1035414 Apr 07 '14 at 10:57
  • StudentClass *student = (StudentClass *) [arrayitems objectAtIndex:buttn2.tag-1]. Try this!! – Dileep Apr 07 '14 at 11:00
  • Can u tell me wr i need to add bcz i added in -(void)RejectActionPressed:(id)sender method, am getting the same error – user 1035414 Apr 07 '14 at 11:05
  • Iam not sure about your logic, try ADDING IT EVERYWHERE. If not working check where you are filling the Array, if any element is not added. – Dileep Apr 07 '14 at 11:09
  • alert1.tag = buttn2.tag; alert1.tag = kAlertViewTwo; Why you are doing this??. You have set a element a value and in next line you are changing value. It looks strange – Dileep Apr 07 '14 at 11:24