Push to a UIViewController from inside of NSObject's subclass - ios

I am a newbie in Objective-C. I stucked into a very big problem.
I have a ModelClass (named MyModel) for data management which is a subclass of NSObject...
And to access the data the app needs authentication using a viewcontroller (say X) I don't have access to its properties (Actually it is a framework's viewcontroller)...
I am accessing that model class from a viewController (named MyViewController)..and I want the retrieval along with authentication of data by calling one method of that model class...but, to present the uncontrolable viewcontroller X for authentication; I must use pushToViewController: method of UINavigationController SDK class.
And I found that subclasses of NSObject class do not respond to navigationController.
But I really need to do this. Any kind of effective help is appreciated and thanks in advance.

I've found a way to do this. I am giving the steps-
1> Create an UIViewController property (say obj) in MyModel.
2> Assign that obj to self(MyViewController's self object) just after creating the object of the model class(MyModel).
3> Put the following line in the model class from where to push to the target viewcontroller (for mine X)-
[self.obj pushViewcontroller:targetViewController animated:YES];
That's it. And thanks all.

Related

How to use showViewController?

I'm new to Xcode.I'm trying to figure out how to use showViewController.My question is how to call the UIViewController that to display ? via name or a identifier? And where can I find them ? In addition, the document says The default implementation of this method calls the targetViewControllerForAction:sender: method to locate an object in the view controller hierarchy that overrides this method,what does it mean? Should I call targetViewControllerForAction:sender:fist when using showViewController?
showViewController behavior basically depends on context you're app is in.
But calling it is really simple. All you need is reference to your UIViewController object(or it's subclass).
You can do it in couple of different ways:
If you're using Storyboards, give your storyboard view controller identifier, and use API call: UIStoryboard(name, bundle).instantiateViewControllerWithIdentifier(identifier). This gives you UIViewController that you should pass as an argument to showViewController. You could find information about API here: https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIStoryboard_Class/index.html#//apple_ref/doc/uid/TP40010909-CH1-SW6
Also, you could get your UIVIewController instance directly constructing it using proper initializer.
So: not worry about this targetViewController method, just get your object (initialize it via constructor or get it from storyboard), and pass it to show method.
In case you have further questions - feel free to ask.

Put Class results in a second viewController

I have an UIViewController in wich I imported a class:
#import "differenza.h"
and then I created an instance of that class when a button is pressed:
- (IBAction)ok:(id)sender {
differenza *classeDifferenza;
classeDifferenza = [[differenza alloc] init];
[classeDifferenza metodo];
}
As you can see, I also called a method.... now I don't have enough space in that UIViewController for the results of that method, so I need a new ViewController to show all the results... but I don't know how to recall results from that new ViewController, as I can not use the
classeDifferenza.variabile
way ....
This is something I did not really get already. In the past I used (thanks to your help: Setting up class instances in a multi view app (Objective C)) a "common class" where to store all data and all methods... but I don't think It's always the proper way... I feel like I am abusing that solution... or It's the right way to do what I described?
Thanks!
You can pass data with prepareForSegue check this topic
How to pass prepareForSegue: an object
You can send variable results like that

Why a delegate work also without defining it in the interface file?

I have a simple question to ask.
I have a ViewController that use a UITableView to show a list of things. The UITableView delegates are the this ViewController (using dataSource = self, etc..)
In all tutorials i know that i need to define the delegate class in the interface using:
#interface Class : SuperClass <ClassNameDelegate>
But all the code works good also without declaring this and implementing only the methods. For example:
#interface Class: SuperClass
Is a bug? I need to declare it anyways?
Thank you.
Mauro
In common case we use performSelector to call delegate methods like this:
[(NSObject *)_delegate performSelector:#selector(storeFeedbackViewControllerWasDismissed)];
and everything works because this delegate have got this method.
But it's better to point that class conforms protocol because XCode can warn you to implement required delegate methods that you can miss.
There is three moments:
Yours second class will return NO for call conformsToProtocol:#protocol(ClassNameDelegate)
If ClassNameDelegate have required methods, you will not get compilation second class error if it's not implement requited methods.
You will get warning if try assign object of second class to id<ClassNameDelegate>

iOS: class instances connection and architecture with multiple UIViewControllers

I have a theoretical-practical question. I can't understand how I must do. I have a class let's call them DataManager that manage all plist writing-reading things and I need to get access to plist (i.e. work with that DataManager class) from different UIViewControllers.
I also have one class, I call it ModelManager, that is work with all kind of "utilities classes", include my DataManager. ModelManager works only with one complex UIViewController right now, let's call it MainUIViewController for clearness. And for now, I thought that all calls from UIViewControllers will be comes to ModelManager and from it to end-call classes. But now I'm confused.
Here is an illustration of my architecture:
I'm see different approaches and don't know how to decide and if there is some rules or guides for that. So, here is my choices:
1) I add some interface to ModelManager and from my another UIViewController (not a MainUIViewController) allocate and initialise it.
2) I add some interface to ModelManager and create a property with reference to ModelManager in another UIViewController and when segues performs set this property from MainUIViewController.
3) Work with DataManager itself and allocate and initialise it from another UIViewController
4) Work with DataManager itself and create a property with reference to DataManager in another UIViewController and when segues performs set this property from MainUIViewController.
Which approach is correct?
I know that this is some kind of depends from developer which approach to choose, but I never read and didn't find any tutorial or guide of how to develop multi-class architecture.
Ask me about any circumstance that you want to know.
You can use a singleton or you can instantiate one instance of the class in your app delegate and pass it around to all your view controllers via #propertys on each controller. There's no right answer, it's mostly a matter of preference. I prefer to make my ModelManager/DataManager type classes singletons, but a lot of people are rabidly opposed to singletons. However, if you work with Cocoa for any length of time you'll find that it's full of them (NSUserDefaults, NSFileManager, UIDevice, probably some others I'm forgetting).
Here's a good example on how to create singletons: http://www.galloway.me.uk/tutorials/singleton-classes/
BTW: Once you have your singleton, learn how to use KVO to make your view controllers respond to changes in the model. It's pretty fantastic once you get the hang of it. http://nshipster.com/key-value-observing/

Recursive Delegation in iOS—How to Implement?

I have a DataController for my ViewController, which handles loading data from the internet. I set the DataController as the data source for my ViewController, and it works fine. But now I want to display a progress bar as the data loads, so I was thinking of having the ViewController be a delegate of the DataController, and be notified of when loading starts, continues, and ends. Obviously, this recursive delegation leads to a Bad Access while the stack is still showing me assembly. How should I implement this situation?
I've never used this exact dataController pattern you're mentioning, but my common implementation for something along these lines is:
Declare a NSArray or NSMutableArray as a member your UIViewController subclass
Create a class that using ASIHTTP or NSURL to load data from the web, and set that class as the delegate for the ASIHTTP or NSURL
Create a protocol in that data access class that your UIViewController adheres to
Create an instance of that class in your UIViewController, and start the fetching process (asynch)
When the requests complete (or are giving progress notice) to your data access class, send that information via delegate to your UIViewController
When the request fully completes return the list of items to a delegate method and store that data locally in the array from step 1.
There are various ways to do this depending on your circumstances, but I just wanted to give you an idea.
Never mind; turns out the issue was due to a premature release. I'm dealing with objects that should never be dealloced (data source and root view controller) and I set up the delegation after both are created, so there's really no issue here.

Resources