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.
Related
I have a method with an id parameter in it. I also have an id as a property inside my class. My question is in the init method is there a way I can determine if the passed parameter can be retained so I can do something like:
someProperty = [idParameter retain];
Thanks
All Objective-C objects (i.e. anything inheriting from the NSObject class or implementing the NSObject protocol) implement retain. It's implemented by the NSObject class and it's a required method for the protocol, so you cannot have an Objective-C object that you cannot call retain on.
The only time you'd not be able to call it in these circumstances is if your variable of type id was not pointing to an Objective-C object. This would be a mistake, do not do this.
Every object that inherits from NSObject has a respondsToSelector: method. (Documentation)
Therefore, you could write:
if ([idParameter respondsToSelector:#selector(retain)])
someProperty = [idParameter retain];
Here's what I'm looking to do. I have a UITableViewController that has a variable, bInternetOK, that I need to reference from a class that the UITableViewController instantiates. This way, the class can make sure the internet is available before it tries to do some work. In the .h of the UITableViewController, I've defined the variable like this:
Boolean bInternetOK;
And I've set a property like so:
#property (nonatomic) Boolean bInternetOK;
In the .m of the UITableViewController, I've synthesised the variable like this:
#synthesize bInternetOK;
I instantiate the class (TheNetworkClass) like this and then call the function to start the work:
TheNetworkClass *TheNetworks = [[TheNetworkClass alloc]init];
[TheNetworks StartUpTheWork];
Inside of the TheNetworkClass class, I'm trying to reference the variable bInternetOK that is in the UITableViewController class. How do I do this?
Thanks!
You could give the instance of your TheNetworkClass object a reference to your table view controller instance when you create it.
Or you could just give your TheNetworkClass object the boolean, if it only checks it on creation.
Or, if the table view can be accessed as a property on your application delegate, you can get the application delegate with [UIApplication sharedApplication].delegate and then access that property to get to the table view.
#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.
Can you explain "An instance method you define in a category of the NSObject class might be performed not only by instances but by class objects as well", i have come across this sentence while reading an Objective C guide...! But i am not able to get it.
Instance method is a method you can call on an object (as opposed to a class). Each object is an instance of a certain class (just as each of us is an instance of a human1), that’s why we talk about instance methods. For example when you say [someArray count], you are calling an instance method called count on some array object (= instance of the NSArray class).
Class methods are called on classes, for example [UIApplication sharedApplication] is a class method of the UIApplication class. You can’t call an instance method on a class, nor can you call a class method on an object.
Category is a way to extend behaviour of existing classes, a kind of plugin you stick on an existing class. For example by writing this:
#interface NSObject (SampleCategory)
- (void) sayFoo;
#end
#implementation NSObject (SampleCategory)
- (void) sayFoo {
NSLog(#"Foo!");
}
#end
…you make it possible to call [anObject sayFoo] on any object derived from NSObject. And now we are getting to the point of the sentence: NSObject seems to be special, because when you declare an instance method of NSObject using a category (such as our sayFoo), you can call this method even as a class method: [NSObject sayFoo], [NSView sayFoo], etc.
1] Sorry, Googlebot!
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.