Triggering methods in Obj c - iphone

I would like to detect taps with a tapRecognizer in one class that triggers another class to create an object of a third class.
How Can I trigger this method from the tap detecting class without creating a new object of the second class?
Or would creating a new object of this second class be fine? It would lose all of its old data though.
How is triggering like this done? I tried something but it just warned about an object is being accessed in a class method.
e.g.
ViewController Class 1
+(void) setupClass2
{
Class2 class = .........
class.someValue = ......
self.navigationController pushViewController:class ......
}
// The calling/trigger class should be able to invoke the setupClass function e.g.
[Class1 setupClass];
How is this done correctly?

You have to declare a protocol with methods like this:
-(void) class1:(Class1*) c didReceivedTouch:(UITouch*) e; //or any other params that you need
Then you assign a delegate that conforms to that protocol (class 2 in your case). Create the class 3 object in that callback.

Declare a protocol with method suggested by Max
#protocol UrProtocolName
#required/#optional
-(void) class1:(Class1*) c didReceivedTouch:(UITouch*) e; //or any other params that you need
#end
Then implement the protocol in your second class
#interface 2ndClass < UrProtocolName>
.....
#end
#implementation 2ndClass
-(void) class1:(Class1*) c didReceivedTouch:(UITouch*) e
{
3rdClass class = .........
self.navigationController pushViewController:class animated:YES];
}
#end
But I really don't understand why do you want to gor for 2nd Class, when all you want is to create object of 3rdClass and push it on navigationController. You can have 2ndClass' object as a member of firstClass initialized while constructing the object of 2ndClass, and den you can have one method in 2ndClass which will simple create an object of 3rdClass, and you can call this method of 2ndClass object as many times, till your firstClass object is in scope.

Related

Adding custom behavior and state to all my classes

I want to add functionality and another property to ALL of my classes.
So I wrote a category:
#implementation NSObject (MyCategory)
And I declared a static property in it:
static MyObj myObj;
And I created 2 class methods to get and set it:
+ (MyObj) getMyObj {
return myObj;
}
+ (void) setMyObj:(MyObj)obj {
myObj = obj;
}
Now I imported NSObject+MyCategory.h in my .pch file, so all classes will be effected by this. Indeed all classes now have the new functionality and state:
#import "NSObject+MyCategory.h"
The problem is that when I set myObj, it changes myObj on all classes. All classes share 1 myObj.
I want each class to have its own myObj that is added using the category. I don't want one myObj, rather I want as many myObj's as classes. Each class should have its own myObj.
Thanks,
Nur
You can not add properties instance variables to a class in categories. Either subclass NSObject or use associated objects.
Your solution adds a single static variable (not "property", in Objective-C that means something else), there is no way using categories to add a static variable per class.
However your idea is close to what will work for you; if you can only have one variable and want to store many values what can you use? A dictionary.
static NSMutableDictionary *References;
+ (void) load
{
// load is called exactly once when this category is first loaded,
// setup the dictionary
References = [NSMutableDictionary new];
}
+ (void) setMyObj:(MyObj)reference
{
// NSMutableDictionary will take any object reference as a key,
// for a class method self is a reference to the unique class object,
// so use self as the key and store the reference
[References setObject:reference forKey:self];
}
+ (MyObj) getMyObj
{
// returns previously stored reference or nil if there isn't one for this class
return [References objectForKey:self];
}
HTH

Creating a class whose methods can be called without an instance of an object (static class)

