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 > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-04-2011, 11:31 AM   #1 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 8
Newlog is on a distinguished road
Default How to invoke a method from another class

Hello people, I'm new here.

I have a very simple question, but I've already try a lot of things and read the objective-c basic documentation and I can not solve it.

The thing is that my Iphone application crashes when I try to execute a method from one class.
My code is the next:

InterfaceFunctions.h
Code:
#import <UIKit/UIKit.h>

@interface InterfaceFunctions : NSObject {

}

+ (void) createNormalButton: (UIButton *) bMyButton 
				   atPosX: (double) X 
				   atPosY: (double) Y 
				withSizeY: (double) HEIGHT 
				withSizeX: (double) WIDTH 
				withColor: (UIColor *) cMyColor 
				  withTag:(int) iTag 
				withTitle: (NSString *) sTitle 
				   inView: (UIView *) Vista ;

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


@implementation InterfaceFunctions

/*
 * Creates a Round Rect Button that is inserted in the view
 */
+ (void)createNormalButton:	(UIButton *) bMyButton 
				   atPosX: (double) X 
				   atPosY: (double) Y 
				withSizeY: (double) HEIGHT 
				withSizeX: (double) WIDTH 
				withColor: (UIColor *) cMyColor 
				  withTag:(int) iTag 
				withTitle: (NSString *) sTitle
				inView: (UIView *) Vista {
	NSLog(@"Executing createNormalButton...");
	bMyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];					//The button is allocated
	[bMyButton setFrame:CGRectMake(X, Y, WIDTH, HEIGHT)];							//The type and size of the button is setted
	[bMyButton setTitle: sTitle forState:UIControlStateNormal];						//The button's title is setted 
	[bMyButton setTitleShadowColor:cMyColor forState:UIControlStateNormal];			//The button's title shadow color is setted for its normal state
	[bMyButton setBackgroundColor: cMyColor];										//The button's background color is setted
	[bMyButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];	//The button's title color is setted for its normal state
	[bMyButton setTag: iTag];														//The button's tag is setted in order to select the proper the action
	[Vista addSubview:bMyButton];												//The button is added to the view	
}

And I call the createNormalButton 'function' like this:
Code:
#import "Main.h"
#import "InterfaceFunctions.h"


@implementation Main

(...)

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	self.view = [[UIView alloc] initWithFrame: CGRectMake(0.0, 0.0, SCREEN_WIDTH, SCREEN_HEIGTH)];		//The visual view is created. In this view all the objects will be added
	[self.view setBackgroundColor:[UIColor blackColor]];												//Background color is setted for the main view
	
	NSLog(@"Main viewDidLoad executing...");
	//InterfaceFunctions * intFunc = [[InterfaceFunctions alloc] init];
	
	//The main 4 buttons are created
	[InterfaceFunctions createNormalButton: bConnect atPosX: CENTER_POS_X atPosY: CENTER_POS_Y withSizeY: NORMAL_BUTTON_HEIGHT withSizeX: NORMAL_BUTTON_WIDTH withColor: [UIColor blackColor] withTag: 1 withTitle: @"Connect" withView: self ];
	[InterfaceFunctions createNormalButton: bConnect atPosX: CENTER_POS_X atPosY: CENTER_POS_Y + 60 withSizeY: NORMAL_BUTTON_HEIGHT withSizeX: NORMAL_BUTTON_WIDTH withColor: [UIColor blackColor] withTag: 1 withTitle: @"Disconnect" withView: self ];
	[InterfaceFunctions createNormalButton: bConnect atPosX: CENTER_POS_X atPosY: CENTER_POS_Y + 60*2 withSizeY: NORMAL_BUTTON_HEIGHT withSizeX: NORMAL_BUTTON_WIDTH withColor: [UIColor blackColor] withTag: 1 withTitle: @"Settings" withView: self ];
	[InterfaceFunctions createNormalButton: bConnect atPosX: CENTER_POS_X atPosY: CENTER_POS_Y + 60*3 withSizeY: NORMAL_BUTTON_HEIGHT withSizeX: NORMAL_BUTTON_WIDTH withColor: [UIColor blackColor] withTag: 1 withTitle: @"Exit" withView: self ];

}
My main class inherits from UIViewController and the InterfaceFunctions class from NSObject.

The error log is:

