There is a general class Animal and descendants (three more classes) that contain the name, age, color of the animal. There is also a forward button when you click on which information should change. Does anyone know how this can be done. Animal class:

#import <Foundation/Foundation.h> @interface OMAnimal : NSObject @property (copy, nonatomic) NSString *name; @property (copy, nonatomic) NSString *color; @property (assign, nonatomic) NSInteger age; @end 

Display:

 #import "OMAnimalViewController.h" #import "OMCat.h" #import "OMDog.h" #import "OMMonkey.h" @interface OMAnimalViewController () @property (assign, nonatomic) NSInteger index; @property (strong, nonatomic) NSArray *arrayAnimal; @end @implementation OMAnimalViewController - (void)viewDidLoad { [super viewDidLoad]; OMCat *animalCat = [[OMCat alloc] init]; animalCat.name = @"Kate"; animalCat.age = 6; animalCat.color = @"White"; OMDog *animalDog = [[OMDog alloc] init]; animalDog.name = @"Bob"; animalDog.age = 3; animalDog.color = @"Balck"; OMMonkey *animalMonkey = [[OMMonkey alloc] init]; animalMonkey.name = @"Charly"; animalMonkey.age = 7; animalMonkey.color = @"Brown"; _arrayAnimal = @[animalCat, animalDog, animalMonkey]; [self updateAnimal]; } - (void)updateAnimal { OMCat *cat = _arrayAnimal[0]; self.nameLabel.text = cat.name; self.ageLabel.text = [NSString stringWithFormat:@"%d", cat.age]; self.colorLabel.text = cat.color; } - (IBAction)actionNext:(UIButton *)sender { } @end 
  • as it can be more specific. Where and in what format is the information stored? How to find out what is on the screen now? in what format should the new information be displayed? - Max Mikheyenko

1 answer 1

did not check, but it seems to work.

 - (void)updateAnimal { OMAnimal *current = self.arrayAnimal[self.index]; self.nameLabel.text = current.name; self.ageLabel.text = [NSString stringWithFormat:@"%d", current.age]; self.colorLabel.text = current.color; } - (IBAction)actionNext:(UIButton *)sender { self.index = (self.index+1) % self.arrayAnimal.count; [self updateAnimal]; } 
  • Thank you very much.) Everything works fine. If it is not difficult to explain the following expression: self.index = (self.index + 1)% self.arrayAnimal.count. - Orest Mykha
  • This is such a smart way to add 1 to the index without overflow. Each time after adding 1 to the index, it is divided by the number of elements in the array and the remainder is assigned. That is, if for example you have 3 elements in the array, if after adding 1 to the index it becomes 1 or 2, then everything is fine, and if it becomes 3 then the remainder of the division will be 0. If it’s difficult to read the variant, you can write self.index++; if(self.index == self.arrayAnimal.count) self.index = 0; instead self.index++; if(self.index == self.arrayAnimal.count) self.index = 0; self.index++; if(self.index == self.arrayAnimal.count) self.index = 0; - Max Mikheyenko
  • and do not forget to accept the answer if everything works - Max Mikheyenko
  • Answer Adopted ..) - Orest Mykha
  • Something does not show me what is accepted - Max Mikheyenko