Cocoa-Matic

iPhone, iPod touch, iPad tutorials, examples and sample code. Come and get it!

Wednesday, September 1, 2010

NSDate number of days between two dates

Date objects in programming are typically both extremely common and notoriously tricky due to the many different syntaxes involved. These rules apply to iPhone development. In one application I created, I needed the number of days between 2 dates in several instances. As there is no simple NSDate2 - NSDate1 function built-in to return the number of days apart, I wrote a function to accomplish the task. Here it is...
Read more »

Labels: , , , , ,

Friday, August 20, 2010

Create UIPickerView programmatically

The UIPickerView is one of the input controls in iOS for displaying and selecting a list of data. Most tutorials implement this control in Interface Builder, which is fine, but I like to have more control and do most everything in code. There are several components to doing this 100% programmatically, which I will demonstrate below...

The first thing we have to do is include the UIPickerViewDelegate in the interface of our class .h file.
@interface myViewController : UIViewController<UIPickerViewDelegate>
Read more »

Labels: , ,

Wednesday, August 18, 2010

Prevent UITableView from scrolling

Quick tip:

UITableView elements enable vertical scrolling by default. The following code disables the scroll:
myTableView.scrollEnabled = NO;

Labels: , , ,

Tuesday, August 17, 2010

Random Number in Cocoa and Objective-c

Generating a random number is fairly simple using arc4random(). Say you want a random integer between 10 (min) and 1000 (max). Here's how to do it:

int max = 1000;
int min = 10;
int value = (arc4random() % max) + min;

Also, no need to do any seeding with this method.

Labels: ,


« Older Entries  
Newer Entries »