Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-07-2012, 06:55 AM   #1 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 97
AdamsApple is on a distinguished road
Question How to create a "Typical methods" class?

I am not sure what it is called, but I'd like to create a .m and .h with a few common functions so that i can call them from all of my viewcontrollers.
I have about 5 view controllers.

So here is what i did, which seems not to work.

I create a class and put the function in there:

in the CommonMethods.h

Code:
#import <Foundation/Foundation.h>

@interface CommonMethods : NSObject


-(UIImage*)processImage:(UIImage*)theImage;

@end
In the CommonMethods.m

Code:
#import "CommonMethods.h"

@implementation CommonMethods




-(UIImage*)processImage:(UIImage*)theImage {
	
// do things here (secret! :P)	
	
	//RETURN
	return theImage;
	
	
	
}


@end
So in my viewController, i add the import h part in the viewcontroller.m

#import "CommonMethods.h"

then to call, i just use

tmp = [self processImage:myImg];

But got a SIGABRT error.. obviously my vc does not recognize this function..

Please help what is the correct way to do this? Thank you.....
AdamsApple is offline   Reply With Quote
Old 02-07-2012, 07:02 AM   #2 (permalink)
Registered Member
 
Join Date: Nov 2009
Location: Helsinki
Posts: 304
fiftysixty is on a distinguished road
Default

Quote:
Originally Posted by AdamsApple View Post
I am not sure what it is called, but I'd like to create a .m and .h with a few common functions so that i can call them from all of my viewcontrollers.
I have about 5 view controllers.

So here is what i did, which seems not to work.

I create a class and put the function in there:

in the CommonMethods.h

Code:
#import <Foundation/Foundation.h>

@interface CommonMethods : NSObject


-(UIImage*)processImage:(UIImage*)theImage;

@end
In the CommonMethods.m

Code:
#import "CommonMethods.h"

@implementation CommonMethods




-(UIImage*)processImage:(UIImage*)theImage {
	
// do things here (secret! :P)	
	
	//RETURN
	return theImage;
	
	
	
}


@end
So in my viewController, i add the import h part in the viewcontroller.m

#import "CommonMethods.h"

then to call, i just use

tmp = [self processImage:myImg];

But got a SIGABRT error.. obviously my vc does not recognize this function..

Please help what is the correct way to do this? Thank you.....
There are a few ways to go about this. You could either create a class that has only so called class methods, or you could implement a singleton-pattern for your class. It seems you have a lot to learn, so I suggest you do some searching on objective-c class methods and the singleton design pattern before continuing. And obviously it would be a good idea to read some Apple documentation as well.

Get familiar with the basic concepts of object oriented programming, objective-c and some of the most basic design patterns (model-view-controller, singleton etc.) and then continue.
fiftysixty is online now   Reply With Quote
Old 02-07-2012, 07:32 AM   #3 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 72
bellissimo is on a distinguished road
Default

For general utility methods, I often just create a separate .h/.m file, but use standard C functions, rather that Objective-C.

Obviously though it depends on what you are doing. If you are creating some String manipulation functions for example, then it may well make more sense to create a Category on the NSString class instead, especially if you want them to act on a particular instance of an NSString.
__________________
My Mortgage Mentor
Debt Manager
bellissimo is offline   Reply With Quote
Old 02-07-2012, 09:18 AM   #4 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 97
AdamsApple is on a distinguished road
Default

Hm... If anyone else can show me an exmple of how to do this would be great. I know i have a lot to learn bcause i am new in xcode, that's why i ask the question here. If i know, i wouldn't ask this question.....

Any other pepeol that can help me i will be extremely thankful.
AdamsApple is offline   Reply With Quote
Old 02-07-2012, 09:54 AM   #5 (permalink)
Registered Member
 
Music Man's Avatar
 
Join Date: Oct 2009
Location: UK
Posts: 81
Music Man is on a distinguished road
Default

Quote:
Originally Posted by AdamsApple View Post

So in my viewController, i add the import h part in the viewcontroller.m

#import "CommonMethods.h"

then to call, i just use

tmp = [self processImage:myImg];

But got a SIGABRT error.. obviously my vc does not recognize this function..

Please help what is the correct way to do this? Thank you.....
Why are you calling [self processImage:myImg] ?

By calling self, you are referring to the viewController class, not the CommonMethods class.

The image method is in the CommonMethods class so you need to refer to that class:

Create an instance variable to your common methods class and use that.

Code:
#import <UIKit/UIKit.h>

@class CommonMethods;

@interface ViewController : UIViewController
{
    CommonMethods *commonMethods;
}



@end
Then import CommonMethods.h to your main viewController file - then implement this (in whatever method you want, I used viewDidLoad as an example).

Code:
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIImage *img = [UIImage imageNamed:@"test.jpg"];
    [commonMethods processImage:img];
}
or modify it to what you need.

Also what is tmp referring to in your example?
Music Man is offline   Reply With Quote
Old 02-07-2012, 07:46 PM   #6 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 97
AdamsApple is on a distinguished road
Default

Music Man, THANK YOU FOR YOUR ANSWER!
You are THE (music) MAN!


tmp is a UIImage.

Ok new problem, if you'd like to continue helping....

the method is recognized but it is not returning the UIImage
result. As I checked, it only returns 0x0. This is how I did it.

Code:
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIImage *img = [UIImage imageNamed:@"mytest.png"];
    
    myImage.image = [commonMethods processImage:img];
}
myImage.image contains 0x0 bytes after it. The processImage
is AOK, because when it is in the same viewcontroller, it works
fine.

Please help.... anyone.. tq....

Last edited by AdamsApple; 02-07-2012 at 07:57 PM.
AdamsApple is offline   Reply With Quote
Old 02-07-2012, 08:17 PM   #7 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 97
AdamsApple is on a distinguished road
Default

Ok I got it to work.

Here's the code:


Use a + sign for method instead of -
.h
Code:
#import <Foundation/Foundation.h>

@interface CommonMethods : NSObject


+(UIImage*)processImage:(UIImage*)theImage;

@end
.m
Code:
#import "CommonMethods.h"

@implementation CommonMethods




+(UIImage*)processImage:(UIImage*)theImage {
	
// do things here (secret! :P)	
	
	//RETURN
	return theImage;
	
	
	
}


@end
TO USE:

import the .h in your vc .m,

then directly call it

Code:
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIImage *img = [UIImage imageNamed:@"mytest.png"];
    
    myImage.image = [CommonMethods processImage:img];
}
there is no need to declare it as a class in the header of your vc as well.
I like this way, it is so simple.

Anyway, thanks again to Music Man for making it clear to me regarding
the "self" usage and what it actually means.

Last edited by AdamsApple; 02-07-2012 at 08:32 PM.
AdamsApple is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 405
13 members and 392 guests
7twenty7, chiataytuday, cristofercolmbos, fiftysixty, gmarro, iOS.Lover, kilobytedump, Matrix23, raymng, ryantcb, stanny, UMAD, xerohuang
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,669
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, dedeys78
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:18 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0