Accessing UIView methods - iphone

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;

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.

Subclasses of a ViewController with a nib

I have a ViewController that I've set up with outlet and a NIB.
This is my super class view controller.
I now want to create a subclass of this view controller as there is one method I would like to override.
I'm not sure how to go about doing this.
I've tried to do it but at the moment I've just got the functions in the superclass running in place of the actual class I want.
Any pointers?
EDIT
Sorry, I had done it correctly but had a typo in the overridden function.
If you want your super class functions not to run then you must override them. Just write the definition of the method you want to override and that will override it. The rest of the functions will be same as super class.

passing data back to a previously allocated UIViewController

I have a view controller that then has a button that passes to an option menu.
When options are set they need to be past back to the previously allocated viewcontroller.
How is this possible without 'alloc and init another' instance of the object?
You can achieve this by using a delegate protocol. First view controller should become the delegate of the second view controller and then you can call this delegate method in your first view controller once the selection is done.
You can lookup google for implementation of delegates in objective-c. Its pretty simple. Add a
#protocol <delegatename>
<declare delegate method>
#end
Create a member variable in the second view controller for assigning the delegate. And define the method in the class implementing the delegate.
When you init your option viewController, pass it a reference to its parent.
I.E.
[[OptionViewController alloc] initWith...: parent:];
Use a #property or a method or somesuch to call on the parent to pass the data back.
You could use a shared singleton?
http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html
Or save the parameters to nsuserdefaults and read them back in in your first viewcontroller
or some other temporary store such as your appdelegate
I've used all three of the above approaches before.

How to set the delegate to another ViewController?

I've recently started developing for the iPhone and so far I'm doing pretty good but there's this basic pattern I really don't seem to get.
Say, I have a TabBar with two views and a custom delegate protocol, thus my structure is the following:
AppDelegate.h/.m
myDelegateProtocol.h
FirstViewController.h/.m
SecondViewController.h/.m
MainView.xib
FirstView.xib
SecondView.xib
Now I want to achieve the following: I placed a button in the FirstView.xib and I'd like the IBAction which it invokes (inside FirstViewController ofc.) to send a message to the SecondViewController ([self.delegate tellSecondViewContrToSayHi]) and invoke another method which simply prints a log into the console saying "hi I'm here."
So far I know what I need to do in theory:
Specify the protocol.
Implement the protocol in the SecondViewController.
Create an id< myDelegateProtocol > delegate inside my FirstViewController,...AND last but not least:
Set the self.delegate = secondViewControllerObject.
Now, nr.4 is where the problem's at. How on earth do I link the delegate to the other viewController? I mean I'm not the one instantiating the views as the tabBar kinda does that for me,... any advise? Or am I just way too tired to notice a really stupid thing I did somewhere?
Theoretically the same question also applies to the target:action: thing,... I mean, how do I define the target?
Thanks a lot,
wasabi
You have the right idea, assuming that you want relatively tight coupling between these controllers via that delegate protocol.
Since neither controller knows about the other until that delegate property is set you need to have some object which has a reference to both of them wire up that relationship. In your case that's probably the application delegate which can create both controllers, set one as the delegate of the other, and pass both along to your tab bar controller.
What you might actually want is to have the app delegate give both controllers a reference to some shared model object. Your FirstViewController can update that model when you tap a button and your SecondViewController can observe changes to the model to update it's display (or just update its view when it appears based on the current model state). That way your controllers don't need to know anything about each other.

Passing info to a delegate from a UIActionSheet

My understanding about passing data back to a delegate completely revolves around making a new view controller and having it conform to a protocol.
I am trying to get a time input back from a UIDatePicker set with the UIDatePickerModeCountDownTimer mode and I am running into issues.
Inside of Main1.m I create a UIActionSheet *action, and set everything up so that it presents itself with a UIDatePicker on a click. Inside of Main.m I also say:
main.delegate = self;
If this were not a UIActionSheet, I would make a protocol reference inside the new ViewController and then have the new vc pass data to a method that Main has, but I can't do that with a UIActionSheet!!
What am I missing? I assume there is something inherently different about Action Sheets, ut I can't figure it out.
Thanks for your help!!!
EDIT:
Sorry! I was giving a generic name to my view controller. It isn't actually Main.m, its FirstViewController.h/m
I didn't realize that my generic reference was getting mixed up with the Main.m file that is completely different than a vc.
I don't exactly understand why you're putting your delegate assignment in Main.m. I assume that you're setting up your UIActionSheet in a ViewController, and launching it from there. In this case, your ViewController is your delegate object. So you need to make sure that your ViewController implements the UIActionSheetDelegate. ie:
#interface SomeController : UIViewController <UIActionSheetDelegate>
Then you simply implement the required methods of that delegate in your view controller class, and that should do it. If I'm missing something about how you're implementing this, then you need to provide more code examples to examine.