Use same UIWebview in 2 viewcontrollers as a subview - ios

I need to use same UIWebview instance in the 2 viewcontrollers as a subview.
But when i add the webview to the second view controller, its removing from the first view controller.
Both the view controllers will be visible at the same time. And I need to retain the same webview in the 2 view controllers.
I have tried of Archiving and unarchiving option. but the loaded details in the webview was not displayed when doing in this way.
Please suggest for any options.

You are just creating a new pointer to the same UIView object. Since UIViews do not respond to copy, you will need to actually allocate and init a new UIView and customize it to match the first one
A view can only be contained in a single parent view's hierarchy. When you add it to a new one, it is removed from the previous one.
So, You can't use same Web view in both View Controllers. You have to create another one. You can't add same instance multiple time.
You can achieve this by following pattern:
Create a common class where you can create UIWebview and load your URL and then add it to your controller. So you don't need to write url for every UIWebView.

Related

Is it possible to add the same view to two different view controllers?

I get the following error message:
A view can only be associated with at most one view controller at a
time!
Is it possible to add the same view to two different view controllers?
I want to add one instance of a Google Map to a Tab View Controller.
I'm using swift4 with the storyboards.
If it were a typical view then you should create an XIB and add a new instance of it to each view.
Since you're using GMSMapView from the Google Maps SDK, and you want to have the same instance across multiple tabs the approach will be different.
You can either:
New Instance, Same Parameters:
Create a new instance in each tab with the parameters set to be the same as the other tabs.
Move Around the Single Instance:
Create an instance of GMSMapView. Store it in a shared property. Use addSubview() to move it around. Basically use addSubview() in the new tab to remove it from the old one and add it to the new one.
A UIView can only be associated with one other view, because it can only have one parent view, which is set as the superView property. Also things like layout are set on this view and are relative to it's superview, so it makes no sense to use the same view in multiple Controllers.
The best solution would be to subclass UIView and then add new instances of your custom class to every ViewController

View as SubView VS ChildViewController

Can any one explain when should we add a UIViewController as ChildViewController?
What is the benefits of adding it as ChildViewController instead of subView?
Please help me to understand the purpose of ChildViewController.
When you add a view controller as child view controller, the parent view controller will hold a strong pointer to the child view controller so it doesn't get released instantly. This does not automatically add child's view to parent's view. So you will have to call them both.
I only used it when I needed to create multiple view controllers to be inserted in another view controller and didn't need to directly access it.
its all about UI and code management if you are using subview to achieve what you want to implement inside your app you need to code for your view inside same viewcontrollers class but something interesting i found by creating childviewcontrollers.
empowered to work on a seprate viewcontroller will invoked along with its parent viewcontroller along with its seprate class.
infinite controllers that will be updated tapping a button.
Creation of childViewControllers can be achived by implementing containerView.
or you must have a look of this link hope its helpful to understand.

IOS: What is the correct way to change a view from the class of a different view?

I have a UIImageView subclass (in swift) set up so I can access touchesBegan/touchesMoved/touchesEnded.
When one of these methods is called, I need to change a property of a different, loaded view.
It seems to me that I will now need to access the active view controller in order to set the properties of this other view. Is there are better way to go about this (such as event methods called in the view controller)?
Note that I'm new to iOS and I am not extremely familiar with the event system yet, as most information I've found is written in Objective C and not in Swift. (Don't worry, I'm looking through Apple's Documentation.)
Also, no, I can't change the UIImageView to a UIButton. Even if I changed to a UIButton, I need access to the individual touchesBegan, etc. methods and the same problem would persist.
If I'm understanding you correctly, you need to reference the UIViewController from the UIImageVIew to push/present a new UIViewController. You have a few options:
Fire an NSNotification from the image view and have an event listener on the view controller.
Create a delegate on the image view that fires a selector on the view controller.
Hold a reference to the view controller on the image view, and push a new view controller from the image view with that reference.
Modally present a new view controller on the application window's rootViewController.

Effect of setting self.view = nil

I have a viewcontroller with default view associated with it.
If I want to discard the view only not viewcontroller, is it ok to set self.view = nil? So that view gets discarded and new view gets allocated with new values.
I want to fill the view with new UI, based on a condition I did that in viewdidLoad. So If I set self.view = nil, and try to access the view from anywhere, it will again create the view. So the new properties will be set.
Is it a right approach to do it. Or I have to write a separate method to refresh view with new properties.
Please provide me the correct approach to do it.
I don't think there is a correct approach, but here is some advice.
Don't interrupt the view lifecycle (make sure -viewWillAppear:, -viewDidAppear:, -viewWillDisappear:, -viewDidDisappear: all get called).
Consider multiple view controllers contained within a custom container view controller instead of swapping views. It will keep each view controller focused on a specific task.
Consider having an empty self.view and use -addSubview:/-removeFromSuperview to swap the content. That way you will not need to reset self.view.
Don't remove the default view which comes with the uiviewcontroller. If you want to show the custom uiview based on conditions then I would suggest, create your custom uiview and then initialize it and add it on your default view for respective conditions.

iOS VIewController for UIScrollViews content

I have a UIScrollView in my app and I am adding some custom views from xib to it so you can horizontally scroll (tabbing) in ScrollView to change which one is shown. For now this works but I have a problem with connecting views to controllers.
I don't know how to choose structure of ViewControllers (how many controllers should I use, use nested controllers,...).
I have a rootView and its controller. In this rootView there is a ScrollView and this ScrollView contains some custom views (subviews) loaded from xib (using loadNibNamed method).
My question is should I use the same ViewController as for rootView also for these subviews in ScrollView? Problem is that the ViewControllers view property is already bind to the rootView (super view in rootView) so when I bind this view property also to subviews an error is occurred. Also if I create new controller for these subviews an error is occurred as well.
When I am loading subviews to the ScrollView with loadNibNamed method in ViewController of rootView, owner of these subviews is ViewController (owner argument of loadNibNamed method is set to self).
Can you tell me please, how should I solve this? What controller should I use for subviews, should I create new one or should I use existing one. Or should I use some nested controller? I am newbie in iOS development so I have a chaos in using ViewControllers right now...
If there isn't much code that is relative to controlling the sub views you could use just the root view controller. i.e A single controller for a single scene would be a good MVC approach.
If you are using it this way , don't change the view property of view controller as this messes it up for the root view - controller setup. If you just need a reference to this views you already have it with the return value of loadNibNamed. Also if you are setting the owner to self then create additional instance variable to hold the sub views(and not the view property) so that you can specify the owner from the xib itself and connect the references appropriately.
However if you have substantial business logic to be written regarding the sub views then its fine to create separate view-controllers(a single class would be fine if all the subviews behave the more or less same way if you are getting what i mean) for it. In the xib for the subviews, you can specify this class as the owner and when using loadNibNamed: you should create an object of the subviewcontroller class and specify this as the owner. This way you can modularize the whole thing.

Resources