简体   繁体   中英

iOS call method of another class

I found similar questions on this site but not one that addresses the problem in a clear and basic way.

I have my ReadViewController.h and ReadViewController.m files along with my ChooseViewController.h and ChooseViewController.m files.

They both need to access the getProperties method which is currently in the ReadViewController.m file.

- (void) getProperties {
    NSLog(@"Start getProperties");
//SOME CODE
    NSLog(@"End getProperties");
}

Now ideally this will be in a third file called GeneralModel.m

Please give me a basic example of what code needs to be in the controller files for them to be able to call this method.

If this method Going to be used in many places in Application then in this case you should treat it as Global method and try to put this method in separate class may be type of NSObject Class.

  @interface Utility :NSobject

  - (void) getProperties
  @end

 @implementation Utility

 - (void) getProperties {
    NSLog(@"Start getProperties");
   //SOME CODE
    NSLog(@"End getProperties");
 }
 @end

Here Whenever you need that methods you just need to create the Object of Utility Class can access it easily wherever it needed.like

in ReadViewController just make object and access in this way

  Utility * obje = [Utility  alloc]init];

  [obje getProperties  ];

And One thing if you just talking about the App architecture ,Suppose you following the MVC in that Case you should keep your model(NSObject Type)Class for Making some DB call, Request call to server. Keep View Classes code Like UIView separately and put the Code inside Controller class only which needed to control the Logic of App.

Here is the Link which explain The MVC Architecture.

I hope it clears to you.

The solution I've implemented looks like this. I'll accept iOS-Developer's answer though since it set me on the right track.

//*********************
//ReadViewController.h
#import <UIKit/UIKit.h>
#import "GeneralModel.h"

@interface ReadViewController : UIViewController {
    GeneralModel *generalModel;
}

@end
//*********************


//*********************
//ReadViewController.m
#import "ReadViewController.h"

@interface ReadViewController ()

@end

@implementation ReadViewController

NSArray *allProperties;

- (void) getProperties {
    generalModel = [[GeneralModel alloc] init];
    allProperties = [generalModel getProperties];
    NSLog(@"ALLPROPERTIES: %@", allProperties);
    [generalModel release];
}
//**********************


//**********************
//GeneralModel.h
#import <Foundation/Foundation.h>
#import "sqlite3.h"

@interface GeneralModel : NSObject {

}
-(NSArray *) getProperties;
@end
//**********************


//**********************
//GeneralModel.m
#import "GeneralModel.h"

@implementation GeneralModel

- (NSArray *) getProperties {
    NSLog(@"Start getProperties");
    NSArray *someProperties;
//Some nice code goes here for getting a lot of nice properties from somewhere else.
    return someProperties
    NSLog(@"End getProperties");
}
//***********************

If this method Going to be used in many places in Application then in this case you should treat it as Global method and try to put this method in separate class may be type of NSObject Class.

@interface Utility :NSobject
- (void) getProperties
@end

@implementation Utility

- (void) getProperties {
    NSLog(@"Start getProperties");
    //SOME CODE
    NSLog(@"End getProperties");
}
@end

Here Whenever you need that methods you just need to create the Object of Utility Class can access it easily wherever it needed.like

in ReadViewController just make object and access in this way

Utility * obje = [Utility  alloc]init];
[obje getProperties  ];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM