iPhone OS: Day 3, pain
Posted by elbryan
Once again, thank you Ayreon.
Today I started developing something more "special". I like the idea of having multipages since the content can be easily surfed with just two fingers. So I've started gathering information about this and I found something really interesting. An iPhone OS Developer can manage program's memory almost as much it likes. In this little application I wrote, there's a bit of memory management since just the pages nearby the current are pre-loaded. For example, if you are on page 3, only pages 2 and 4 are loaded in memory (mainly to avoid glitches when you decide to switch page).
However all that glitters ain't gold! What I'm really finding difficult to understand, is how components work. The main concept is "delegation". Everything delegates to something in a completely mystical way (at least for beginners). Sometimes you think to have full control of what you do. Sometimes you "just click into two spots" and that's it. It's quite confusing.
One thing you have to remind is the fact that delegation means "explicit delegation". Inside Interface Builder application you can put the components as you wish but you've to remember to explicitly delegate those to your previously defined interface; otherwise nothing will work. This has to be done for components as well for methods or events. It's something that you've to get used to if you want something running.
Another painful concept is nib/xib files management. These packages are something that you manage from Interface Builder but they are completely useless unless you full instance them in compile time. The first time I gave a look at Interface Builder, I started thinking that "visual objects" has to belong to "visual interface". That's not completely true. I wrote this test-application in order to understand this behaviour. The two pages that compose the applications are two distinct xib files. Once you've written them, you can put the two xibs instances into an objects array and load them on demand.
It's something like:
NSMutableArray *controllers = [[NSMutableArray alloc] init]; [controllers addObject:[[Controller alloc] initWithNibName:@"Page1" bundle:[NSBundle mainBundle]]]; [controllers addObject:[[Controller alloc] initWithNibName:@"Page2" bundle:[NSBundle mainBundle]]]; self.viewControllers = controllers; [controllers release];
In this example I create a NSMutableArray and I put inside it two instaces of two NIB files. Something that scared me firstly is that release. In the row above you just assign the value to the viewControllers variable and then you release it. I thinkg that "equals/=" means that you perform a copy of the object. Otherwise, in my code, I release something that's going to be used nextly in the program. KABOOM!! I know that's something really trivial for someone that already knows how iPhone OS works but I liked sharing that with you. Once you've done that, your array will contain two "pages". In order to pop them when you switch page, you'll have to do the opposite. Something like:
- (void) loadScrollViewWithPage:(int)page { Controller *aViewController; aViewController = [viewControllers objectAtIndex:page]; [scrollView addSubview:aViewController.view]; }
This snippet says that you have a method that does "something" whenever you change the page currently showed. Declare a Controller instance, grab from viewControllers (the NSMutableArray used above) the object at index page and then add it to the Subview. Subview is simply what you are looking at on the device (not sure if that hides something more).
Since I'm starting right now to learn programming in Obj-C / iPhone OS , I might have made some mistakes above. If you notice something weird, please let me know. Thank you.
Posted in Apple, iPhone | no comments |