开发者

can I reference a subview by its index?

I display a modal view controller and from there use buttons to add subviews. I need to add other view controllers as subviews, and then remove the one behind it. So can I reference the views by their index?

Practical example: User sees page 1 (The root view controller) User presses button which adds a subview, page 2. User presses another button which adds a subview, page 3. Now I need to remove page 2 so as not to consume memory and hav开发者_运维百科e things going on back there.

It's not as simple as removing it by name, because my root view controller has next/back buttons and buttons to specific views. I can always insert the right "next" subview but the one to remove could be any of them. The user could be on page 2, 3, 4 etc and jump to page 5. In the button action for page 5 I can of course add the page 5 view controller as a subview, but how do I tell it to remove the last subview when it could be any of them?


i think this could be solved with a proper use of UINavigationController; however, you could always use [rootViewController.subviews objectAtIndex:subViewIndex];


You can use

[[parentView subviews] objectAtIndex:n];

But buyer beware, it's dangerous to make assumptions about where things appear in that array. If you can't think of a better way to achieve your goal, you'll want to at least do some sort of type check (isKindOfClass) to make sure what is at that index is actually what you wanted to act upon.


Root View = A; Second View = B; Third View = C;

you want to navigate A -> B -> C -(back)> A correct?

as you are going from B to C, just pop to the root view controller to A and then push on C, B is now not in the mix.

This only will work if you are using a UINavigationController, but you can use the same idea for your problem.

-- For Modal: --

// Viewing B, going to go to C:
[self dismissModalViewControllerAnimated:NO]; // B is gone.
[self presentModalViewController:C animated:YES]; // C animates up
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