2

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];
Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
William LeGate
  • 540
  • 7
  • 18

4 Answers4

3

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...

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
1

First make myView into a property of the superview Then use

[[[self superview] myView] addSubview:mySubview];
Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56
1

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];
Felix
  • 35,354
  • 13
  • 96
  • 143
  • Okay, so it no longer ads the subview to the view it was... but its not adding it at all now. Probably because it cant find a view with the tag I set even though I set the tag in the superview. Any idea why its not finding it? – William LeGate Jun 08 '12 at 18:31
0

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.

Kimpoy
  • 1,974
  • 3
  • 21
  • 38