objective-c – How to pass class methods to another class (iOS 7 on storyboard)

Question:

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 a method:

- (IBAction)display:(id)sender
{

}

Questions:

  1. How to call?
  2. What is needed for this?

Answer:

@ leonid3452 , Good time of the day! I will try to explain it with my example. Let's say there are two classes:

"ViewController : UIViewController" и "DetailController : UIViewController"

And we need to pass the method:

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

And so, let's move on to the implementation: 1 what you need to do make sure that the method is declared in "ViewController.h" will look something like this:

//ViewController.h
@interface ViewController : UIViewController 
{

}
- (void) displayLog;
@end

Let's go to ViewController.m and 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 DetailController, say by clicking on the button, we 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 I have explained it clearly. If you do not understand anything, ask questions.

Scroll to Top