Saturday, November 22, 2008

Creating Toolbar in iphone sdk

Here is how you can create a toolbar at the bottom of your screen. (with code taken from the UICatalog sample)

UIToolbar *bar = [UIToolbar new];
bar.barStyle = UIBarStyleBlackOpaque;

// size up the toolbar and set its frame
[bar sizeToFit];
CGFloat toolbarHeight = [bar frame].size.height;
CGRect mainViewBounds = self.view.bounds; //view of your viewcontroller
[bar setFrame:CGRectMake(CGRectGetMinX(mainViewBounds),
CGRectGetMinY(mainViewBounds) + CGRectGetHeight(mainViewBounds),
CGRectGetWidth(mainViewBounds),
toolbarHeight)];

//add button
// create a bordered style button with custom image
UIImage *im = [UIImage imageNamed:@"selection.png"];
UIBarButtonItem *selectItem = [[UIBarButtonItem alloc] initWithImage:im
style:UIBarButtonItemStylePlain
target:self
action:@selector(changeSelection:)];
selectItem.width = 50.0f; //you can set the width of the icon
selectItem.title= @"Selection"; //set text below image icon

//note the nil at the end
NSArray *items = [NSArray arrayWithObjects: selectItem,nil];
[bar setItems:items animated:NO];
[self.view addSubView:bar];
[selectItem release];
[bar release];

sizing UILabel accordingly to the height of the string

In my app I needed to resize the UILabel size dynamically in runtime as the text would not be determined during compilation time. Luckily this can be done by using the sizeWithFont method of NSString class.
  1. Call sizeWithFont of the text.
  2. Get back a CGSize struct and you can now init your UILabel with the struct.
UIFont *font = [UIFont systemFontOfSize:14];

CGSize textSize = [ myText sizeWithFont: font constrainedToSize: CGSizeMake(lineWidth, lineHeight*1000.0f) lineBreakMode: UILineBreakModeTailTruncation ];

UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(10,10,textSize.width,textSize.height)];

This took me awhile to find out..hope it helps you. Happy coding.

Objective C forward declaration

Was 'debugging' this for few hours, nothing more than not reading the documentations carefully. I needed to create a forward declaration of a class A in my class B as I would need to access some properties in class A.

However when compiling XCode was complaining to me about 'error: request for member in something not a structure or union'. Cracked my brain, tried rewriting my accessors in class A. Checking the access rights, property declarations, all was in order.

Finally did the google ritual and found out that to access properties in a forward declared class you must include the interface file in the implementation file of the class accessing. Too abstract to talk about here's the example:

Class B .h file

#import ....

//forward declare class a
@class A

@interface B

Class B .m file

//import interface file for class A
#import "classA.h"

@implementation B
....
A.someProperty = @"Got me there";

Hope this helps.. :)

my iphone development blog

Hi all,
This would be a blog about my iphone application development blog. Have been developing my app for the app store for the past two months and making headway. It was a great learning experience and I must say developing for the iphone is like no other device, the possibilities is huge and the best part is that boring business apps are even given a new lease of life.

I can say the end product is certainly better with less effort as compared to WM which I had developed for.

Steadily I would be posting tutorials or code snippets and lessons I learn along the way. Hopefully can get my iphone developer program approved and have my app up to the app store.

Best regards.