Best Way to Add Object Via Other Class? - iphone

In my iPhone app, I have a custom UIViewController class setup which adds some UIImageViews and things.
How can I access this class via my main UIViewController and, for example, call a method from that outside class and have it add those UIImageViews to the view?

Make property and public methods, after that you can call them in target viewcontroller.
p.s. But I never used this approach because it breaks controller's logic. Instead of I usually use some managers which has all required methods. (DBManager, NetworkManager, etc)

Related

Change UITextField.text from an external class swift

I'm trying to simplify my life by dividing chunk of related code into different classes.
But I encounter a problem: changing the ViewController property (like UITextField.text, etc.) from those classes.
I have tried creating protocol, but that was really messy so I want to find a new way to do this.
So, is there a simple way to change ViewController property directly from external classes? I'm using Swift 3 btw.
Thank y'all for helping.
If the object from where you want to change your label is a child object owned or presented by the controller, than you need to use delegation to comunicate back with the controller. You declare a protocol in that object from where you want to change the controller's label, and declare a weak property of that protocol type called delegate. When the controller creates that object you will set that object's delegate property to the controller itself (If that object is ownee by the controller, declaring the property weak you will not create a retain cycle). In the controller you implement the method declared in the protocol and in the implementation you can change the label.
The other case when the controller is owned or presented by the object from where you wanna change the label, in yhis case you just need a public method to do that.

What "UIViewController" class really does?

I was working in the ViewController.swift and noticed that when I open it, it declares a UIViewController class. I was wondering if that is a new class or an already declared class that we can add code to.
I was wondering if that is a new class or an already declared class that we can add code to.
UIViewController is a class defined in the UIKit framework. It's the class that's used as the base class for all other view controllers in iOS applications, so it provides the behaviors that are expected of any view controller as well as a number of methods that are meant to be overridden. The usual way to "add code" is to create your own subclass of UIViewController, which is exactly what you're doing in the ViewController.swift file.
You're going to run across many classes that have the UI prefix, and you can look up any of them in the documentation that comes with Xcode.
UIViewController is not the class that you can add code to. But ViewController is default class that has UIViewController as a parent.
ViewController class is already connected with storyboard view controller when you create your project by default. So you can add some code to ViewController class and connect your UI controls (buttons, labels and so on) with you ViewController class and add some functionality to it.
So yes, you can add some code to it and see what happens when you build and run the simulator.
Its a class that manages view controller functions.
For example, "YourClass:UIViewController" means that You extend from UIViewControllerClass, so You are able to use all the functions from mother class, but also add new in "YourClass".

iphone development: is it possible to extend more than one viewControllers?

in my app I want to use googleAnalytics. To use it I have to extend GAITrackedViewController but the problem is I already extend GLKViewController because my view has an openGL application. So is it possible to extend the properties of both view controllers?
For a similar case, I've simply created a subclass of UIViewController (GLKViewController in your case). That subclass handles the tracking of the view. All "specific" ViewControllers extend that custom UIViewController, instead of the default one.
Then you could, for instance, track the view manually:
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
[tracker sendView: NSStringFromClass(self.class)];
No, there isn't multiple inheritance in Objective-C. You have write a subclass of GAITrackedViewController and a subclass of GLKViewController separately, and write a controller class that has an instance of these two classes, coordinating them.
It's not possible, objective-C doesn't support multiple inheritance. You should take a look to this question: Objective-C multiple inheritance
it is very bad to extend 2 classes even on languages that allow it,
because you can get 2 ways to your "super" and this is a way to many bugs

Iphone SDK selector or abstract class

I am developing a special type of view controller for an iPhone component library.
I have got the who view controller working well, but I need to change it so that it works in one of two ways:
Either it is an abstract class which you must subclass and provider the implementation for a specific method which the controller will call whenever it needs its data.
Or it needs to be a useable class which has a property which is a selector... when you set the selector it specifies the method which should be called to collect the data.
I would like to know how I can implement either of these and which you would recommend
The standard way of doing this in Objective-C and the iPhone is through delegation.
Normally you provide a property in your view controller called delegate that is typed for a particular protocol you create. Then who ever is using your view controller will set the delegate property with their delegate for your view controller. You can then call the methods in your protocol on their delegate.
See the answer at this SO question for a full example.
Also read the Cocoa Fundamentals Guide for information on what delegates are and why they are used in Cocoa. There is also an example there of how to create delegates for your own custom classes.

tabBar viewControllers in IB: send custom init?

My tabBarController-based app has several tabs. Each has a custom viewController class, the only difference being the way the instance is initialized. Is there a way to make interface builder send the different tabs custom init parameters?
Currently I'm doing the initialisation in viewWillAppear, but for a bunch of reasons it would make sense to do it in IB instead of in the code.
Any suggestions?
thanks,
Kelso
Interface Builder creates an archive of objects that is unarchived when you program executes. You can't really tell IB to call particular methods.
If you need to initialize before viewWillAppear: is called, you can do so in awakeFromNib, which is guaranteed to be called after all objects have been loaded and all outlets have been connected to their targets.
If you want to do initialization even earlier, you can do so by overriding initWithCoder: (see the NSCoding protocol for documentation). I don't know if it is documented anywhere, but that is the designated initialized for objects being decoded from an archive.
In all of the above, you won't be able to receive parameters, but in the code you should be able to access whatever you need with some judicious use of global variables. You can also use [[UIApplication sharedApplication] delegate] to get access to your application delegate object.
I don't think there's any way to change what methods are called by the IB runtime when your nib is loaded. If you described what you were trying to accomplish (i.e. why doing the setup in viewDidAppear doesn't work for you), you might get a suggestion of a better way to handle your initialization.