Aashay
Aashay

Reputation: 383

Reusing the same methods(functionality) in two different classes in xcode

I was wondering how I can reuse some code in multiple files in xcode. To elaborate, I have two classes, InviteViewController and FindFriendsViewController. InviteViewController is of type XViewController and FindFriendsViewController is of type YViewController. In both InviteViewController and FindFriendsViewController, I have a button that does the same thing, i.e, shows an action sheet with options like 'Send SMS', 'Send Email', etc. As I am not an expert in iOS development, I wanted to know what some good techniques are to reuse code in such situations? Is there a way I can write a common file which creates and performs action button click events in both these files? Should I create a NSObject file and import the NSObject file in both the classes, InviteViewController and FindFriendsViewController? Is there a more sophisticated way of doing so? Is the method I described the correct implementation in this situation?

Any help and advise in doing so would be great! Thank you in advance for your replies!

Upvotes: 1

Views: 416

Answers (1)

Konstantin  Kryzhanovsky
Konstantin Kryzhanovsky

Reputation: 1001

so many capabilities to do this.

u can create a static class, u can create a singleton , u can create protocol.

simple example of one way:

h.file

@interface ClassName : NSObject

+(void)sendEmail;
+(void)sendSMS

m.file

@implementation ClassName
+(void)sendEmail {
   // some code
}
+(void)sendSMS {
    // some code

}

in your cntrls add handle button actions and call methods

// some code
[ClassName sendSMS];
// some code

Upvotes: 4

Related Questions