I'm new to objective c and i want to create a class containing certain methods that can be called in any of my other classes, mostly helper methods. im still learning the syntax and i dont know how to declare it properly
kind of like in java Integer.parseInt( );
Thanks!
Static methods in objective-c are called 'class methods' and can be declared with '+' symbol (while instance methods with '-'), e.g.:
- (void) instanceMethod;
+ (void) classMethod;
To call class method use class name:
[MyClass classMethod];
Those are called (unsurprisingly) class methods. You can declare one by using + instead of - in the method signature, e.g.
#interface MyInteger : NSObject
+ (MyInteger *)parseInt:(NSString *)str;
#end
This method is then called on the class itself, e.g. [MyInteger parseInt:#"12"].
Of course, since this is C, if your class method doesn't actually have much relation to any particular class, you could just define it as a C function instead.
NSInteger myParseInt(NSString *str);
When you see a - sign in front of a method, it's an instance method. That means you can only call that method on an instance of a class.
If you want to create a class method, all you need to do is change that - to a +.
they are called class methods. they are declared and used like this:
#interface MONClass : NSObject
+ (NSString *)convertString:(NSString *)string;
#end
in use:
NSString * converted = [MONClass convertString:string];

Objective-C - Access method from other controller

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.

Calling a method with return type "void" in same file

I've got a simple question.
In Objective-C, when you have a method you want to call, with a return type of void, how you you call it from another method?
The way I've been doing it in my application is this:
[self nameOfMethod];
But that causes Xcode to spit out the following error:
Method '-nameOfMethod' not found (return type defaults to 'id')
Though it seems to still be executing.
Am I calling it right, or is there a better way?
Thanks!
I’m guessing you haven’t declared -nameOfMethod in the class interface and you’re calling it from another method whose implementation precedes the implementation of -nameOfMethod, i.e.:
- (void)someMethod {
[self nameOfMethod];
}
- (void)nameOfMethod {
// …
}
When the compiler is parsing -someMethod and -nameOfMethod hasn’t been declared in the class interface, it generates a warning because it doesn’t know about -nameOfMethod yet.
There are essentially two solutions for this. You could reorder the implementation file so that -nameOfMethod appears before -someMethod, but that’s not always possible. A better solution is to declare -nameOfMethod in the class interface. If -nameOfMethod is supposed to be called by clients of your class, place it in the corresponding header file. On the other hand, if -nameOfMethod is only supposed to be called inside your implementation file, use a class extension. Supposing your class is named SomeClass, this is what your header and implementation files would look like:
// SomeClass.h
#interface SomeClass : NSObject {
// … instance variables
}
// … external methods
- (void)someMethod;
#end
// SomeClass.m
#import "SomeClass.h"
#interface SomeClass () // this is a class extension
// … internal methods
- (void)nameOfMethod;
#end
#implementation SomeClass
- (void)someMethod {
[self nameOfMethod];
}
- (void)nameOfMethod {
// …
}
#end
Using class extensions, the order of method implementations won’t matter.
You need to make sure that your interface file contains a definition for nameOfMethod - so;
-(void) nameOfMethod;
You're calling it correctly, but make sure that the interface for your (void) method is in your .h file.

Implementing run time polymorphism and inheritence

What is the equivalent concept of interface in java in objective C. I want to create a interface and define some common variables and methods which will be shared by many classes inheriting from my interface. When any class outside want to use a child class it will get a reference object of my interface which will point to correct child class.
How to achieve this in Objective C?
An Objective C equivalent of Java interfaces is called "protocol".
A small intro can also be found here, and if you want a full reference, it's at Apple
In Objective-C, you can achieve this by making use of Protocols. A protocol is basically the precursor to an interface in Java, so most of the behavior should come naturally.
A protocol declaration looks like the following:
#protocol Foo
-(void) foo;
-(int) boo: (int) arg;
#end
It may be implemented by a class. In the following case, you would say MyClass conforms to the Foo protocol.
#interface MyClass <Foo>
{
}
#end
#implementation MyClass
-(void) foo {
//do something
}
-(int) boo: (int) arg {
//do something else
return arg;
}
#end
Finally, you can pass them around like this:
-(void) someMethod: (id<Foo>) arg;
If you need to be more specific about the object, they can also be used like this:
-(void) someMethod: (NSObject<Foo> *) arg;