How to use showViewController? - ios

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.

Related

Pass Data,Share global variable,Singleton?

I have a UITabbarController with three UINavigationControllers.And each navigation controllers has a UITableViewController as rootViewController. It's a typical design in many apps.Now I want to share a object between these three UITableViewController in their UINavigationControllers.I have the following ideas:
In UITabbarViewController, I can declare a property,maybe 'strong'.Each UITableViewController can access to the object useself.tabbarcontroller.object
Create a singleton that keeps this object. But it may cause singleton abuse.Some topic advise use dependency injection to avoid the abuse.But it seems it could not satisfy my requirement.
Make the object a global variable.I really do not want to take this method.
I prefer to use method 1. Any ideas about the data passing?
If you need to pass it to the ViewController just like an Android intent extra then i would go for method 1.
If you need to store it and access it multiple times from various classes then i would go for method 2 or 3, maybe 2 is more clear.
Hope this helps.

passing data between interfacecontrollers not working in apple watch

I was using this code to pass data between controllers.
InterfaceController.reloadRootControllersWithNames(["1","2","3"], contexts: ["adf","asd","asd"])
I called this code in willActivate() method, that you can see here.
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
InterfaceController.reloadRootControllersWithNames(["1","2","3"], contexts: ["adf","asd","asd"])
}
I have no idea about whats wrong with this code, in apple watch, the app is just refreshing again and again. Is there any other method to pass data between interface controllers while we use page based navigation.??
Please Find the below way of passing data in paged-based navigation in watch application (Watch os 2 and later).
Note :
An array of strings, each of which contains the name of an interface controller you want to display in the page-based interface. In your storyboard, the name of an interface controller is stored in the object’s Identifier property, which is located in the attributes inspector. The order of the strings in the array is used to set the order of the corresponding interface controllers. This parameter must not be nil or an empty array.
Objectiv C
[self presentControllerWithNames:#[#"FirstViewController",#"SecondViewController",#"ThirdViewController"] contexts:#[#"adf",#"asd",#"asd"]];
Swift
let controller = ["FirstViewController","SecondViewController","ThirdViewController"]
let pageContexts:[AnyObject]? = ["adf","asd","asd"]
presentControllerWithNames(controller, contexts: pageContexts)
If you call reloadRootControllersWithNames(NSArray, contexts: NSArray) onto the view controller, it will do as the function is titled: reload. Therefore, you have a view controller that refreshes all 3 of the items in the first Array you give, and then, since your current view controller calls willActivate() upon loading, it infinitely refreshes.
I don't have enough reputation to make this a comment, but my suggestion here is to find a method that you can call on an instance of the watch interface you want to in order to pass data.
What I mean is, if you have each interface as its own class, then you can make a method in the interface you want to get to that sets a property to whatever data you want to transfer before you present the interface.
I also use Objective-C more than Swift, but here's what my guess is as to what the Obj-C code would come to:
SecondWatchInterface *secondWatchInterface = [SecondWatchInterface new];
[SecondWatchInterface setSomeDataWithAStringValue:#"Foo"];
[self presentWatchInterface:SecondWatchInterface]
In Swift, I'm thinking this would equate to:
SecondWatchInterface *secondWatchInterface = SecondWatchInterface.new()
secondWatchInterface.setSomeDataWithAStringFile:"Foo"
presentWatchInterface(secondWatchInterface)
I'm unfamiliar with WatchKit myself; I typically use SpriteKit. I hope I helped identify what the problem was, at least!
To pass data between interface controllers you need to go for this method:
presentControllerWithName(“Second Watch Controller”, context: [“segue”:“pagebased”,“data”: "Data Passed Successfully”]);
Also, you can refer to this example: http://www.informit.com/articles/article.aspx?p=2272657&seqNum=3

No viewDidLoad method in PSListController

I have checked recent header files for PSListController , PSViewController and PSBaseView, but there is no method for acknowledgement of view creation, like iOS has viewDidLoad - Although there are many methods available for view Display. One I found is: -(id)initForContentSize:(CGSize)contentSize, but I don't want to use it as I don't think it would be appropriate to do so.
So which method should I choose in order to initialize my instance variables or do other stuff? Thanks.
Note: I am using rpetrich's Header Files.
Some of those headers are slightly older on the repository but looking at an iOS 7 version of PSViewController you will see that it is a subclass of UIViewController. UIViewController does contain viewDidLoad so PSListViewController "should" contain the same.
Source: http://www.developer.limneos.net/?framework=Preferences.framework&header=PSViewController.h

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/

Push to a UIViewController from inside of NSObject's subclass

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.

Resources