Code:
[Session started at 2011-03-20 18:25:47 +0200.]
2011-03-20 18:25:48.482 Estel[60455:207] Main viewDidLoad executing...
2011-03-20 18:25:48.484 Estel[60455:207] +[InterfaceFunctions createNormalButton:atPosX:atPosY:withSizeY:withSizeX:withColor:withTag:withTitle:withView:]: unrecognized selector sent to class 0x5188
2011-03-20 18:25:48.486 Estel[60455:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[InterfaceFunctions createNormalButton:atPosX:atPosY:withSizeY:withSizeX:withColor:withTag:withTitle:withView:]: unrecognized selector sent to class 0x5188'
*** Call stack at first throw:
(
	0   CoreFoundation                      0x00db2be9 __exceptionPreprocess + 185
	1   libobjc.A.dylib                     0x00f075c2 objc_exception_throw + 47
	2   CoreFoundation                      0x00db47bb +[NSObject(NSObject) doesNotRecognizeSelector:] + 187
	3   CoreFoundation                      0x00d24366 ___forwarding___ + 966
	4   CoreFoundation                      0x00d23f22 _CF_forwarding_prep_0 + 50
	5   Estel                               0x00003769 -[Main viewDidLoad] + 504
	6   UIKit                               0x0036965e -[UIViewController view] + 179
	7   UIKit                               0x00367a57 -[UIViewController contentScrollView] + 42
	8   UIKit                               0x00378201 -[UINavigationController _computeAndApplyScrollContentInsetDeltaForViewController:] + 48
	9   UIKit                               0x00376831 -[UINavigationController _layoutViewController:] + 43
	10  UIKit                               0x00377b4c -[UINavigationController _startTransition:fromViewController:toViewController:] + 524
	11  UIKit                               0x00372606 -[UINavigationController _startDeferredTransitionIfNeeded] + 266
	12  UIKit                               0x0048ae01 -[UILayoutContainerView layoutSubviews] + 226
	13  QuartzCore                          0x01c70451 -[CALayer layoutSublayers] + 181
	14  QuartzCore                          0x01c7017c CALayerLayoutIfNeeded + 220
	15  QuartzCore                          0x01c6937c _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310
	16  QuartzCore                          0x01c690d0 _ZN2CA11Transaction6commitEv + 292
	17  UIKit                               0x002be19f -[UIApplication _reportAppLaunchFinished] + 39
	18  UIKit                               0x002be659 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 690
	19  UIKit                               0x002c8db2 -[UIApplication handleEvent:withNewEvent:] + 1533
	20  UIKit                               0x002c1202 -[UIApplication sendEvent:] + 71
	21  UIKit                               0x002c6732 _UIApplicationHandleEvent + 7576
	22  GraphicsServices                    0x016e8a36 PurpleEventCallback + 1550
	23  CoreFoundation                      0x00d94064 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
	24  CoreFoundation                      0x00cf46f7 __CFRunLoopDoSource1 + 215
	25  CoreFoundation                      0x00cf1983 __CFRunLoopRun + 979
	26  CoreFoundation                      0x00cf1240 CFRunLoopRunSpecific + 208
	27  CoreFoundation                      0x00cf1161 CFRunLoopRunInMode + 97
	28  UIKit                               0x002bdfa8 -[UIApplication _run] + 636
	29  UIKit                               0x002ca42e UIApplicationMain + 1160
	30  Estel                               0x00002490 main + 102
	31  Estel                               0x00002421 start + 53
	32  ???                                 0x00000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException'


I've also tried to import Foundation/Foundation.h in the two class and it also crashed, equal than when I try to put @class InterfaceFunctions in the Main class.

I hope you can help me, because I'm lost.

Thanks in advance.

Last edited by Newlog; 04-04-2011 at 11:42 AM.
Newlog is offline   Reply With Quote
Old 04-04-2011, 12:27 PM   #2 (permalink)
Registered Member
 
Join Date: Jan 2011
Location: South Florida, US
Posts: 357
lgehrig1 is on a distinguished road
Default

The exception message is the giveaway - unrecognized selector.

Usually means typo, case error, something like that. In your particular case, the last argument is supposed to be "inView" and you are sending "withView".
lgehrig1 is offline   Reply With Quote
Old 04-04-2011, 12:56 PM   #3 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 8
Newlog is on a distinguished road
Default

Oh my god, I'm stupid!

I've changed that, now is all 'inView' and, now, the problem is here:

Code:
[Vista addSubview:bMyButton];												//The button is added to the view
And the log says:

Code:
[Main addSubview:]: unrecognized selector sent to instance 0x4d10ad0
Isn't correct the addSubview?

Thanks for the answer.
Newlog is offline   Reply With Quote
Old 04-04-2011, 01:20 PM   #4 (permalink)
Registered Member
 
Join Date: Jan 2011
Location: South Florida, US
Posts: 357
lgehrig1 is on a distinguished road
Default

Quote:
Originally Posted by Newlog View Post
Oh my god, I'm stupid!

I've changed that, now is all 'inView' and, now, the problem is here:

Code:
[Vista addSubview:bMyButton];												//The button is added to the view
And the log says:

Code:
[Main addSubview:]: unrecognized selector sent to instance 0x4d10ad0
Isn't correct the addSubview?

Thanks for the answer.
I'm going to guess that Main is a UIViewController? You'd want the view property of Main, not Main itself.

Aren't you getting all kinds of compiler warnings? Saying things like "... may not respond to ..."? You can solve these problems long before you try running the program.
lgehrig1 is offline   Reply With Quote
Old 04-04-2011, 03:52 PM   #5 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 8
Newlog is on a distinguished road
Default

Uff, thank you very much!

You are completly right! In fact, the Main class is an UIViewController, and yes, I had those warnings, but I thought that they mean that the Main class didn't have access to the methods, because of their visibility or something.

Well, thanks a lot. For your speed and effectiveness answering and guessing

See you around here.
Newlog is offline   Reply With Quote
Old 06-03-2011, 05:47 AM   #6 (permalink)
Registered Member
 
Join Date: Jun 2011
Location: Sydney, Australia
Posts: 44
new2objectivec is on a distinguished road
Default

Hope it's ok to expand the code a bit..

I quite like this area, create a shared function for shared button and other functions.

Got a question, how do you handle the following function to add an action to be called when the button is clicked?

[buttonObj addTarget:self
action:@selector(buttonPressedAction)
forControlEvents:UIControlEventTouchUpInside];

I was thinking how to change the declaration to pass the "self" and "buttonPressedAction" names over??

Code:
+ (void)createNormalButton:	(UIButton *) bMyButton 
				   atPosX: (double) X 
				   atPosY: (double) Y 
				withSizeY: (double) HEIGHT 
				withSizeX: (double) WIDTH 
				withColor: (UIColor *) cMyColor 
				  withTag:(int) iTag 
				withTitle: (NSString *) sTitle
				inView: (UIView *) Vista
I was thinking of adding 2 parameters:

Code:
				 withSelfID: (id)buttonSelfID 
			   withActionID: (id)selectorID
But it doesn't work, any better idea? Thanks!
new2objectivec is offline   Reply With Quote
Old 06-08-2011, 06:58 AM   #7 (permalink)
Registered Member
 
Join Date: Jun 2011
Location: Sydney, Australia
Posts: 44
new2objectivec is on a distinguished road
Default

I got it working now:

Code:
 withSelfID: (id)buttonSelfID 
 withActionID: (SEL)selectorID
and inside the method add this line:

Code:
[bMyButton addTarget:buttonSelfID action:selectorID forControlEvents:UIControlEventTouchUpInside];

and call it using

Code:
[InterfaceFunctions createNormalButton: buttonName 
							atPosX: 180 
							atPosY: 420 
						 withSizeY: 30 
						 withSizeX: 80 
						 withColor: [UIColor grayColor] 
						   withTag: 12345 
							 withTitle: @"Click Me" 
							withSelfID:self 
					withActionID:@selector(buttonClickMeClicked)
							inView: self.view];
If you have any other way to improve this, please let me know.
Thanks!
new2objectivec 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: 478
16 members and 462 guests
7twenty7, AlanFloyd, AppsBlogger, David-T, HemiMG, iAppDeveloper, imac74, Jaxen66, lovoyl, Music Man, mutantskin, Paul Slocum, SLIC, solardrift, unicornleo, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,683
Threads: 94,131
Posts: 402,932
Top Poster: BrianSlick (7,990)
Welcome to our newest member, unicornleo
Powered by vBadvanced CMPS v3.1.0

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