iOS - Passing Values from an Object to it's Containing Class - iphone

The setup:
I have an AppDelegate and declared on that is CustomUIViewController.h and in that controller I have RandomName.h declared (as an object, not a subclass) can I use [super methodName] or similar to trigger a method on CustomUIViewController from a method on RandomName.h?
Or do I have to pass it to the appDelegate and then from there to CustomUIViewController? (How I have been doing it)
Thanks
P.S. Coffee is good.

I think I've understood your question. Pardon me if I didn't ;-)
super doesn't work that way. You can simply call,
[appDelegate.customViewController methodName];
The other way is to pass the reference of the customViewController to RandomName object, something like,
[[RandomName alloc] initWithParent:self];
You have to keep the reference of self in initWithParent method, lets say the variable name is parent, and call the method like this,
[parent methodName];

Related

access to "super" through pointer to class's object

I have some class SimpleButton. It is a Child of Button class. Button class has some method, which is overridden in SimpleButton. Now, if I whant to get access to this method from simpleButton, I just need to call [super someMethod]; But how to get access to it from outside of simpleButton? I tried:
SimpleButton *button;
[(Button*)button.superclass someMethod];
but this does not works...
You can't do it like that. If you've overridden a method in a subclass then you have changed the publicly available method of that class to be the one in your implementation. You would have to have a separate method which just calls the super implementation:
Say you've overridden method foo. Create a new method, superFoo
[button superFoo];
Which internally does this:
-(void)superFoo
{
[super foo];
}
However, if you need to do this sort of thing then you probably need to rethink why you have overridden the method in the first place. Perhaps you should have a new method on your subclass instead which does the unique parts and calls the super implementation itself, rather than overriding.

Calling methods in different classes

This question is semi-similar to the one asked here
But the difference is that I'm using cocos2d.
I have a method named screenshot in my AppDelegate. It's obviously used to take a screenshot. I want to call this method in another class, but simply doing [self screenshot]; isn't working out because I get the warning 'GameOver' my not respond to '-screenshot'.
All I want is for the screenshot method to be called in GameOver.
Thanks!
You can't call a method that does not exists - it will generate a runtime error.
If you want to call a method of AppDelegate from GameOver you should have a pointer to AppDelegate object if the method is not static:
[appDelegatePointer screenshot];
If the method is static
[AppDelegate screenshot];
I suppose you have only one object AppDelegate and you can make a static method
+(AppDelegate) sharedDelegate;
of class AppDelegate that will return your object singleton. And so you will be able to call screenshot method from GameOver object or from any other place like this:
[[AppDelegate sharedDelegate] screenshot];
Such thing is done in CCDirector, CCTextureCache, SimpleAudioEngine, ... classes in Cocos2D

Would it be correct/ellegant use only alloc without init?

If we don't want to implement init method in our class, and bearing in mind that init in NSObject only returns an instance of the object without initialization, I don't see the point of calling init if we already get the instance with alloc. I have tried and it works, but I am not sure it won't cause future problems.
myClass *newObject = [myClass alloc];
instead of:
myClass *newObject = [[myClass alloc] init];
Thanks a lot.
No, just calling alloc would not be correct. alloc zeroes out all instance variables of the object, init then has the chance to set all or some instance variables to their default values. Some classes even use their init methods to create another instance and return that one instead of the one you allocated.
Many classes expect that their init methods get called and would possibly cause crashes if you don't call init. If you are talking about a custom class that inherits directly from NSObject and needs no initialization of instance variables, you might get away with [myClass alloc] but it is definitely not good programming style.
I think that it is not a good idea.
Read Cocoa Design Pattern, especially the "Two stage creation"
You can also read this article http://www.informit.com/articles/article.aspx?p=1398610
I think that it wouldn't matter much if you didn't implement a "- (id)init" because if you did, you would call NSObject's init method which just returns the same value you send to the method. Though it is a good idea to create your own init method to set your instance variable.
in runtime source code
perform -(id)init will call _objc_rootInit(self) and will return self. I guess only perform init is OK。

