user47156
user47156

Reputation: 31

How to identify the current view which is on display

If I have 3 different views which are defined in 3 corresponding functions, namely:

- (UIView *)getView1 { /*...*/ }
- (UIView *)getView2 { /*...*/ }
- (UIView *)getView3 { /*...*/ }

These are added to self.view when a particular view is required.

My question is, how do we know which of these views is currently being displayed? Is there a parameter that will identify which view is the current view?

Upvotes: 3

Views: 2081

Answers (3)

progrmr
progrmr

Reputation: 77291

All UIView's have a window property which is set when it is being displayed in a window and set to nil when it is removed. You can get the value of the window property to see if a view is currently being displayed in a window:

BOOL isDisplayed = self.view.window != nil;

You can also override willMoveToWindow: in a subclass of UIView and it will be called whenever the view is added to or removed from a window.

Upvotes: 1

Lounges
Lounges

Reputation: 4664

Assuming you are removing the other two views from self.view when you change them you can use [self superview] to determine which one is currently displayed.

Upvotes: 3

codelogic
codelogic

Reputation: 73752

You can tag each view with with an integer and later read the tag to determine which view is active (assuming you are replacing self.view).

#define TAG_VIEW_1 1
#define TAG_VIEW_2 2
#define TAG_VIEW_3 3
...
[ [self getView1()] setTag:TAG_VIEW_1 ];
[ [self getView2()] setTag:TAG_VIEW_2 ];
[ [self getView3()] setTag:TAG_VIEW_3 ];
...    

if ( self.view.tag == TAG_VIEW_1 ) {
    // do something
}
else if ( self.view.tag == TAG_VIEW_2 ) {
    // etc
}
...

Upvotes: 5

Related Questions