The situation is this, there is a class Class : UIViewController it has a method, for example

 - (void)displayRefresh { } 

How to call this method in another class Class2 : UIViewController in the method:

 - (IBAction)display:(id)sender { } 

Questions:

  1. How to call?
  2. What is needed for this?
  • In the second class, a reference to the created first class object is needed. - KoVadim
  • How are your controllers interconnected? Three methods of communication between controllers are usually used: 1. Modal presentation - one controller presents another (modal presentation). 2. Parent-child - so-called relationship Controller Containment. 3. You use the navigation controller and push the transition from one to another (by hand or through performSegueWithIdentifier). Most likely your way will be to one of this list. I could describe the methods of communication for all these cases, but I still want to first find out about yours. - Stanislav Pankevich
  • @Stanislaw Pankevich, I use the transition to the cell from the Table View, and Class2 contains detailed displays of the data that I chose from the table - I hope it is clear) - leonid
  • Can you put a zip test project somewhere on the file sharing service and add a link to it to the question? - Stanislav Pankevich
  • @Stanislaw Pankevich, Now there is no such possibility, since it is not at home. Actually, I didn’t begin to study iOS development a long time ago, so I don’t have to write something wrong :) It shouldn’t be hard, you just need to pass the method to another class, but how do you transfer methods from one class to another? - leonid

1 answer 1

@ leonid3452 , Good time of day! I will try to explain with my own example. Suppose there are two classes:

 "ViewController : UIViewController" и "DetailController : UIViewController" 

And we need to pass a method:

 - (void)displayLog с "ViewController" в "DetailController" 

And so, let's proceed to the implementation: 1 What needs to be done is to make sure that the method is declared in "ViewController.h" approximately looks like this:

 //ViewController.h @interface ViewController : UIViewController { } - (void) displayLog; @end 

Go to ViewController.m. Implement the method:

 //ViewController.m @interface ViewController () @end @implementation ViewController - (void) displayLog { NSLog(@"Hello"); } @end 

Next, you need to call this method (- (void) displayLog;) in the DetailController, say by pressing a button, implement:

 //DetailController.h @interface DetailController : UIViewController { } - (IBAction)button:(id)sender; @end 

Go to .m:

 //DetailController.m @interface DetailPassword () @end @implementation DetailPassword - (IBAction)button:(id)sender { ViewController *viewCon = [[ViewController alloc] init]; [viewCon displayLog]; } @end 

I hope that explained clearly. If you do not understand, ask questions.

  • @ Kovalsky, thank you so much, really great !!! it worked, thanks, thank you !!! So far there are no questions, everything seems to be as clear :)) - leonid
  • one
    @ leonid3452, Absolutely not at all. - Zarochintsev
  • It is possible and so, but for these purposes there is a delegation. - zhenyab
  • I was going to talk about delegation and other recommended cases, but if this was enough for the author, then it was enough)) - Stanislav Pankevich
  • one
    @Stanislaw Pankevich, Tell me, I also began to study not long ago, everything is not yet clear, and I will be very grateful to you, or if you can send a link to the article :) - Zarochintsev