How to get super class delegate and update the variable in super class from subclass - iphone

#interface first{
nsstring one;
second secondobject;
}
#interface second{
nsstring two;
}
in the above classes from second class I want to update the first class "one" string.
I knew that we can update the "two" string from the first class
but I want to update the string "one" from second class
should not use appdelegate
should not use inheritance
*I want to know like our AppDelegate has the [[uiapplication sharedapplication]delegate]
by getting above delegate of appdelegate we can access properties of appDelegate class
Like this how can we can get the delegate for first class and access the properties of first class from the second class.
if any pictorial tutorial for tree structure please specify the link
here is my structure

It seems that you don't really understand the meaning of delegates and what they are used for.
In your example class first aggregates (contains) instance of class second. It means that the reference to that class is an ivar. You can access all public members, properties and methods of that instance from class first.
If you want to access the ivars of the superclass then you can do that like they were declared in the child class.
Delegates are used when you need to notify another object about something during execution. Objects usually have weak references (they don't retain) to delegates.
So i think the best choice for you would be to read some good book about object oriented programming. This is really good book about that

You can pass a pointer from the first class to the second, and the second can use this to call methods or access data members in the first class.
Or if first will be a singleton class (like UIApplication), you can set up a class method (like sharedApplication) that second can call to get a reference to the first object.
Generally, structuring iPhone apps using the MVC pattern helps to reduce these kinds of tangles.

Related

objective-c: multiple class defintions in one .m file, and calling methods

I've defined two classes in an m file, the first subclassing UIView and the second UIViewController. The UIViewController is instantiated at some point, and the vc is who instantiates my first class.
the first class implements the touchesEnded method, to simulate a button. when the touchesEnded method is fired in the first class, is it possible to easily call a method defined in the 2nd class, without going into delegates and such?
I tried playing with selectors with no luck
is it possible to easily call a method defined in the 2nd class
Yes, assuming that you are creating an instance of the second class and calling the method on that instance.
Regardless of whether the two classes are subclasses of the same type, or in the same or different files, you need a reference to an instance of that class to call a method on it, or force it to perform a selector.
The proper OO way to do this is with delegates, but you could theoretically do something like pass a reference to view 2 into view 1 when you create the views. If you create them in IB you could create outlets so they reference each other that way.
In short: Yes, it is possible and easy to do, but I can't give you too much in terms of specific code without a more specific example of your situation

iphone - delegates

I have an object that belongs to a class. Lets call it classA. This classA is a subclass of classB.
ClassA has a delegate protocol.
I create an classA object on my main code. This object is inside a view. Lets call it viewX.
Now I am in classB and I would like to get a reference to viewX.
remember that classA has a delegate protocol, so it has a reference to its delegate, that is the viewController where viewX is. From class A I can access viewX doing [delegate view], but how do I do that from classB???
thanks.
If you have access to class B, I would say you should add a variable to the class of type id, and set that variable as the view, and that would be a very easy way to do it. Otherwise, I don't think it's possible. But I may be wrong.

How do I reference a pointer from a different class?

First off, I despise singletons with a passion. Though I should probably be trying to use one, I just don't want to. I want to create a data class (that is instantiated only once by a view controller on loading), and then using a different class, message the crap out of that data instance until it is brimming with so much data, it smiles.
So, how do I do that? I made a pointer to the instance of the data class when I instantiated it. I'm now over in a separate view controller, action occurs, and I want to update the initial data object. I think I need to reference that object by way of pointer, but I have no idea how to do that. Yes, I've set properties and getters and setters, which seem to work, but only in the initial view controller class.
There's no need to use a singleton if you don't like the pattern or if it doesn't fit. Assuming you are creating your second view controller in the first one, just declare an ivar and property for your model object in your second view controller and when you instantiate it, assign the model object to this property.
Make a global variable for your object and store it there on creation. You can wire that up in the init method (probably bad style), or from the caller or via interface builder. Just make your variable known in the files that use it.
Or - well - use some kind of singleton pattern and get the instance directly from that class. Looks much cleaner.
Seriously use a singleton. In case you don't like them cause you don't know the code:
#interface Order : NSObject {
NSMutableArray *order;
}
#property (nonatomic, retain) NSMutableArray *order;
+ (Order *)sharedInstance;
#end
#import "Order.h"
#implementation Order
#synthesize order;
+(Order *)sharedInstance {
static Order *myInstance = nil;
#synchronized(self) {
if(!myInstance) {
myInstance = [[Order alloc] init];
}
}
return myInstance;
}
-(void)dealloc {
[order release];
[super dealloc];
}
#end
Um. Hello. Isn't Core Data a good enough framework for you? It allows you to have a single persistent store and multiple contexts to manage updates and merging of changes in response to notifications. I may be out of line here, but seeing how you start the question with a strong opinion about a well accepted pattern in your first question indicates that you have not spent much time discovering the ways in which the objective c runtime and Foundation classes in iOS can collaborate to achieve a task. In any software, one object and only one object owns a specific resource. You should embrace singletons. I suggest you spend some time reading http://www.cocoadesignpatterns.com/. Oh yeah, check out the meaning of KVO.
Why not make it a property of your app delegate? That way you don't have to use the singleton pattern but you are taking advantage of Apple's already existing usage of the singleton pattern.
Don't forget that Objective-C is a superset of C.
Basically, a data class is a plain C struct.
If you want to access a variable of that class from another class, make it global.
mydata.h:
struct MyData {
// Whatever data fields that you need, e.g.:
NSInteger value;
};
extern struct MyData mydata;
mydata.c:
struct MyData mydata = {
// Whatever initial value that you find relevant, e.g.:
.value = 42;
};

