Cocoa Classes

 

Cocoa Classes

Name: Newsreader

Platform: iPhone OS 3.0+

SDK: iPhone SDK 3.1.x+

Type: Template

License: Free, non-distributable

 

What it does:

 

 

Newsreader is an Xcode project template for iPhone.

The project is a starting point for creating your own tab-based newsreader apps:

it includes a tab bar with 4 buttons each linked to a table view which you can fill with headlines.

Newsreader includes pre-made UITableView and UITableViewCell subclasses that

you can use as a starting point for your own subclasses.

Simply add a data model for your news feeds, then modify each pane's

controller class to display the data in the table view. Edit the 4 tab bar item icons,

Default.png, and app icon to match your app's theme, and you're done.

Note that the template project doesn't include navigation controllers but it

is fairly easy to add them yourself: add a UINavigationController instance

to each tab view's controller class and then move the table views into a separate view controlled by each

nav controller, or subclass a UINavigationController for each tab and put the tables inside those.

There are a few great tutorial videos on combining tab bar and nav controllers from O'Reilly Media

on YouTube so check those out for the specifics on how to do it. I may add the Navigation controllers

in the future to the template project but I will probably need UINavigationController subclasses for each

tab so right now I am leaving this as an exercise for the reader.

 

To use Newsreader, place the entire Newsreader Xcode project folder inside Xcode's app templates folder as shown below:

 

 

Restart Xcode, select New Project... from the File menu, and then select "Application" under "iPhone OS:

 

 

 

In the pane on the right, select "Newsreader" and click the Choose... button. Type a new name for your

newsreader project and save it. Voilå! - instant newsreader project based on the template project.

The idea here is to do most of the architectural setup work for you - to provide the skeleton of most

of the classes you will need for a newsreader app and then have you fill in the details. Everything

is set up in advance for you so you don't have to spend time creating all the files and subclasses

every time you want to write a new newsreader app.

 

If you get a Code Sign error when building, switch the SDK popup menu from "Device" to "Simulator":

 

 

Enjoy!

(350KB .zip file)

Name: UIImageView+UIImageViewAdditions

Platform: iPhone OS 3.0+

SDK: iPhone SDK 3.1+

Type: Category

Attached Class: UIImageView

License: Free, non-distributable

What it does:

 

How many times have you wanted to create a UIImageView instance directly from an existing image file?

Using UIImageView+UIImageViewAdditions, now you can.

Add the two source files to your project, and #import the header into your source file.

In your code, create a UIImageView by allocating it, but not initializing it, like so:

 

UIImageView *imageView = nil;

imageView = [ [ UIImageView alloc ] autorelease ];

 

Once you have a UIImageView, just send imageViewWithContentsOfFile: to the UIImageView instance:

 

imageView = [ imageView imageViewWithContentsOfFile:[ [ NSBundle mainBundle ] pathForResource:@"filename" ofType:@"png" ] ];

 

Voila! You now have a new UIImageView from your image file. The returned UIImageView is not autoreleased.

Also note that the returned UIImageView is not the same one as you sent the imageViewWithContentsOfFile:

message to, so you'll want to do any additional setup on the instance after the imageViewWithContentsOfFile: message is sent.

This is sort of a hack because it ditches the original allocated UIImageView, but it will work just fine.

You no longer need to manually create a UIImage first in order to create a UIImageView programatically.

imageViewWithContentsOfFile: works by using the UIImageViewAdditions category declared on UIImageView.

The idea here is to make UIImageView behave just like UIImage's imageWithContentsOfFile: method.

Feel free to use UIImageViewAdditions in your own projects, but do not redistribute it without my permission.

UPDATE: I've fixed some of the comments in the code and added Lane Roathe's suggestion of a

-setImageFromContentsOfFile:

method which allows you to set the image of an existing UIImageView to that of the image file path passed in.

He also suggested changing -imageViewWithContentsOfFile: to a class method, but I decided not to because:

1) Doing so would break existing code.

2) Class methods are only supposed to return autoreleased objects which -imageViewWithContentsOfFile:doesn't and

3) There is currently no existing class method for creating a UIImageView.

Enjoy.

 

(3KB .zip file)

 

Name: NSString+NSStringAdditions

Platform: Mac OS X 10.5/10.6

Type: Category

Attached Class: NSString

License: Free, non-distributable

What it does:

 

I found myself constantly needing to convert an NSString full path to an FSRef and

also to make items invisible that my paths pointed to.

NSString+NSStringAdditions declares two methods on NSStrng:

 

- (BOOL)toFSRef:(FSRef*)ref;

- (void)makeVisible:(BOOL)visible;

 

This category assumes that the NSString you are using contains a full path to a valid file system object.

The item the path points to must exist or both methods will fail.

The first method returns an FSRef for the item that the string points to. If conversion succeeds, YES is also returned.

The second method makes the item either invisible or visible depending on what you pass in the visible parameter.

Note that the -toFSRef and -makeVisible methods are only available on OS X since iPhone doesn't support FSRefs.

This category makes the use of any NSString or NSMutableString containing a path more convenient.

UPDATE: I've now conditionalized the code to work on both iPhone and OS X.

I've also added a -convertEntities: method for both iPhone and OS X which normalizes HTML characters in strings.

Enjoy.

 

(3KB .zip file)

 

 

If you have any questions or comments, email me at

info <at> michael-amorose <dot> com

 

 

 

 

 

 

 


Driving Schools