iPad Wireframe

Posted in Cocoa Touch, iPad on January 29th, 2010 by Christian Beer – Be the first to comment

I love to scribble my screen design ideas for my iPhone apps to paper to see if it the concepts work out and if it will be usable. For that I use the really nice Notepod.

As there is no such thing for the iPad by now (only a matter of time until they come up with that), I created my own wireframe for the iPad which you can download here: iPad Wireframe (A4). The display size is exactly 20cm by 15cm (9,7″, 4:3).

Take advantage of C in Objective-C development

Posted in Cocoa, Cocoa Touch, OS X, iPhone on January 22nd, 2010 by Christian Beer – 1 Comment

Objective-C has the great mechanism called Categories that gives you the ability to extend existing classes (like NSString, etc.) with your own methods. To get an easier way to check if a string starts with another string you can do:
@implementation NSString (CBAccessors)
- (BOOL) startsWith:(NSString*)str {
NSRange r = [self rangeOfString:str];
return r.location == 0 && r.length == [str length];
}
@end

You can then use that method as if it was always available (as long as you #include the header where that Category is declared):
if ([anyStringVariable startsWith:@"test"]) {…}

Please note that you should always give the new methods a prefix, to prevent issues with other Categories adding the same method or if Apple decides to add the method themselves (I removed my prefix here for readability).

But what would you do, if you need a utility method that does’n act on a given object. For example if you want to have a method, that shows an alert screen with a message. You may extend UIAlertView or add a method to your delegate. Both options look a bit like this, when called:
[UIAlertView showAlertViewWithTitle:@"Title"
andMessage:@"Message to display"];

That is nice and readable.

To get an even more compact code, you can build on the fact, that Objective-C is based on C. As many novice Objective-C developer might not know, you can easily integrate Objective-C and C code and build a compact C method for that:
void ShowAlertView(NSString *title, NSString *message) {
UIAlertView *alertView = [[UIAlertView alloc] initWith…. ];
[alertView show];
[alertView release];
}

Then you can show an alert view by:
ShowAlertView(@"Error", @"Downloading the whole internet failed");

The first solution is a bit more self-explanatory because the parameters are named.

There are other examples where a C function is useful. A nice example is a function to get the current language:
NSString *currentLanguage() {
static NSString *currentLanguage = nil;
if (!currentLanguage) {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
currentLanguage = [languages objectAtIndex:0];
}
return currentLanguage;
}

Interesting read: “How to recognise a good programmer”

Posted in Uncategorized on January 18th, 2010 by Christian Beer – 1 Comment

This article: How to recognise a good programmer is a must-read for all people hiring developers that need / want to find good devs!

For me the most interesting part in this article is in part #4 “Hidden experience” the author is quoted:

I started programming when I was about 9, on a Commodore 64. I then migrated onto the PC, did some Pascal. When I was 14 I wrote a raycasting engine in C and Assembler, spent a large amount of time playing with cool graphic effects that you could get your computer to do by messing directly with the video card. This was what I call my “coccoon stage”. When I entered that stage, I was a mediocre programmer, and lacked the confidence to do anything really complicated. When I finished it, I had gained that confidence. I knew that I could code pretty much anything so long as I put my mind to it.

That fits 99% on me. The only differences are: I started at around an age of 10 and I had a C128 as my first home computer :-) The rest ist identical to me.

Prevent Repetitive Strain Injury Syndrome

Posted in OS X on October 30th, 2009 by Christian Beer – Be the first to comment

If you’re working the whole day at a desk it’s a great idea to do something to prevent Repetitive Strain Injury Syndrome (Wikipedia). If you’re using a Mac there is a great software that reminds you isochronously to have a short or longer break. It is a very easy to use software: normaly you download and run it, that’s all… Only if you don’t like the standard time interval you can configure it.

You find the software here: AntiRSI

Enable button depending on UITextFields text

Posted in Cocoa Touch, iPhone on October 19th, 2009 by Christian Beer – Be the first to comment

Sometimes you need to enable a UIButton or UIBarButtonItem only if a given UITextField contains text. The first solution that comes to mind is:

[textField addObserver:self
forKeyPath:@"text"
options:NSKeyValueObservingOptionNew
context:nil];

And check the newValue in observeValueForKeyPath:ofObject:change:context: if it is empty:

- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
NSString* newValue = [change objectForKey:NSKeyValueChangeNewKey];
button.enabled = newValue.length > 0;
}

But that doesn’t work as expected, because the text field of a UITextField is only updated as soon as Return is tapped.

The solution is to implement the following method of UITextFieldDelegate and set the implementing object as delegate:

- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {
NSString *str = [textField.text
stringByReplacingCharactersInRange:range
withString:string];
[button setEnabled: ([str length] > 0)];
return YES;
}

This way the button get’s enabled as soon you enter or remove text in the text field. What remains is the clear-button you can activate in a UITextField. If that button is clicked, the following selector get’s performed on the delegate:

- (BOOL)textFieldShouldClear:(UITextField *)textField {
button.enabled = NO;
return YES;
}
.

Cocoa Literature List

Posted in Cocoa on September 20th, 2009 by Christian Beer – Be the first to comment

A very nice website I found a few days ago:

Cocoa Literature List

It contains a searchable list of articles about all topics related to Cocoa. You can either browse it from a big list of topics or search for certain keywords.

Core Graphics Introduction

Posted in Cocoa, Core Graphics, Uncategorized on September 19th, 2009 by Christian Beer – 2 Comments

If you need an introduction to Core Graphics, these two pages are a nice introduction with some small examples:

Cross Compile ProVoc with Cocotron?

Posted in Cocoa on September 12th, 2009 by Christian Beer – 2 Comments

To build a vocabulary editor for Windows I considered building one in Java as the only option. But then I found the project Cocotron. Cocotron is an implementation of Cocoa for Windows and Linux (and could also be ported to other plattforms). It is easily integrated to Xcode so that you can build the Windows version of your application with few clicks.

For building Cocotron you need to install some tools. These are downloaded and installed with few steps that are well documented on the Cocotron website. The navigation is not always clear on that website but you can find your way through it. The website itself is also coded in Objective-C and compiled for Linux using Cocotron.

After you installed the needed tools you can checkout (or download) and compile Cocotron. For me the Cocoa project was all I had to build since it references some of the other needed packages. With the build process the library gets installed into the Developer folder.

The next step is to add a new target to your existing (or new) project. You can easily do so by copying an existing Mac OS target and clean up and adapt the build settings. That process is also well documented on the Cocotron website but it take some trial and error for me to find out if I did all I need. As soon as you don’t get any low level errors from gcc you can start porting your application. Depending on the complexity of your application it may use some parts of Cocoa that are not implemented in Cocotron by now.

For ProVoc the last part was not easy and is not finished by now. I added many #ifdef _WIN32 and #ifndef __APPLE__ to the code to disable unimplemented parts and it builds without errors (but with many warnings). ProVoc starts on Windows but there still seem to be some problems as it hangs shortly after starting. You can also debug your application on Windows using Insight-GDB what I started to do at last.

As soon as I find some free time I will continue trying to get ProVoc run on Windows and I will post my findings here.