Is there a better way to remove a last subview from view - objective-c

To remove last subview from a view, I use
[[[self.view subviews] lastObject] removeFromSuperview];
I'd use removeLastObject, but can't as subviews is readonly immutable array.
So I need to:
access all subviews
get last object
call method on it to remove it from superview
It works, but is there a better way? This seem to be a bit unnatural. I look for something like [self.view removeLastSubview], but unfortunately it doesn't exist.

To avoid Objective-C's message-sending syntax
Note: dont do that
UIView *lastView = self.view.subviews[self.view.subviews.count-1];
lastView.removeFromSuperview;
I said: dont do that
but seriously:
[[[self.view subviews] lastObject] removeFromSuperview];
is just fine. I'd consider the first snippet code-smell.

Make a category
#interface UIView (Exptened)
- (void)removeLastSubView;
#end
#implementation UIView(Exptened)
- (void)removeLastSubView
{
[[[self subviews] lastObject] removeFromSuperview];
}
#end

Related

Removing View from its superview getting a bad access error XCODE

since the release of the storyboard it has been a long time since I've used addSubview and removeFromSuperview. Obviously I have forgotten how to use them.
So on my UIViewControllerA I have this code for a button:
- (IBAction)buttonClickHandler:(id)sender {
dyf_FacebookViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"Facebook"];
[self.view addSubview:controller.view];
}
Which loads a UIView that is handled by UIViewControllerB. now this view can be called by many different ViewControllers to be a subview, which is why I have it remove itself from the superview.
So on UIViewControllerB I have this code:
- (IBAction)close:(id)sender {
[self.view removeFromSuperview];
}
Unfortunately that returns with a bad access error. Please help!
Thanks
Michael
EDIT:
The subview added (subview's viewcontroller) will be in control for removing itself as it is in control of the button on the subview. So I do not have access to the original controller.view variable as that is in ViewControllerA
You'll have to keep a reference to your dyf_FacebookViewController object, so you can do the following:
- (IBAction)close:(id)sender {
[controller.view removeFromSuperview];
}
What you're doing now is to remove your main view from its superview, which can have serious consequences, giving you a bad access.
if ([controller.view superView]) {
[controller.view removeFromSuperview];
}
Try this
- (IBAction)close:(id)sender {
for (UIView *subView in self.view.subviews) {
[subView removeFromSuperview];
}
}
If you want to remove the view you added, then keep a copy of the view controller...
// in this VC.m
#interface
#property (nonatomic, strong) dyf_FacebookViewController *dyfController;
#end
- (IBAction)buttonClickHandler:(id)sender {
self.dyfController = [self.storyboard instantiateViewControllerWithIdentifier:#"Facebook"];
[self.view addSubview:self.dyfController.view];
}
Later, to close the view...
[self.dyfController.view removeFromSuperview];

How do I Insert View in Superview's View? (iOS) [duplicate]

I have the line of code below... How would I modify it to insert the subview in the superview's view named 'foo'?
[[self superview] addSubview:copy3];
There are more than 1 ways of doing this -
1.have an IBOutlet reference in your code. This is created from xcode Interface builder. Once you have this then it fairly straight forward -
[iboutlet_foo_object addSubview:copy3];
2.tag the view that you are interested in. Again tagging a view can be done from xcode Interface builder or from code. After that -
Foo *fooObj = [[self superview] viewWithTag:tagInt];
[fooObj addSubview:copy3];
3.Finally you can iterate through all the views in your superview & see which one is of type Foo -
NSArray *subviews = [[self superview] subviews];
for(UIView *v in subviews)
{
if(v isKindOfClass:[Foo class])
{
[v addSubview:copy3];
break;
}
}
hope these methods help you...
First make myView into a property of the superview
Then use
[[[self superview] myView] addSubview:mySubview];
First tag the view myView with a unique number:
myView.tag = 0x1234;
Then you can find it using viewWithTag:
[[[self superview] viewWithTag:0x1234] addSubview:mySubview];
You might just actually add it to 'view' instead of 'superview'.
[[self view] addSubview:mySubview];
It works just fine for me, whenever I add subviews to my parent view.
Let me know if something went wrong.

How to Insert View in Superview's View? (iOS)

I have the line of code below... How would I modify it to insert the subview in the superview's view named 'foo'?
[[self superview] addSubview:copy3];
There are more than 1 ways of doing this -
1.have an IBOutlet reference in your code. This is created from xcode Interface builder. Once you have this then it fairly straight forward -
[iboutlet_foo_object addSubview:copy3];
2.tag the view that you are interested in. Again tagging a view can be done from xcode Interface builder or from code. After that -
Foo *fooObj = [[self superview] viewWithTag:tagInt];
[fooObj addSubview:copy3];
3.Finally you can iterate through all the views in your superview & see which one is of type Foo -
NSArray *subviews = [[self superview] subviews];
for(UIView *v in subviews)
{
if(v isKindOfClass:[Foo class])
{
[v addSubview:copy3];
break;
}
}
hope these methods help you...
First make myView into a property of the superview
Then use
[[[self superview] myView] addSubview:mySubview];
First tag the view myView with a unique number:
myView.tag = 0x1234;
Then you can find it using viewWithTag:
[[[self superview] viewWithTag:0x1234] addSubview:mySubview];
You might just actually add it to 'view' instead of 'superview'.
[[self view] addSubview:mySubview];
It works just fine for me, whenever I add subviews to my parent view.
Let me know if something went wrong.

Objective-C: How can I remove just one subview from UIWindow?

I want to remove just 1 subview not all.
And That subview is UIWebView.
You can call this on the UIWebView you want to remove:
[myWebView removeFromSuperview];
This method is available to all subclasses of UIView and NSView.
You can also try this if you do not declare an instance var in your header file.
for(UIView *view in window.subViews){
if([view isKindOfClass[UIWebView class]]){
[view removeFromSuperview];
}
}
Additionally, if you don't have a reference to it, just give a tag when you created it like this
myWebView.tag = 1234;
And remove it like this
[[myParentView viewWithTag:1234] removeFromSuperview]

Resize view of NSCollectionViewItem

How do I programatically set the size of a view of an NSCollectionViewItem?
I tried doing this in an NSCollectionView subclass:
#implementation CustomCollectionView
- (NSCollectionViewItem *)newItemForRepresentedObject:(id)object {
NSCollectionViewItem *newitem = [[self itemPrototype] copy];
[newitem setRepresentedObject:object];
NSView *itemview = [newitem view];
[itemView setFrame:NSMakeRect([itemView frame].origin.x, [itemView frame].origin.y, [itemView frame].size.width, 500)];
return newitem;
}
#end
However this code has no effect. I tried subclassing my NSView that I use for the NSCollectionViewItem, and adding setFrame: to the initWithCoder method, but I get an EXC BAD ACCESS crash when I do that.
You might checkout this open source NSCollectionView alternative from Steven Degutis which, coincidentally I think was just posted to GitHub today (I noticed it today via Twitter):
http://github.com/sdegutis/SDListView
Looks like it allows you to have items with different heights.