How are you feeling about for-loops? They making okay sense? I want to further drive the point home about how awesome they are by giving an illustration of a problem I faced recently. This post will be more of a quiz than anything else, but I feel like it will be useful.
Let me start by describing the scene. I was doing some OS X development (which is basically the same thing as for iOS as far as logic goes) and I ran into an interesting issue.
I had a window that I needed to be able to dynamically add subviews to. I needed to be able to hand this empty window an array of views, and have it lay them out display them correctly. To make the math simple, each subview in the array is 100 px tall and as wide as the outer window.
So if the array has one item in it when it is time to size the outer window, how tall is the outer container going to be? 100 pixels. So if their are 5 objects in the array, how tall does the outer window need to be? 500 pixels. Pretty easy.
The interesting part comes when you understand that the OS X coordinate system is opposite that of iOS. That means that (0,0) is on the bottom-left as opposed to the top-left like in iOS. To make things more interesting, I need to have the object that is at the 0 position in the array to always be at the top of the window.
If you think about it for a minute, can you come up with the basis for the logic we will need to put into this layout effort? We are definitely going to need a for-loop. Why is that? Well, we need to iterate through each view in the subview array and set it's y coordinate value to a certain position. This is easily achieved with a for loop. I'll go ahead and give you some basics to start off with:
Hopefully that isn't to hard to see what is going on. We are iterating through our array of view objects (NSView is the equivalent of UIView in OS X programming) and are using the setFrameOrigin: method to set the (x,y) values of each view we see.
We have everything in place to solve our problem. We don't need to worry about the x value, that stays the way it is. We do need to worry about the y value though. This is where the quiz comes in.
- Can you tell me what is wrong with the current code that is setting the y value like this: i * 100 ? Keep in mind, we want to have the object at index 0 be at the top of the view that originates from the bottom. And if there is an object at index 1, it needs to be below the object at index 0 in the view hierarchy, etc.
- What could we do (there are a few different ways of accomplishing this) to fix it so that our problem is solved? So that we are displaying our subviews correctly?
This type of exercise is a great way to evaluate your ability to reason through logical dilemas. Send me an email or leave a comment if you have questions or if you want to make a stab at the answer. I'll post the solution later on.
Since OS X (0,0) is at the bottom left, you need to know the height of the view and subtract down from that height with each iteration of the for loop. Instead of i*100 you would put viewHeight-i*100.
ReplyDeleteWell done, Bryce! Using the height of the view is definitely a great way to do it.
ReplyDelete