Accessing a ViewController's subview that's added on the storyboard - ios

Please see the image below for a storyboard visual. I have a ViewController called StudioViewController (It's labeled ViewController on the storyboard). And I have a UIScrollView that's called CanvasViewController (the thing to the right of the view controller that actually sits on top of the View Controller). I want to be able to access the CanvasViewController in the StudioViewController class, how do I do that? Because I created it in the storyboard here, I dont' seem to have a variable that allows me to access the CanvasViewController... Should I create it in code instead, or is there some obvious way to access it that I'm completely missing?
Thanks
******** UPDATE *********
The question has been answered in the comments below by rdelmar:

You could make a shared variable in the app delegate. This is not very secure but I have resulted to this when I can't access a child or parent due to page transitions.

Related

Update UI from another controller

I would like to update my UILabel on click the button of ContainerView contains table ViewController. When I try to do this UILabel's outlets reference shows nil value exception. I am using Swift3 with Xcode8
Most probably the problem you are seeing is due to the fact that the view that owns this label on another view controller is still not loaded.
This happens often, basically because views owned by a view controller are instantiated in a lazy manner, this means that they are loaded only when required.
To fix that before setting the value on the label, just preload the view by doing something like.
_ = another_viewcontroller_instance.view
In this way you are forcing the destination view controller to load the view and creating all the necessary connection on the xib.
Even if this fix works, this is not a good way to deal with this kind of pattern (sending info from a VC to another), but since you didn't gave us any further detail this is the only solution I have.

How can I link an outlet from a view controller to an other?

I am working on an app that uses parse so I used the "starter project" as a base and worked from there.
The issue I am facing is that the ViewController is controlling the login screen a well as others such as the tableView and mapView witch I added later.
As this is the case if it would be possible I would link the map outlet by simply dragging from the code to the map but obviously this is not possible, How could I solve this problem (I understand I may be looking at the problem the wrong way but any help would be appreciated)
here is the code with the map outlet
here is what the layout looks like
The MVC model, Model-View-Controller model, isn't intended to have an action in one view touch the controller of another view. In InterfaceBuilder, you should only ever be able to attach actions to the controller for that specific view.
In general, if you set the file owner to ViewController, then you can only link IBoutlets to that view controller not make to another one.
your map is available in your MapViewController not ViewController, so you need to give the reference/IBoutlet of map need to assign the MapViewController, if you want to implement in ViewController, you need to create new one map
No you have to create different file for each controller.
you cant add outlet of all in one controller

Present UIViewController from another UIViewController

I have a UIViewController named ViewControllerHome and when the user touches an image on the screen I would like to display a second view which is a Membership Card. I am doing the following from the touch of the image:
membershipCardViewController = [[MembershipCardViewController alloc] initWithNibName:#"MembershipCard" bundle:nil];
[self presentViewController:membershipCardViewController animated:YES completion:nil];
When the code executes an exception is thrown on the presentViewController line.
I have an .xib with a ViewController that contains a view and a UIImageView of the Membership Card. I set the class of the ViewController to my MembershipViewController.
Once that shows up I will dismiss it on a touch.
Can anyone tell me what I am missing? I thought I had all the steps correct to present the view controller.
Thanks for the help.
In the MembershipCardViewController's nib file, its view (what ever view it is controlling) is connected to the view controllers view outlet.
To do this control drag from files owner to the view you want to connect it to (the grey view in this case)
And you should get this:
Files owner should point to your MembershipCardViewController. Every view controller has a pointer to a view. I'm going to guess that you added some custom view after deleting the stock one. Control drag from files owner to that view to make the outlet. (If this outlet returns nil, an exception will be thrown).
To be safe, make sure file's owner (in the nib) is pointing to MembershipCardViewController
(This probably isn't the problem but it sounds like you may have started with an empty nib).
To do this, click on files owner, and select the identity inspector on the right. Make sure the class says MembershipCardViewController
I answered another question before about this Am I right in saying initWithNibName:bundle is used to manually load nib files and that initWithCoder would be used as an alternative?
This explains what is actually going on.
The purpose of your XIB is to archive the view of the controller. Having the controller class set is only part of the required information, you also need to connect any IBOutlet relationships between the controller and the views.
As standard any subclass of UIViewController provides an outlet called view. You need to ensure that it's connected. Otherwise when you load the XIB the view doesn't get set and you get an exception.
There are a number of ways to make the connection. Check this.
See also loaded-nib-but-the-view-outlet-was-not-set-new-to-interfacebuilder.

Accessing another View Controller's IBOutlets Housed in a Container View

I have a Container View that holds 1 of 3 view controllers swapped out by 3 tabs (across the bottom).
Here's my Storyboard:
I'm taking this approach so that I can have custom tabs and a single Save button for all the fields in this big form.
How can I access the IBOutlets in those 3 child view controllers from inside my Entry Detail View Controller class?
Here's what I've done to try and access it in the parent.
//EntryFlightInfoViewController.h (Child 1)
#property (strong, nonatomic) IBOutlet UITextField *aircraftIdTextField;
Then in my parent class (Entry Detail View Controller) I can't access the property:
//EntryDetailViewController.m (Parent)
#import "PPEntryFlightInfoViewController.h"
- (IBAction)buttonSave:(id)sender {
NSLog(#"save: %#", _aircraftIdTextField); //(!) Error: Use of undeclared identifier '_aircraftIdTextField'
}
How do I access another view controller's IBOutlets when they are in a Container View? Hopefully I'm on the right track. Forgive me as I'm still quite new to Objective-C. :)
View controllers in a container view are children of the controller with the container view. So, you can access the current child view controller with self.childViewControllers[0], and the outlet with [self.childViewControllers[0] aircraftIdTextField]
The short answer is you can't unless you write your own communication layer. You can get at the different views via the childViewControllers (mentioned above) as well as getting your own custom pointers in your prepareForSegue method call as the view is loaded into the container view. (see this link for more info on this) This will work great if you are forcing the user to visit each page. If not then the same defaults you'd load there on the first viewDidLoad can likely be saved without checking the specific view controller.
Apple's answer is to never have a save button. If you edit it, you meant it, and it's saved immediately. =)
That said, were I you, I'd have an object that all of the views access to load/unload their data for storage whenever you show/hide the different views, possibly in viewDidLoad/viewDidDisappear. This way you always have a "known good" object ready for saving, and you won't have to directly access those view controllers.
Hope that helps some.
Clifton, whenever you can, you should avoid having VCs know about each others' controls. You could just have the save button send out a message, and have the three subordinate VCs observe for it. When they get the message, they save their info. That way, the master VC doesn't have to know about controls inside its subordinates.
You need a reference to the current tab view controller, then you ask it for the outlet:
self.entryFlightInfoViewController.aircraftIdTextField
By trying to use _aircraftIdTextField your trying to directly access an instance variable on the container view controller class. Obviously if doesn't have a variable with that name so you get the compile error.

Refactoring a UITableViewController subclass to a UIViewController subclass

In my app, I wanted to show a table in the "full screen," so I created a subclass of UITableViewController. When I want to show the table, I just instantiate it and use pushViewController:animated:. Now I've decided that I actually want to show it as a modal, so I want to give it a navigation bar with a "Done" button. I believe this means that I now need to make this a UIViewController subclass instead of a UITableViewController subclass, since it will now be more than just a table. I'm not really sure if this is something that can be done, though; I've created an xib, but Xcode doesn't seem to want to let me use that xib to create IBOutlets in the .h file, so I don't think the two are "connected."
I ran into this problem once before and worked around it by just creating a new UIViewController subclass and xib and just copy-and-pasting all the stuff I needed from the UITableViewController subclass's files into the new subclass's files. I knew then as I know now that that was a hacky and unpleasant solution, though. Does anyone know how something like this can be done properly?
Also, to avoid this problem in the future, should I just never make top-level views like this in the future and just make sure everything I use pushViewController:animated: or presentModalViewController:animated: with is a UIViewController subclass? Or is it safe to do what I've been doing?
Several things you mention in your question indicate some misunderstandings. First, if you're pushing you table view controller, then it has to be embedded in a navigation controller, and thus will have a navigation bar -- you can add a done button to this bar, if that's all you're trying to change. No need to refactor your code.
If you want to use a UIViewController instead of a table view controller, you should be able to hook up your nib. Did you change the class of the files owner in the nib to your UIViewController class? After you do that, you should be able to hook up the outlets.
There's nothing "hacky" about copying and pasting code from a table view controller into a view controller. I do this all the time, rather than writing out all those files.
I'm not sure what you're asking in the last question. How you get your table view on the screen (pushing or presenting) has nothing to do with whether what you're showing is a table view controller or a view controller. You should use the one that best fits your needs.

Resources