UNDERSTANDING VIEW HIERARCHY

As views are created and added, they are added to a tree data structure. Views are displayed in the order that they are added. To verify this, modify the location of the UIButton object you created earlier by changing its location to CGRectMake (10, 30, 300, 50), as in the following:

//---create a Button view---
//frame = CGRectMake(10, 70, 300, 50);
frame = CGRectMake(10, 30, 300, 50);

image

FIGURE 3-24

When you now run the application again, you will notice that the Round Rect Button overlaps the Label (see Figure 3-24) because the button was added last:

[view addSubview:label];
[view addSubview:button];

To switch the order in which the views are displayed after they have been added, use the exchangeS ubviewAtIndex:withSubviewAtIndex: method:

[view addSubview:label];
[view addSubview:button];
[view exchangeSubviewAtlndex:1 withSubviewAtIndex:0];

self.view = view;
[label release];
[view release];

image

FIGURE 3-25

The preceding statement in bold swaps the order of the Label and Round Rect Button. When the application is run again, the Label will now appear on top of the Round Rect Button (see Figure 3-25).

To learn the order of the various views already added, you can use the following code segmentto print the value of the tag property for each view:

[view ...

Get Beginning iOS 5 Application Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.