Effect of setting self.view = nil - ios

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.

Related

Check which view presented the current view

I am trying to check which view presented the current view as there are many views that could have brought me there.
Is there code that i can use to determine if it was a specific view? If it was the sepecific view that i want, i need to do something on viewDidLoad.
This is not something you should do. A view shouldn't care which view presented it. That is fragile and it doesn't scale well.
A proper approach would be for your view to have one or more properties that can be set by whatever will present the new view. Then the view can display itself accordingly.
I assume when you say "view" you really mean UIViewController. You will find that UIViewController has a presentingViewController property you can access to find the view controller that presented it. (If this is not the scenario you're working with, you need to provide more specific information.)
A better pattern may be to instead pass a boolean or enum to your view controller's init method that controls the behavior.

Use same UIWebview in 2 viewcontrollers as a subview

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.

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.

Set View Controller Subview Loaded from Storyboard

I have a custom view controller I load from a Storyboard. When I try to set one of its subviews nothing happens (remains generic white view). What I don't understand is if I try to set VC.view it works fine. Why is this? Everything seems to be initialized after I load from the Storyboard. Where would I set the VS's subview?
Yes, this slightly confusing behaviour is how it is 'supposed' to work. When a view controller is loaded, its view is not - at least not until it is actually needed. See this doc for further info. Only when the view controller is presented, will it then load the view. As you have found, this is tiresome, because you often want to set some of the the subviews' properties before it is presented (say in a prepareForSegue or prior to pushViewController: or presentViewController:).
There is a work around - based on what you have observed. If you directly access the view property, the view controller will immediately load the view and all its subviews. So, if you want to set subview properties, just "touch" the view itself:
NSLog(#"View tag is %i", viewController.view.tag);
and you should then be able to access the subviews.
Alternatively, you could pass the relevant data in (non-UI) properties of your view controller, and then set up the subviews using that data during viewDidLoad or viewWillAppear.

Can a view be added to multiple other views safely, if so what does removeFromSuperView: do in that case?

I am wondering what happens if a single instance of a UIView object gets added as a subview of multiple other views simultaneously.
If UIView:removeFromSubview: gets called then does it get removed from all superviews or just the currently displayed one?
For background:
I have a status-bar like view object that needs to be displayed within several different other views (each other view is managed by its own view controller).
[i.e. a) the user is in one view, b) something happens to make the status-bar-like view appear, c) the user switches to another view d)the status bar is still visible in the new view e) the status bar expires after a time and disappears from site. And so on]
Initially I implemented this by adding/removing it as required as a subview of the window, and this was managed by a singleton.
However due to some complications with some animations I have instead added it as a subview of each of the main view's for each of the view controllers.
Note that there are not multiple copies
When the view needs to be removed I am calling its removeFromSuperview:, and everything is all working perfectly.
However I am wondering what the situation is regarding the removal of the view, is it being fully removed or is there something else I need to do?
For example the view might get added to N view controller's views by calling addSubview as required (it will only get added to each view controller if that view controller actually launches)
However when it is being removed I am only calling removeFromSuperview: for the view of currently loaded view controller, not all view controllers it might have been added to.
Next time I navigate to one of these other view controllers it displays fine without the view being there, even though I didn't explicitly call removeFromSuperView.
As I said everything is working as it is, however at the back of my mind I feel there might be something missing?
Hope this was understandable.
You can only have it added to one view. Documentation is your friend!
(void)addSubview:(UIView *)view:
Views can have only one superview. If view already has a superview and that view is not the receiver, this method removes the previous superview before making the receiver its new superview.
From my point of view, having to add a same view to different parent views (and more important, from different view controllers) is an indication that something is wrong on the design...
However, if you really (really) need so, I had always thought that a view instance could have one and only one parent view... Moreover, you can access it by [myView superview] message, which gives you a UIView instance instead of an array... It may auto remove from its old parent before adding to a new superview?
About the design, what about creating it each time you need a new one and have a singleton to manage their status/logic?
Good luck with that!

Resources