Saturday, February 18, 2012

C# Initializers

C# generic collections offer a lot of flexibility and power for organizing your program's data. However, if you need to set one up manually, you may have written code that looks something like this:

List<string> list1 = new List<string>();
list1.Add("list1 item1");
list2.Add("list1 item2");
list2.Add("list1 item3");

List<string> list2 = new List<string>();
list2.Add("list2 item1");
list2.Add("list2 item2");
list2.Add("list2 item3");

List<string> list3 = new List<string>();
list2.Add("list3 item1");
list2.Add("list3 item2");
list2.Add("list3 item3");

Dictionary<string, List<string>> dictionary = new Dictionary<int, List<string>>();
dictionary.Add("list1", list1);
dictionary.Add("list2", list2);
dictionary.Add("list3", list3);

Wouldn't it be nice if you could declare a dictionary in one statement? Or a list? Or an array? Or even an entire class, complete with all data members, which may themselves be other classes, lists, dictionaries, or arrays? Well, turns out that you can. Find out how after the break!

Friday, February 17, 2012

Customizing Appearance of Disabled UIBarButtonItems

Have you ever wanted to change the appearance of a disabled UIBarButtonItem? It seems to be a fairly common issue without many good solutions - at least, until iOS 5.0, anyway. Perhaps you've tried this one:

UIBarButtonItem button = 
    new UIBarButtonItem("Untitled 1", UIBarButtonItemStyle.Plain, null);
button.TintColor = UIColor.White;
button.Enabled = false;

It all works fine, up until you disabled the button, at which point your button becomes a very faded version of whatever you had in there. Normally, that's what you want to have happen, because dimming the button is a good indication to the user that the button cannot be pressed at the current time. However, there may be situations where that isn't true...

For example, I have a toolbar with a document title in the center. It's a UIBarButtonItem, so that it can detect touches, and respond by showing a UITextField that allows the document title to be edited. The document editor has two modes: an edit mode, and a run mode. In run mode, I don't want the title to be edited, so I disable the button. However, I still want it to be nice and visible, like a title should be. I also wanted to avoid the obvious workaround of swapping out my title button for another non-functional button with the same content. It would work, but it would kind of break some of the behind-the-scenes stuff we're doing with our user interaction model, and would require me to maintain the state of two separate buttons with the title. Also, a button that is enabled has a little sparkly animation when it is touched, to let you know that it is touchable and that the system recognized that you touched it. The two-button workaround means I would still have that animation, and gives the user the impression that they can still edit the title, but it's not working right.

So, what did I do? Find out after the break.

Sunday, February 12, 2012

Easy Permutation Calculation in Python

I was recently doing some Python programming, and had to compute all possible combinations of items from two separate lists. At first, I thought, I'll just do it this way:

list1 = ['a', 'b', 'c']
list2 = ['x', 'y', 'z']
for i in list1:
    for j in list2:
        print i, j
a x
a y
a z
b x
b y
b z
c x
c y
c z

However, my loop body was something that was a little more complicated than a print, and couldn't be done well inside of a loop like that. And besides - this is Python we're talking about. Surely there is a better way!

Well, there is - and don't call me Shirley. Details coming up after the break!