Good morning. I have 2 classes. One is inherited from UITableViewCell, the second from UIViewController (c).

In the first class there is a method that changes the image of the button when pressed (the button itself is in a cell).

-(void)method:(int)sender { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; UIImage *checkSel = [UIImage imageNamed:@"checked.png"]; if (sender == 0) { [self.checkButton setBackgroundImage:checkSel forState:UIControlStateNormal]; [defaults setBool:YES forKey:@"checkReq0"]; checkReq0 = YES; self.checkLabel.textColor = [UIColor greenColor]; NSLog(@"CheckOn1 OK"); } 

In the second class, I try to call this method, which should change the image on the same button when I click on another button (it is outside the cell)

 -(IBAction)secondButton:(id)sender { firstClass *object = [[firstClass alloc] init]; [object method]; } 

The method itself works as a whole, tested using NSLog, but does not perform the task in any way. In the headers, too, everything seems to be spelled out as it should. I hope for your help, thanks

  • You create a new instance and call its method. You need to call for an existing one. Judging by the description, the approach can be changed. I correctly understood that you want to change the image of the button in the cell, both when you press the button inside the cell and the button from the outside? - VAndrJ
  • @VAndrJ correctly understood. My method is dynamic, how can I call it without creating an instance of the class? - Bandicoot
  • @Bandicoot you need to take the link to the cell from the table via cellForRowAtIndexPath and call your method in this instance - Andrey Iskamov
  • @ andrey-iskamov tried to do as you said, but was not able to achieve the desired. Apparently, I did not understand something - Bandicoot

2 answers 2

Your metod method takes an int parameter, but when you call this method, you do not pass anything there.

I think the code should look like this:

 - (IBAction)secondButton:(UIButton *)sender { firstClass *object = [[firstClass alloc] init]; [object method:sender.tag]; } 
  • And how should you? Show by example. - 0xdb
  • It is very strange that you answered my comment! I would remake the secondButton like this: - (IBAction) secondButton: (UIButton *) sender {firstClass * object = [[firstClass alloc] init]; [object method: sender.tag]; } - Nikita Milko
  • @ 0xdb I understand that you put the -1 response. Can you argue? - Nikita Milko
  • No, not me. I can not argue. - 0xdb

In this way, firstClass *object = [[firstClass alloc] init]; you create a new cell instance that is not in the table, respectively, a method call on this instance does not lead to the desired result.

To call a method on the desired cell, it must be retrieved from the table (tableView). To do this, create an indexPath (the 'address' of the cell in the table), where row is the cell number in the section, section - the section number in the tableView:

 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 

Next, we refer to the table to get the cell instance we need:

 firstClass *cell = [self.tableView cellForRowAtIndexPath:indexPath]; 

Now we can call the desired method:

 [cell method];