Accessing Method from other Classes Objective-C

前端 未结 8 1494
挽巷
挽巷 2020-11-29 05:57

Looked for an answer for this question, but I haven\'t found a suitable one yet. I\'m hoping you guys (and gals) can help me out! (This is for an iPhone app)

Alrig

相关标签:
8条回答
  • 2020-11-29 06:27

    Another method that you can use

    @interface ServerManager : NSObject
    
    +(ServerManager *)getInstance;
    
    @implementation ServerManager
    
    +(ServerManager *)getInstance
    {
    static ServerManager *objServerManager = nil;
    
    if(objServerManager==NULL){
    objServerManager=[[self alloc] init];
    }
    // Return the servermanager object.
    return objServerManager;
    }
    

    Call Whether you want to use

    ServerManager *SMObject =   [ServerManager getInstance];
    

    Don't forget to import servermanager.h file.

    0 讨论(0)
  • 2020-11-29 06:29

    What you want to do is to make the two controllers share a common superclass:

    UIViewController : MyAwesomeViewController : ViewController1
                                               : ViewController2
    

    commonMethod: would then reside in MyAwesomeViewController. Also, don't start method names with capital letters. :)

    To elaborate:

    +@interface MyAwesomeController : UIViewController {
    
    -@interface ViewController1 : UIViewController { // and ditto for ViewController2
    +@interface ViewController1 : MyAwesomeController {
    
    0 讨论(0)
  • 2020-11-29 06:40

    As an iPhone neophyte with a Java background and little C, I had a similar problem wishing to refer to a method in both the RootController and a ViewController. It seemed to me that the proper place for the method was the AppDelegate class, an instance of which one obtains in other classes by:

    MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
    

    then if the method is "doSomething" one accesses it by:

    [delegate doSomething];
    

    But perhaps this is too obvious or not what was required.

    0 讨论(0)
  • 2020-11-29 06:44

    There are some answers here telling you to create a common "parent" class. However I think that you can do a lot better. Create a category for UIViewController instead. You don't know all of the internals of what is going on with UIViewController so I don't think it is worth creating your own View Controller hierarchy off of. In fact it could be dangerous. I ran into a number of problems when I tried to create a "base" UITableViewController and then create classes that inherit from that. I avoided these problems by using categories instead.

    Your #1 priority shouldn't be inheriting things for no good reason, it should be getting an app into the app store that people will want to download.

    0 讨论(0)
  • 2020-11-29 06:45

    Pass in a reference to your commonClass when you alloc and init your views...

    CommonClass *cc = [[CommonClass alloc] init];
    
    ViewController1 *vc1 = [[ViewController1 alloc] ... initWith:cc];
    ViewController2 *vc2 = [[ViewController2 alloc] ... initWith:cc];
    

    but making a classic c include might suffice.

    0 讨论(0)
  • 2020-11-29 06:47

    Option 1:

    @implementation commonClass
    + (void)CommonMethod:(id)sender  /* note the + sign */
    {
    //So some awesome generic stuff...
        }
    @end
    
    @implementation ViewController2
    
    - (void)do_something... {
        [commonClass CommonMethod];
    }
    
    
    @end
    

    Option 2:

    @implementation commonClass
    - (void)CommonMethod:(id)sender
    {
    //So some awesome generic stuff...
        }
    @end
    
    @implementation ViewController2
    
    - (void)do_something... {
        commonClass *c=[[commonClass alloc] init];
        [c CommonMethod];
        [c release];
    }
    
    @end
    

    Option 3: use inheritance (see Mr. Totland's description in this thread)

    @implementation commonClass
    - (void)CommonMethod:(id)sender
    {
    //So some awesome generic stuff...
        }
    @end
    
    /* in your .h file */
    @interface ViewController2: commonClass
    
    @end
    

    naturally you always need to #import commonClass.h in your view controllers..

    0 讨论(0)
提交回复
热议问题