calling function in a controller from a class (objective-c)

I'm writing an iPhone application in Objective-C. I created a class (Foo.m) which I would like to be able to call a method in the controller (MainViewController.m) which instantiated it. How do I do this? Please provide an example. Thank you!
One way you can do this is to create a property in your Foo class that references its creator. You should not retain this reference to avoid circular references, but the code might look like the following
-(void)yourControllerClassMethod{
Foo* f = [[Foo alloc] init];
[f setOwnder:self];
}
In this case, your Foo class has a property called owner which is set when the Controller class makes a new Foo instance. Now from your Foo class you can call controller methods like this:
[[self owner] callSomeControllerMethod];
First of all, I'd recommend reading some design patterns books. If your Foo class is a Model, then why would your model communicate with the Controller? If Foo is a View, then why would it communicate with your controller?
Now, while I suspect your app has a design issue with the way you are structuring the code, there are ways to do this.
When MainViewController.m instantiates Foo, can you pass self in and have Foo retain a reference to it?
Alternatively, you should create a #protocol in Foo and when MainViewController creates Foo, have the MainViewController implements Foo's delegate.
You should probably check out Cocoa Design Patterns by Erik M. Buck and Donald A. Yacktman. It's an amazingly excellent book and it's quite comprehensible even if you aren't already familiar with design patterns in general.
It sounds like what you want to do is what the other guys were saying which is a pattern called Delegation. To see how delegation works, look at all of the built in classes that use it. Anything that has a delegate property and a protocol like UIBlablaDelegate is using delegation and you can do the same thing with your classes.

Accessing class method in iPhone

How can i access NSObject class method?
i have one NSobject class for AudioStreaming where the audio gets start and stops,
when i changing the tab i want to stop that streamer by method define in AudioStreaming class how can this be done.
Thank you.
A class method is invoked using the class name and the method. Thus if you have:
#interface AudioStreaming
{
// ...
}
+(void)startAudio;
+(void)stopAudio;
#end
then all you need to do to call these methods is:
[AudioStreaming startAudio];
// ... do other things...
[AudioStreaming stopAudio];
Note that you cannot refer to instance variables within a class method (as there is no current instance!).
If you want to implement a Singleton, this StackOverflow Singleton answer is a good start.