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.
Related
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)
I have a UIViewController that contains a UITabBarController, which contains a UIViewController, e.g.:
UIViewController1 -> UITabBarController -> UIViewController2
I want to hook up a button in UIViewController2 that will call a method in UIViewController1, but how do I access UIViewController1 from UIViewController2? I know that calling self.parentViewController from UIViewController2 gets me to the UITabBarController, but where do I go from there?
I tried self.parentViewController.parentViewController from UIViewController2, but got null.
Any suggestions?
The self.parent.parent technique often won't work because there may be Apple implementation layers in-between your controllers. Best to create pointers yourself in your own implementation by subclassing and creating instance variables, and setting them at the point of creation.
There are several ways to do this depending on what your actually trying to solve....for example if its a utility like class method then you should put it in a different file and import it into UIViewController2. If its an instance method then you obviously have an instance of UIViewController1 then you need to declare an instance variable of type UIViewController1 that holds a reference to the view controller. Then you can call methods on that instance....what is it that you are trying to accomplish?
Just add the first one as a property and assign it when you instantiate:
viewController1.otherView = viewController2;
A beginner's question:
If, in your nib, you have the File's Owner linked to the ViewController class, and you also have a NSObject-derived class, how do you communicate between the ViewController class and the NSObject class within code?
For instance, suppose ScientificCalculatorView.xib looks like this
File's Owner (class: ScientificCalculatorViewController)
FirstResponder
View
Calculator (an object that has been linked to the Calculator class)
Obviously, I'd want Calculator to be reusable, so it could be used with a NormalCalculatorViewController or something like that. So that UI and the calculator code are separate. Does Calculator even need to be in the nib?
It's a beginners question, but I'm just trying to get my head around it.
There are two ways (at least) to handle this:
Set up an outlet in your ScientificCalculatorViewController defined:
IBOutlet Calculator *calculator; or something like that. In IB connect that outlet to your Calculator object. You will then be able to reference it in your ScientificCalculatorViewController.
In the - (void) viewDidLoad {} method, programmatically allocate and initialise a Calculator and set it to a property in your ScientificCalculatorViewController. In this case, you can remove the object from your NIB.
I have a view controller which gets an NSObject conforming to a protocol and then displays a view with the object's properties.
The question : How can my controller know that a property of this object has been modified and then refresh the view ?
Thanks a lot
Thierry
There are three ways of doing this:
Have the object call a method in the controller in response to an event e.g. a user clicking the button. This is usually done using an IBAction.
Set the controller to be the delegate of object e.g. a UIWebView sends a message to its delegate when it finishes loading a page.
Use a notification. The object generates the notification and then one or more objects (including the controller) registers to listen for the notification. This is usually not used with interface elements although it can be.
I can't tell you more without more detail about the specifics of your project.
Your viewcontroller should conform to your .
In your model, all your set methods should trigger appropriate functions you define in your modelchangedprotocol.
This OO design pattern is also known as "Observer" design pattern.
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.