Archive

Archive for February, 2011

CE241 and CE242 Labs Resources

February 20th, 2011 Ahmet Ardal No comments

I put together all resources I had prepared for the CE241 and CE242 courses’ labs when I was the TA between the years 2008-2010, in Kadir Has University. What I mean by “Resources” is documents and all the source code for lab assignments, homeworks, term project assignments and any example programs. All those stuff is available as three separate git repositories on GitHub:
CE241–Fall-08
CE242–Spring-09
CE242–Spring-10

For course descriptions:
CE241 – Programming Languages
CE242 – Data Structures and Algorithms

Presenting Custom Views Modally in Cocoa Touch

February 19th, 2011 Ahmet Ardal 1 comment

We all know about and use the well known -(void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated method of the UIViewController class, right? It does well for most of the cases, especially for displaying temporary view controllers for a limited task. We present them, they do what they’re expected to do, and we dismiss them when we’re done. An example might be a view that gets some login credentials from the user and passes those to its delegate and dismisses. But, what if we want to present a relatively smaller view, without covering the complete device screen and we also want it to be shown in a modal manner, that is, somewhat disabling the visual and functional interaction with what is behind the view for a while.

This demo project shows one of the many possible ways to do it, just to give an idea on the topic. What I implemented here is a class named ModalBoxView. It is the view to be shown modally. It supports both portrait and landscape interface orientations. Instead of trying to describe what it’s like, here it is:

A screenshot in landscape mode

A screenshot in landscape mode

Read more…

Customizing the Appearance of UINavigationBar

February 11th, 2011 Ahmet Ardal 12 comments

Remember how the navigation bar of Apple’s Notes application looks on iPhone? It doesn’t look like one of the system provided styles for navigation bar, neither UIBarStyleDefault nor UIBarStyleBlack. And it also looks nice and elegant, doesn’t it?

It, sometimes, would be nice to be able to change the background of the navigation bar and the color of navigation bar buttons as well. There are several ways for changing the background view of the navigation bar, one is inserting a UIImageView as a subview to the UINavigationBar at index 0. But i found this method to be a bit problematic when it comes to adapt the background view when the device interface orientation changes. Instead i create a UINavigationBar category class and override the default behaviour of the -(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context method. This alters the appearance of all UINavigationBar instances in an application-wide manner and this may be something you don’t want in some cases. However, it is really not so much difficult to use the same technique and change the appearance of UINavigationBars for individual instances. What we’ve done here may also be useful in the case you want to implement a visual theming system for your application. By dynamically changing the images we use as background, it’s possible to make UINavigationBars look different with different themes your application provides.
Read more…

Loading Images Asynchronously in Background in Cocoa Touch

February 7th, 2011 Ahmet Ardal 4 comments

While developing apps for iOS devices, we, in some cases, need to load some remote resources asynchronously in background without making the main thread and the UI become unresponsive. This demo project explains loading images from the web but the same technique could be applied for loading any type of resources, i.e.: audio files, text files, mail headers… to name a few. There are several possible approaches for implementing background loading in Cocoa Touch. All are based on the same simple principle: perform resource loading tasks on some other thread(s) other than the main/UI thread and make UI updates(if any needed) on main thread. There are two nice classes in Cocoa Touch framework which I used for this demo project: NSOperationQueue and NSInvocationOperation. Although NSOperationQueue provides some other complex features, what we simply need here is just:

  1. Create an NSOperationQueue object,
  2. Create NSInvocationOperation objects with target, selector and task data(in our case this is the image url)
  3. Add the NSInvocationOperation object to the NSOperationQueue we created
  4. Run the operation queue to process queued operations(in our case, an operation is to download an image from the internet)
  5. If there is need for any updates on the UI after a task is processed, i.e. an image is downloaded, perform those updates on the main thread by calling one of the performSelectorOnMainThread methods of the NSObject class.

That’s all it simply takes. Now time to show some parts of the code (which i consider worthwhile to have a look at). I believe the code is self-explanatory enough to be understood without any further explanation on what it does.
Read more…