What is the difference between using remove from sub view vs ishidden? I need a button to be inaccessible by a user. Which is better and more secure to make sure a user does not have access to that button? What is the difference between these two methods and is there a better method for doing this other than these two mentioned here?
The simple equation is "If you don't need the View/SubView in future, just do removeFromSuperView otherwise hide and show the View/SubView."
Which one is better? Depends on your requirement. In most cases, it is better to hide a View, If you need to show again when some event occurs.
Here you go the differences:
isHidden:
A hidden view disappears from its window and does not receive input events. It remains in its superview’s list of subviews, however, and participates in autoresizing as usual. Hiding a view with subviews has the effect of hiding those subviews and any view descendants they might have. This effect is implicit and does not alter the hidden state of the receiver’s descendants. Hiding the view that is the window’s current first responder causes the view’s next valid key view to become the new first responder.
removeFromSuperView:
Unlinks the view from its superview and its window, and removes it from the responder chain. If the view’s superview is not nil, the superview releases the view. Calling this method removes any constraints that refer to the view you are removing, or that refer to any view in the subtree of the view you are removing.
isHidden will just hide the button and can be shown whenever necessary. If you use remove from subView then you are essentially removing it from memory. In this case you'll have to instantiate or create a new button when you want to show that button. In order to make it inaccessible, you can also set isEnabled property to false. In this way button will be shown but will not be usable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With