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

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]

Related

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.

Getting Subview from another Subview

I have my main window's content view set like this:
newContentView = [[CutoutView alloc]initWithFrame:window.frame];
[window setContentView:newContentView];
[newContentView release];
Where CutoutView is the name of my subclass. Now I want to add a subview to it so I did the following,
filterView = [[FilterView alloc]initWithFrame:NSMakeRect(100, 100, 500, 500)];
[newContentView addSubview:filterView];
[filterView release];
that all works fine except now I want to call methods from my filterView subclass and I want to get it like this but it wont work.
FilterView *filter = [[NSApp delegate] contentView];
I read in the docs that by using contentView it only "returns the highest accessible NSView object in the window hierarchy" So I tried adding the following to the addSubview
[newContentView addSubview:filterView positioned:NSWindowAbove relativeTo:nil];
but that didnt work either any ideas as to what I need to do? Thanks!
The content view is really your CutoutView class so you should be using:
FilterView *filterView = [[[[[NSApp delegate] window] contentView] subviews] objectAtIndex:0];
But two cleaner ways are:
1) Use IBOutlets to keep track of your views and assign them via IB.
2) Use tags:
filterView.tag = 101;
then use:
FilterView* filterView = [[[NSApp delegate] contentView] viewWithTag:101];
ContentView is a method of your window not the app delegate, so if you have a window whose IBOutlet is "window" in your app delegate, then you need to use: FilterView *filter = [[[NSApp delegate] window] contentView];

How can I add a view after the nib is loaded?

Previously, I generate the programme using code, and I have a super class that have a background image all the time. But now I change my coding to using XIB, how can I insert a view after the Nib is loaded? But I don't want all my XIB have a view. Thanks.
-(void)awakeFromNib {
[view addSubview:newView];
}
awakeFromNib is the right method to use. You don't want to cover up views by just adding the subview. Insert the subview instead.
- (void)awakeFromNib
{
UIView *targetView;
UIView *viewToInsert = [self pointerToViewToInsert];
for (UIView *aSubview in self.view.subviews){
if ([aSubview isEqual:self.someView]){
targetView = aSubview;
break;
}
}
if (targetView){
int targetIndex = [self.view.subviews indexOfObject:targetView];
[self.view insertSubview:viewToInsert atIndex:targetIndex];
}

How to remove a subview from a cell's content view?

I have added a UIWebview to the cell's content view
UIWebView *webview = [UIWebView alloc]init];
[cell.contentView addSubview:webview];
Now I intend to remove 'webview' from the cell's contentview.
Can I do the following?
Method 1:
[webview removeFromSuperview];
Or do I really need to loop through all the subviews of the contentview before removing it?
Method 2:
for (UIView *subview in [self.contentView subviews])
{
if ([subview isKindOfClass:[UIWebView class]])
{
[subview removeFromSuperview];
}
}
I tried method 1 and it didn't seem to work, am not sure if I am missing something or if method 2 is the only way to go
Method 1 should work, but are you sure that the web view you're removing is really the same as the one you added? A simple way to remove a given subview from a view is to assign a tag:
//...
webView.tag = 123;
[cell.contentView addSubview:webView];
//...
[[cell.contentView viewWithTag:123] removeFromSuperview];
In practice it would of course be better to use a named constant as the tag.
//creating
// cell.bounds will fill up the cell with your webview
UIWebView *webview = [[UIWebView alloc]initWithFrame:cell.bounds];
webview.tag = 11;
[cell.contentView addSubview:webview];
//removing
[[cell.contentView viewWithTag:11] removeFromSuperview];
You can just use removeFromSuperview. What do you mean by "didn't seem to work?" The most likely problem is that webview is nil at the point that you called removeFromSuperview (or is pointing to some other view). Make sure it's pointing to the view you mean.
I believe that your webview is nil when actually trying it to remove from the superview. Have you tried reloading table data after removing it from the content view? In which methods are you doing view manipulation? Datasource/delegate or custom IBActions?