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

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.

Related

Using struct in Objective c to bind same type of objects

I am new in IOS. I want to use two or three dispatch queue in my class. my class has too many properties and ivars, if i put everything in interface its looks messy. So what i thought to use struct So that same kind of object we can bind For example for dispatch queue
#interface MNHMapViewController ()
struct DispatchQueues{
dispatch_queue_t layeringDataQueue;
dispatch_queue_t branchDataQueue;
};
--
--
--
#end
I want to know is it good way to program in Objective C? If no then what i should do so that its not looks messy.
Thanks In Advance.
In iOS 6 dispatch objects are managed by ARC, so your code as shown is going to get even messier. A more elegant way to handle this is to create "helper" objects, NSObject subclasses. Each can hold a logical grouping of properties, and hopefully you can refactor you big class and move relevant functionality to them. You can even have multiple types of helpers. In the end, you'd like your primary class to clearly show what it's doing operationally, and put lower level details into the helpers.

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

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

#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.

Is it possible to declare a chain of super classes in interface declaration?

This may be a silly question but I haven't found any information on it.
Let's say several of the classes in my program derive from 'MySubView' which is derived from another class, UIViewController.
I would declare it like this:
#interface NewViewController : MySubView {
// code ...
}
#end
In the future the client wants a change, and desires another view with a table. So I would need to make another class, called MySubTableView, that is a UITableViewController subclassed from MySubView.
I was thinking this would be easier if I could do something like this:
#interface NewViewController : UITableViewController : MySubView {
// code ...
}
#end
But this doesn't work.
Is there a way to do this with Xcode, or do I have to specifically make the class itself?
EDIT:
I'm not looking for multiple inheritance. A straight inheritance hierarchy would follow:
NewViewController
UITableviewController
MySubView
UIViewController
No, Objective-C doesn't support declaring those kind of (vertical) inheritance chains. You can only specify the direct super class.
Even if it was possible, there would be problems like calling the correct initializers as they won't be called automatically. Consider a hierarchy like A : B : C - now you can initialize B using e.g. [super init] in As initializer, but how would B know what initializer you want it to call for C?
Objective-C doesn't support multiple inheritance... But Objective-C programmers rarely miss it, because you can accomplish many of the same tasks using Categories instead. Read up on Objective-C Categories.

What's the fastest and easiest way to create a new managed object?

When I have a subclass of NSManagedObject, I could do this:
Friend *newFriend = (Friend*)[[NSManagedObject alloc] initWithEntity:#"Friend"
insertIntoManagedObjectContext:moc];
But since I have a subclass, couldn't I simply instantiate it another way so that I don't have to tell it what Entity it is? (I mean: Is there already an predefined initializer for this, or must I write my own?)
Besides initWithEntity:insertIntoManagedObjectContext: the only two methods you can use to customize object initialization are awakeFromInsert and awakeFromFetch. See the full discussion related to Core Data object initialization here.