What does [super viewWillAppear] do, and when is it required?

-(void)viewWillAppear:(BOOL)animated{
//something here
[super viewWillAppear];
}
Is [super viewWillAppear]; always required? If not when and why do you use it?
First of all, the correct boiler plate should be:
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//something here
}
In other words, call super first, then do your own thing. And you have to pass the animated parameter to super.
You usually want to call the super class' implementation first in any method. In many languages it's required. In Objective-C it's not, but you can easily run into trouble if you don't put it at the top of your method. (That said, I sometimes break this pattern.)
Is calling super's implementation required? In the case of this particular method you could get unexpected behavior if you don't call it (especially if you have subclassed a UINavigationController for example). So the answer is no not in a technical sense, but you should probably always do it.
However, in many other methods there may be good reasons for not calling super.
Calling super method provide possibility to execute code in parent class.
Regarding your question according to Apple doc
So, yes, this method required.
In my experience, calling [super viewWillAppear] in the first line, when calling reloadData after that, makes it impossible to retrieve the previously selected row when coming back from a detail view. When [super viewWillAppear] is the last sentence, you can get the selected row and show the previously selected row hint. This happens only when using [tableView reloadData] inside viewWillAppear.
Lets say you have 2 class, a Parent and a Child. Child inherits from Parent. They have a method called greet which returns a string.
Here is what the parent method looks like:
Code:
-(NSString *)greet {
return #"Hello";
}
We want the child to learn from his parents. So we use super to say greet how Mommy would greet, but with our own little additions too.
Code:
// Inherits from Parent
-(NSString *)greet {
NSString *parentGreeting = [super greet];
return [parentGreeting stringByAppendingString:#", Mommy"]
}
So now Parent greets "Hello", and the Child greets "Hello, Mommy". Later on, if we change the parent's greet to return just "Hi", then both classes will be affected and you will have "Hi" and "Hi, Mommy".
super is used to call a method as defined by a superclass. It is used to access methods that have been overriden by subclasses so that the class can wrap its own code around a method that it's parent class implements. It's very handy if you are doing any sort of inheritance at all.

Creating an object to act as a delegate - Objective C

My question is simple actually, how do I create an object to act as a delegate, instead of including the delegate methods in my view?
For example, I have x functionality that requires delegate methods, and they're currently setup to use self as the delegate. I'd like to put those methods in their own object so that the delegate methods can be called and do stuff if the view has ended.
What's the best way?
for example, NSXMLParser delegate methods - they exist, the delegate is defined, but I dont want to call them as self in my view controller... what other option do I have?
You can specify another custom class to handle the delegate methods, if you wish. Simply create a class, call it MyXMLParserDelegate or something similar. Then, all you have to do is tell your NSXMLParser object that it should use an instance of your class as its delegate.
If you are using Interface Builder, add a new object to the XIB file, set its class to MyXMLParserDelegate, and then drag a connection from your NSXMLParser object's delegate selector to the new object.
If you are doing it programmatically, the basic operation looks like this:
MyXMLParserDelegate * myDelegate = [[MyXMLParserDelegate alloc] init];
[someXMLParser setDelegate:myDelegate];
Keep in mind, however, that delegates are not retained, so in order to do this without leaking memory, you should add an ivar of type MyXMLParserDelegate to your viewController class, and then do the following:
// in your #interface block:
{
...
MyXMLParserDelegate * myDelegate;
}
// in your init method:
myDelegate = [[MyXMLParserDelegate alloc] init];
// in your awakeFromNib method (or anywhere else it seems appropriate):
[someXMLParser setDelegate:myDelegate];
// in your dealloc method:
[myDelegate release];
Check out this answer, I think it covers what you need: How to use custom delegates in Objective-C