Objective-C - Access method from other controller - iphone

i have a little question about getting access to a method in another controller, nu i am trying this.
So for example i have the controller A and B. In the controller A i have programmed a method, now i want to get access this through controller B.
What i have done in class A in the header file:
+(void)goBack;
and in the implementation file:
+(void)goBack {
NSLog(#"go back");
}
in the controller B i do this to get access to the method in controller A:
+(void)goPreviousArticle:(id)sender {
ViewProductInformation_ViewController *theInstance = [[ViewProductInformation_ViewController alloc] init];
[theInstance goBack];
}
However when i execute the program, then it does not work, the program just shuts down, when i do command click on the function goBack in controller B i get referred to the method in controller A.
Does anybody have an idea what the problem could be?
thanks in advance,
snowy

It's quite easy ... you just mixed the class and instance-method declaration: The "+" sign indicates that the method is a class method. In your case it should be a "-" so
-(void)goBack; // a instance method declaration!
Hope this helps.
Class vs instance method declaration ... see also What is the difference between class and instance methods?

You are declaring goBack as a CLASS method (with the preceding "+"). Change the + to a -.

Since goBack is a static method of Class A, you don't need an instance of A to call it's method, you can just call it like so:
[ClassA goBack];

You don'y need to declare static functions you can writ like this:
-(void)goBack {
NSLog(#"go back");
}
In the class A and same in the class B:
-(void)goPreviousArticle:(id)sender {
ViewProductInformation_ViewController *theInstance = [[ViewProductInformation_ViewController alloc] init];
[theInstance goBack];
}
Then use them. I think in that case application will not crashed.

Related

Objective-C singleton only responds to class methods

I have a singleton class that is instantiated as follows:
#import "FavoritesManager.h"
static FavoritesManager *sharedFavoritesManager = nil;
#implementation FavoritesManager
+ (id)sharedManager {
#synchronized(self) {
if (sharedFavoritesManager == nil) {
sharedFavoritesManager = [[self alloc] init];
}
}
return self;
}
This returns an object, but for some reason it will only respond to class methods. If I call a instance method I get
+[FavoritesManager testMethod]: unrecognized selector sent to class 0x59198
For what it's worth, this is what testMethod looks like:
- (void)testMethod {
NSLog(#"Test");
}
and I'm absolutely positive it's declared in the interface. I've used this exact code in other classes and it works like a charm, so I don't really understand what the problem is here. One thing that is suspicious is the plus sign in +[FavoritesManager testMethod], but I can't explain it. Any ideas?
EDIT: I was confusing public/private and class/method terminology. Thanks to everyone who pointed that out.
If you want to call testMethod from another class method then you need:
+ (void)testMethod {
NSLog(#"Test");
}
The reason is that if you call a class method then there's no instance, so nothing on which to call instance methods. But probably you want to call:
[[FavoritesManager sharedManager] testMethod];
Which means 'get the shared instance, then call testMethod on it'. Thinking as I type, you might also like to add:
+ (void)forwardInvocation:(NSInvocation *)anInvocation
{
id sharedManager = [self sharedManager];
if ([sharedManager respondsToSelector:
[anInvocation selector]])
[anInvocation invokeWithTarget:sharedManager];
else
[super forwardInvocation:anInvocation];
}
Which is the Objective-C means for message forwarding. So if the metaclass FavoritesManager receives a message it can't respond to, it lets its shared manager instance have a go. That means that:
[FavoritesManager testMethod];
Becomes functionally equivalent to (though a little slower than):
[[FavoritesManager sharedManager] testMethod];
Providing that you haven't implemented a class method in addition to an instance method. You can learn more about message forwarding in Apple's official documentation.
The error indicates that you're sending the message testMethod to the class, rather than an instance.
The reason for this is that your sharedManager method is incorrect. You are currently returning self, which, in this class method, is the class itself. This means that when you write [[FavoritesManager sharedManger] testMethod] you end up sending testMethod to the class. Since testMethod isn't a class method, you get an error.
You should have return sharedFavoritesManager; at the end of sharedManager, not return self;. The latter is correct only in instance method initializers.
Also, as dbrajkovic commented, you seem to be confused about public/private and class/instance methods. Strictly, ObjC has no private methods. You can hide the declaration, which will cause a compiler warning, but the message will still be sent and the method will be called. The + and - distinguish class methods from instance methods; the distinction is which kind of object you send a message to. Info here: What is the difference between class and instance methods?
The error is right you must be calling [FavoritesManager testMethod] which means you're trying to call a class method. I believe you want [[FavoritesManager sharedManager] testMethod];
+ at the start of a method declaration means that it's a class method, - means that it's an instance method. Do this:
+(void)testMethod {
NSLog(#"Test");
}
If you want to invoke testMethod on your sharedManager, then keep the testMethod declaration as you have it and instead change your invocation to
[[FavoritesManager sharedFavoritesManager] testMethod];
Either will work, and choosing between the two is a matter of app design.
Instead try
[[FavoritesManager sharedFavoritesManager] testMethod];
there are no priavte methods in obj-c.
But anyway on a singleton you are always calling from the outside of the class, so only declare "public methods". for detailed help post your code.
Call your singleton instance:
[[ FavoritesManager sharedManager] testMethod];

Objective-C passing methods as parameters

How do you pass one method as a parameter to another method? I'm doing this across classes.
Class A:
+ (void)theBigFunction:(?)func{
// run the func here
}
Class B:
- (void)littleBFunction {
NSLog(#"classB little function");
}
// somewhere else in the class
[ClassA theBigFunction:littleBFunction]
Class C:
- (void)littleCFunction {
NSLog(#"classC little function");
}
// somewhere else in the class
[ClassA theBigFunction:littleCFunction]
The type you are looking for is selector (SEL) and you get a method's selector like this:
SEL littleSelector = #selector(littleMethod);
If the method takes parameters, you just put : where they go, like this:
SEL littleSelector = #selector(littleMethodWithSomething:andSomethingElse:);
Also, methods are not really functions, they are used to send messages to specific class (when starting with +) or specific instance of it (when starting with -). Functions are C-type that doesn't really have a "target" like methods do.
Once you get a selector, you call that method on your target (be it class or instance) like this:
[target performSelector:someSelector];
A good example of this is UIControl's addTarget:action:forControlEvents: method you usually use when creating UIButton or some other control objects programmatically.
Another option is to look at blocks. It allows you to pass a block of code (a closure) around.
Here's a good write up on blocks:
http://pragmaticstudio.com/blog/2010/7/28/ios4-blocks-1
Here's the apple docs:
http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Blocks/Articles/00_Introduction.html
Objective C makes this operation relatively easy. Apple provides this documentation.
To directly address your question, you are not calling a function, but a selector. Here is some sample code:
Big Function:
+ (void)theBigFunction:(SEL)func fromObject:(id) object{
[object preformSelector:func]
}
Then for class B:
- (void)littleBFunction {
NSLog(#"classB little function");
}
// somewhere else in the class
[ClassA theBigFunction:#selector(littleBFunction) fromObject:self]
Then for class C:
- (void)littleCFunction {
NSLog(#"classC little function");
}
// somewhere else in the class
[ClassA theBigFunction:#selector(littleCFunction) fromObject:self]
EDIT: Fix selectors sent (remove the semicolon)
You can use Blocks for this purpose. http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Blocks/Articles/00_Introduction.html

method may not respond to

Inside a class (named TimeDetails), I need to call a method (a class method) but during the compilation I receive this message: TimesDetails may not respond to 'generateTimeOfPassage' , (one method call another and send a NSString object. (In this case return nil).
in the .h file I declare the method like this:
+(NSString *)generateTimeOfPassage:(NSString *)_timeString;
in the .m file :
-(void)initWithTimeData:(NSString *)_timeString{
[super init];
passage=[self generateTimeOfPassage:_timeString]; ---I have the message here...
}
+(NSString *)generateTimeOfPassage:(NSString *)_timeString{
return nil;
}
Thanks !
Maxime
+generateTimeOfPassage is a class method, so it should be called using class name, not an object:
passage=[TimeDetails generateTimeOfPassage:_timeString];
When there's a + in front of it, then it's a class method (in Java, this would be called a static method). When there's a -, it's an instance method. See here for further explanation.

Objective-C: Share object between two view controllers

I have an iPhone application and I do this in my MyView1Controller:
MyView2Controller *myV2C = [[MyView2Controller alloc] initWithNibName:#"MyView2" bundle:nil];
myV2C.shareObject = self.shareObject;
[self.navigationController pushViewController:myV2C animated:YES];
[myV2C release];
So I push shareObject to the next ViewController. There, in viewDidLoad, I set a property of shareObject like this:
self.shareObject.myText = #"Test String";
So in MyView2Controller, everything is okay, the String is set. But going back to the previous MyView1Controller with the left upper "back" Button, the value of shareObject.myText is not set to Test String.
So, how can I do this? I want to give the View2Controller an object which can be modified, and the modifications I want to have in the View1Controller.
Does anyone know? Thank you in advance & Best Regards, Tim.
OR you can even try using the Singleton pattern
http://www.cocoadev.com/index.pl?SingletonDesignPattern
You can use delegation pattern and/or key value observations
EDIT:
You can achieve this my creating a single object, and then use that object in all classes, wherever you want. For this you can use singleton class, Or you can create a class method. For ex:If I have class A, and I want to push some values in B class, So if I modify the values in B, the newly updated value can be retrieved in a too, or in any other class. In that case, you can create a different class (usually subclass of NSObject),and then write the getter/settor methods in that.
Suppose the name of this newly created class is Manager, then in Manager. m create getter/setter methods, like
NSString *strGlobal;
+(void)setString:(NSString *)strTemp
{
if(!strGlobal)
{
strGlobal = [[NSString alloc] init];
}
//point to same location
strGlobal = strTemp;
}
+(NSString *)getMySavedString
{
return strGlobal;
}
Now In you class A , where you want to send the value to Class B controller, call the setter method to set the value, like: -
-(void)navigateto_ClassB
{
//Setting the value, that should be sent to the other controller
[ManagerClass setString:#"Hello"];
[self.navigationController pushViewController:[[[ClassB alloc]init] autorelease] animated:YES];
}
Now In class B (Or wherever you want to get the saved value, use getter method, like:-
NSString *strSavedValue = [ManagerClass getMySavedString];
No you will have the value, If you want to update the value from anywhere, the again call the setter method, with new value. For ex, i want to update the value in class B, then
[ManagerClass setString:#"Hello Upadted"];
Now this value is updated to same memory location, retrieve it from anywhere using getter method created in Manager class.
As I stated earlier, this is the easiest but not the best approach. Alternatively, you can achieve same functionality with delegate patterns, KVOs (Key value observations) and/or singleton class.
tia was right: "You did it right already, so there must be something wrong somewhere".
It was my fault, so the source code in my question is correct.

How can I dynamically create an instance of an class?

Example:
I have 10 view controllers, which are all allocated and initialized in the same way:
UIViewController *controller = [[MyViewController alloc] initWithNib];
(note that -initWithNib is a custom method of a UIViewController subclass)
The next view controller class is OtherViewController, and so on. I want to load the view controllers lazily, just when I need them. But to do that, I need to have some kind of "array" that will give me the corresponding class for a given index, so that I can initialize it.
I ended up creating a method with a big switch-statement, that will just do that nasty allocation and initialization separately for every single view controller. I'm not happy with that. There it would be much better if I could assign the appropriate class to a variable, and then at the end of the switch statement just allocate and initialize that class from the variable.
Is there a way to achieve that?
EDIT: I've found a function
id class_createInstance(Class cls, size_t extraBytes)
and every class seems to have a property "class". But I can't assign it to an instance variable. This doesn't work:
Class cls = [UIImage class];
cls *image = [cls imageNamed:#"avatar.png"];
The first line compiles. But the second one gives an error: "image undeclared".
If you know the names of the classes at compile time, you can assign the classes to Class variables. For example:
static Class factory[2];
factory[0] = [MyViewController1 class];
factory[1] = [MyViewController2 class];
...
Then you could have (classid would be a constant known at compile time that would map to a desired class:
-(UIViewController*)createViewController:(int)classid
{
return [[factory[classid] alloc] init];
}
Assuming that method is defined in a class named MyFactory, you can then do:
MyFactory * fac = [[MyFactory alloc] init];
UIViewController * v1 = [fac createViewController: 0]; // typed
id v2 = [fac createViewController: 1]; // untyped
If you don't have the compile time name of the class, you can simply do the following:
#include <objc/objc-runtime.h>
id object = [[NSClassFromString(#"TheClassName") alloc] init];
Since your original question involves a set of UIViewControllers though, there's no reason to lose type safety with the latter method.
You want to use reflection:
id controller = class_createInstance(NSClassFromString(#"your class name"), 0/*extra bytes*/);
Objective-C Runtime Reference
I blogged about this last month at:
http://igotosoft.blogspot.com/2009/05/dynamically-creating-viewscontrollers.html
Essentially, it involves a new class I call the ClassConstructor, which takes a class name, init method name, and comma separated arguments. When you need to create an instance of that class, just use your [myClassConstructor create];
What you want is Reflection. No idea of objective-c has it though - but the term might help you Google for your answer better.
The Objective C Reference also will be a good place to look to get the calls. Try searching for Objective-C 2.0 Runtime Reference since I cant add links