Search code examples
iosobjective-cuitableviewuiviewcontrolleruiactionsheet

Populating TableView upon clicking action sheet Objective-C Prime No, value entered from textfield


When I am importing the mutable array "primearray" from PRIME class and assigning it to nsmutablearray "array" declared in view controller it throws error.

Basically I am trying to populate the tableview with the contents of nsmutablearry.

The contents of this mutablearry declared in class depends upon the user pressing the button at index:0 in action sheet.

Can anyone help ??? Thanks in advance.

 IBOutlet UITextField *txt1;

    IBOutlet UITextField *txt2;

    NSMutableArray *arry;

    int startno;

    int endno;    
}

@property IBOutlet UITableView *tbl;

-(IBAction)ok:(id)sender;


@end

END OF VIEWCONTROLLER.H FILE

 ViewController.m


    #import "ViewController.h"

    #import "PRIME.h"

    #import "NON-PRIME.h"

    #import "ODD.h"

    #import "EVEN.h"

   @interface ViewController ()

   @end

    @implementation ViewController

    - (void)viewDidLoad
    {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a  nib.


   arry=[[NSMutableArray alloc]init];



}

      - (BOOL)textFieldShouldReturn:(UITextField *)textField
      {
          [textField resignFirstResponder]; 


          return YES;
      }

        -(IBAction)ok:(id)sender
  {
       // arry=[[NSMutableArray alloc]init];


          startno=[txt1.text intValue];

         endno=[txt2.text intValue];

       if(startno ==0 || endno ==0 || startno > endno)
       {

    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"CHECK   INPUT "message:@"input must not be nil,zero or alphabet & endno must be  greater than startno" delegate:self cancelButtonTitle:@"CANCEL"  otherButtonTitles:nil, nil];

    [alert show];

      }

 else

  {


  //if (startno !=0 || endno !=0 || !(startno > endno))
    {
      NSLog(@"1....start @%d, end @%d",startno,endno);

      UIActionSheet *action=[[UIActionSheet   alloc]initWithTitle:@"select your choice" delegate:self  cancelButtonTitle:@"cancel:" destructiveButtonTitle:nil otherButtonTitles:@"prime",@"non-prime",@"odd",@"even", nil];

       [action showInView:self.view];
    }
}

}

    - (void)actionSheet:(UIActionSheet *)actionSheet   clickedButtonAtIndex: (NSInteger)buttonIndex
    {

    if(buttonIndex==0)
{

    NSLog(@"2...start @%d, end @%d",startno,endno);

   PRIME *prime=[[PRIME alloc]init];

      [prime setnumbers1:startno sec:endno];

    [arry addObjectsFromArray:[prime primecal]];


    for(int i=0;i<arry.count;i++)
    {
        NSLog(@"count : @%d",i);

    }
     for(count1 in arry)
     {

        NSLog(@" arry %@ ",count1);

    }
 }


if(buttonIndex==1)
{
    NON_PRIME *nonprimeobj=[[NON_PRIME alloc]init];
}
if(buttonIndex==2)
{
      ODD *oddobj=[[ODD alloc]init];

}
if(buttonIndex==3)
{

    EVEN *evenobj=[[EVEN alloc]init];

}

}

    - (NSInteger)tableView:(UITableView *)tableView         numberOfRowsInSection:(NSInteger)section
    {
       return arry.count;
   }


      - (UITableViewCell *)tableView:(UITableView *)
        tableView      cellForRowAtIndexPath:(NSIndexPath *)indexPath
          {

            UITableViewCell *cell;


        if(cell==nil)
           {

            cell=[[UITableViewCell    alloc]initWithStyle:
            UITableViewCellStyleSubtitle reuseIdentifier:@"A"];

           }
       cell.textLabel.text=[arry objectAtIndex:indexPath.row];

       return cell;



  }

   - (void)didReceiveMemoryWarning
    {
    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

   }  

   @end

end of view controller.m file

 @interface PRIME : NSObject
    {

       int i,start,end;

       int remain,k,p;

       int count;

    //int count=0;   //error????

   }

    @property(strong,nonatomic) NSMutableArray *primearray;

   -  (void)setnumbers1:(int)start1 sec:(int)end1;

    -(NSMutableArray *)primecal;

END OF PRIME.H CLASS

     #import "PRIME.h"

      @implementation PRIME


     -(void)setnumbers1:(int)start1 sec:(int)end1
      {

       start=start1;

      end=end1;

  }



     -(NSMutableArray *)primecal
     {
        NSMutableArray *primearray=[[NSMutableArray alloc]init];

          for(i=start;i<=end;i++)
         {

           for(p=2;p<=1;p++)

        {
            remain=i%p;

            p++;
        }


        if(p==i)

        {
             [primearray addObject:[NSString stringWithFormat:@"%d",i]];

            count++;
        }
    }

    return  primearray;
   }

@end

END OF PRIME.M CLASS


Solution

  • The main problem you have is that you want to use a method from PRIME class but you are not initializing or using at all

    I think that instead of

    arry=[[NSMutableArray alloc]init];
    
    startno=[txt1.text intValue];
    
    endno=[txt2.text intValue];
    
    arry=[arry addObject: primearray]; // 
    

    you need something like this

    NSMutableArray * arry = [NSMutableArray array];
    startno=[txt1.text intValue];
    endno=[txt2.text intValue];
    
    PRIME * prime = [[PRIME alloc]init];
    [prime setnumbers1:startno sec:endno];
    [arry addObjectsFromArray:[prime primeCal]];
    

    This is using your actionSheet

    - (void)actionSheet:(UIActionSheet *)actionSheet   clickedButtonAtIndex: (NSInteger)buttonIndex
    {
    
        if(buttonIndex==0)
        {
            startno=[txt1.text intValue];
            endno=[txt2.text intValue];
    
            PRIME * prime = [[PRIME alloc]init];
            [prime setnumbers1:startno sec:endno];
            [arry addObjectsFromArray:[prime primeCal]];
            [self.tableView reloadData]
        }
    
        if(buttonIndex==1)
        {
            NON_PRIME *nonprimeobj=[[NON_PRIME alloc]init];
        }
        if(buttonIndex==2)
        {
              ODD *oddobj=[[ODD alloc]init];
    
        }
        if(buttonIndex==3)
        {
    
            EVEN *evenobj=[[EVEN alloc]init];
    
        }
    }
    

    I hope this helps you.