<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Derek Neely &#187; Programming</title>
	<atom:link href="http://derekneely.com/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://derekneely.com</link>
	<description>...what I care to share...</description>
	<lastBuildDate>Thu, 06 Oct 2011 15:10:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Size-to-Fit Text in UITextView (iPhone)</title>
		<link>http://derekneely.com/2011/04/size-to-fit-text-in-uitextview-iphone/</link>
		<comments>http://derekneely.com/2011/04/size-to-fit-text-in-uitextview-iphone/#comments</comments>
		<pubDate>Wed, 27 Apr 2011 16:44:43 +0000</pubDate>
		<dc:creator>derek</dc:creator>
				<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[Fit Text]]></category>
		<category><![CDATA[Font Size]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Resize Text]]></category>
		<category><![CDATA[Text]]></category>
		<category><![CDATA[UITextView]]></category>

		<guid isPermaLink="false">http://derekneely.com/?p=615</guid>
		<description><![CDATA[The UITextView is a very useful view. You&#8217;re able to show and interact with large amounts of text. However, I recently found the need to size the UITextView in Interface Builder to fit nicely in a pre-layed out interface. Well, low-and-behold the client I was working for started sending large amounts of text that was [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://derekneely.com/wp-content/uploads/2009/11/xcode-icon.png"><img class="alignright size-full wp-image-479" title="xcode-icon" src="http://derekneely.com/wp-content/uploads/2009/11/xcode-icon.png" alt="" width="143" height="143" /></a>The UITextView is a very useful view. You&#8217;re able to show and interact with large amounts of text. However, I recently found the need to size the UITextView in Interface Builder to fit nicely in a pre-layed out interface. Well, low-and-behold the client I was working for started sending large amounts of text that was to be displayed here. We didn&#8217;t want scrolling but needed to display all the text. So, we needed to resize the text until it would fit and all would be displayed. Below is a very simple way of resizing the text in the UITextView to fit within its current bounds.</p>
<p>At some point define the max size you want the font to be. I typically put this in my .h file</p>
<p><!-- p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #7b482f} span.s1 {color: #2f2fcf} --></p>
<pre>#define kDefaultFontSize 24.0</pre>
<p>Then in your .m file where you add the text to the UITextView you would place something like:</p>
<p><!-- p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #488186} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px} p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008426} p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #753ea3} p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo} span.s1 {color: #000000} span.s2 {color: #753ea3} span.s3 {color: #411f7c} span.s4 {color: #d92823} span.s5 {color: #c22a9c} span.s6 {color: #488186} span.s7 {color: #2f2fcf} span.s8 {color: #7b482f} --></p>
<pre>myTextView.text = @"Some long string that will be in the UITextView";
myTextView.font = [UIFont systemFontOfSize:kDefaultFontSize];

//setup text resizing check here
if (myTextView.contentSize.height &gt; myTextView.frame.size.height) {
    int fontIncrement = 1;
    while (myTextView.contentSize.height &gt; myTextView.frame.size.height) {
        myTextView.font = [UIFont systemFontOfSize:kDefaultFontSize-fontIncrement];
        fontIncrement++;
    }
}</pre>
<p>Hope this helps! Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://derekneely.com/2011/04/size-to-fit-text-in-uitextview-iphone/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>iPhone &#8211; UITableViewCell Custom Selection Style Color</title>
		<link>http://derekneely.com/2010/01/uitableviewcell-custom-selection-style-color/</link>
		<comments>http://derekneely.com/2010/01/uitableviewcell-custom-selection-style-color/#comments</comments>
		<pubDate>Fri, 15 Jan 2010 04:16:59 +0000</pubDate>
		<dc:creator>derek</dc:creator>
				<category><![CDATA[Mac]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Custom UITableView]]></category>
		<category><![CDATA[Custom UITableViewCell]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Selection Style]]></category>
		<category><![CDATA[Selection Style Color]]></category>
		<category><![CDATA[UITableView]]></category>
		<category><![CDATA[UITableViewCell]]></category>
		<category><![CDATA[UITableViewCellSelectionStyle]]></category>

		<guid isPermaLink="false">http://derekneely.com/?p=516</guid>
		<description><![CDATA[Oddly, Apple has given you the ability to vastly customize the primary view of  a UITableViewCell. However, the selection style view is much more difficult to do such. Apple kindly gave us UITableViewSelectionStyleBlue/Gray/None. Well, that doesn&#8217;t always cut it does it? There are already some very good tutorials on revamping the whole table view.  Google [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright size-full wp-image-479" title="xcode-icon" src="http://derekneely.com/wp-content/uploads/2009/11/xcode-icon.png" alt="xcode-icon" width="154" height="154" />Oddly, Apple has given you the ability to vastly customize the primary view of  a UITableViewCell. However, the selection style view is much more difficult to do such. Apple kindly gave us UITableViewSelectionStyleBlue/Gray/None. Well, that doesn&#8217;t always cut it does it?</p>
<p>There are already some very good tutorials on revamping the whole table view.  <a title="Google - Custom UITableView" href="http://www.google.com/search?q=custom+uitableview&amp;ie=utf-8&amp;oe=utf-8&amp;aq=t&amp;rls=org.mozilla:en-US:official&amp;client=firefox-a" target="_blank">Google it!</a> The best I came across was from <a title="Cocoa with Love - Custom UITableView" href="http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html" target="_blank">Cocoa with Love &#8211; Custom UITableView</a>.</p>
<p>However, this can be a bit overkill for some situations. It can also take a good bit of time to setup all the necessary images. What I needed was a quick and easy way to &#8216;theme&#8217; the application and thus the selection style for a UITableViewCell. This needed to work with both plain and grouped table view styles and custom cells as well. You can see now why this may take  a while using the above methods.</p>
<p>Here is a quick and easy method to do just that.</p>
<p><strong>SomeViewController.h</strong></p>
<pre>#import &lt;UIKit/UIKit.h&gt;

@interface SomeViewController : UITableViewController {
     UITableViewCell *currentSelectedCell;
}

@property (nonatomic, retain) UITableViewCell *currentSelectedCell;

- (void)setSelectedCell:(UITableViewCell *)selectedCell
     deselectedCell:(UITableViewCell *)deselectedCell;

@end</pre>
<p><strong>SomeViewController.m</strong></p>
<pre>#import "SomeViewController.h"

@implementation SomeViewController

@synthesize currentSelectedCell;

- (void)setSelectedCell:(UITableViewCell *)selectedCell deselectedCell:(UITableViewCell *)deselectedCell {
     //revert deselected cell
     if (deselectedCell != nil) {
          deselectedCell.backgroundColor = [UIColor whiteColor];
          for (UIView *subView in [deselectedCell.contentView subviews]) {
               if ([subView isKindOfClass:[UILabel class]]) {
                    UILabel *label = (UILabel *)subView;
                    label.textColor = [UIColor blackColor];
               }

               if ([subView isKindOfClass:[UITextField class]]) {
                    UITextField *textField = (UITextField *)subView;
                    textField.textColor = [UIColor blackColor];
               }
          }
     }

     //setup selected cell
     selectedCell.backgroundColor = [UIColor redColor]; //Some defined UIColor
     for (UIView *subView in [selectedCell.contentView subviews]) {
          if ([subView isKindOfClass:[UILabel class]]) {
               UILabel *label = (UILabel *)subView;
               label.textColor = [UIColor whiteColor];
          }

          if ([subView isKindOfClass:[UITextField class]]) {
               UITextField *textField = (UITextField *)subView;
               textField.textColor = [UIColor whiteColor];
          }
     }
}

#pragma mark -
#pragma mark Table View Delegate Methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
     (NSIndexPath *)indexPath {

     setSelectedCell:[tableView cellForRowAtIndexPath:indexPath]
          deselectedCell:currentSelectedCell];

     currentSelectedCell = [tableView cellForRowAtIndexPath:indexPath];
}

#pragma mark -
#pragma mark Table View Data Source Methods

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath {

     //........Setup UITableViewCell...........//

     cell.selectionStyle = UITableViewCellSelectionStyleNone;

     return cell;
}

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:
     (NSInteger)section {

     return count;
}

@end</pre>
<p>Now, I&#8217;ve provided this in one class but what I&#8217;ve done is setup an &#8216;application&#8217; singleton that has this function and is called from each UITableView&#8217;s didSelectRowAtIndexPath delegate. Works great on standard, custom, plain, and grouped views.</p>
]]></content:encoded>
			<wfw:commentRss>http://derekneely.com/2010/01/uitableviewcell-custom-selection-style-color/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>iPhone Development &#8211; Continuous Picker</title>
		<link>http://derekneely.com/2009/12/iphone-development-continuous-picker/</link>
		<comments>http://derekneely.com/2009/12/iphone-development-continuous-picker/#comments</comments>
		<pubDate>Thu, 31 Dec 2009 21:42:21 +0000</pubDate>
		<dc:creator>derek</dc:creator>
				<category><![CDATA[Mac]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Continuous Picker]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Picker]]></category>

		<guid isPermaLink="false">http://derekneely.com/?p=513</guid>
		<description><![CDATA[Coming soon&#8230;been hard at work on some projects.]]></description>
			<content:encoded><![CDATA[<p>Coming soon&#8230;been hard at work on some projects.</p>
]]></content:encoded>
			<wfw:commentRss>http://derekneely.com/2009/12/iphone-development-continuous-picker/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>iPhone Development &#8211; Global Variables (Singleton)</title>
		<link>http://derekneely.com/2009/11/iphone-development-global-variables/</link>
		<comments>http://derekneely.com/2009/11/iphone-development-global-variables/#comments</comments>
		<pubDate>Fri, 06 Nov 2009 15:20:41 +0000</pubDate>
		<dc:creator>derek</dc:creator>
				<category><![CDATA[Mac]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[App Delegate]]></category>
		<category><![CDATA[Global]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Singleton]]></category>
		<category><![CDATA[Variables]]></category>

		<guid isPermaLink="false">http://derekneely.com/?p=480</guid>
		<description><![CDATA[In iPhone development, if/when you need a single instance of a variable that can be shared and manipulated where and how should you implement this. Thus far I have found 2 ways of doing so. The first would be to add the global variables to your AppDelegate (which can be done and will be explained [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright size-full wp-image-479" title="xcode-icon" src="http://derekneely.com/wp-content/uploads/2009/11/xcode-icon.png" alt="xcode-icon" width="143" height="143" />In iPhone development, if/when you need a single instance of a variable that can be shared and manipulated where and how should you implement this. Thus far I have found 2 ways of doing so. The first would be to add the global variables to your AppDelegate (which can be done and will be explained but isn&#8217;t the preferred method). The second and &#8216;correct&#8217; way of going about globals is to create a Singleton.</p>
<p>For the following examples we will assume the global variable you are trying to implement is a User object.</p>
<p>User.h</p>
<blockquote>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #df0002;"><span style="color: #7d4726;">#import </span>&lt;Foundation/Foundation.h&gt;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;"><span style="color: #c900a4;">@interface</span> User : NSObject {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #7a0eac;"><span style="color: #000000;"><span style="white-space: pre;"> </span></span>NSString<span style="color: #000000;"><span style="white-space: pre;"> </span>*</span><span style="color: #438288;">username</span><span style="color: #000000;">;</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #7a0eac;"><span style="color: #000000;"><span style="white-space: pre;"> </span></span>NSString<span style="color: #000000;"><span style="white-space: pre;"> </span>*</span><span style="color: #438288;">password</span><span style="color: #000000;">;</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008d00;"><span style="color: #000000;"><span style="white-space: pre;"> </span></span><span style="color: #7a0eac;">NSString</span><span style="color: #000000;"><span style="white-space: pre;"> </span>*</span><span style="color: #438288;">accountType</span><span style="color: #000000;">; </span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;">}</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;"><span style="color: #c900a4;">@property</span> (<span style="color: #c900a4;">nonatomic</span>, <span style="color: #c900a4;">retain</span>) NSString *username;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;"><span style="color: #c900a4;">@property</span> (<span style="color: #c900a4;">nonatomic</span>, <span style="color: #c900a4;">retain</span>) NSString *password;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;"><span style="color: #c900a4;">@property</span> (<span style="color: #c900a4;">nonatomic</span>, <span style="color: #c900a4;">retain</span>) NSString *accountType;</p>
</blockquote>
<p>User.m</p>
<blockquote>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #7d4726;">#import <span style="color: #df0002;">&#8220;User.h&#8221;</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #c900a4;">@implementation<span style="color: #000000;"> User</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #c900a4;">@synthesize<span style="color: #000000;"> username;</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #c900a4;">@synthesize<span style="color: #000000;"> password;</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;"><span style="color: #c900a4;">@synthesize</span> accountType;</p>
</blockquote>
<hr /><strong>App Delegate Method</strong></p>
<p>ApplicationAppDelegate.h</p>
<blockquote>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #7d4726;">#import <span style="color: #df0002;">&#8220;User.h&#8221;</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #7d4726;"><span style="color: #df0002;"><br />
</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;"><span style="color: #c900a4;">@interface</span> ApplicationAppDelegate : NSObject &lt;UIApplicationDelegate&gt; {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #7a0eac;"><span style="color: #000000;"> </span>NSManagedObjectModel<span style="color: #000000;"> *</span><span style="color: #438288;">managedObjectModel</span><span style="color: #000000;">;</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #7a0eac;"><span style="color: #000000;"> </span>NSManagedObjectContext<span style="color: #000000;"> *</span><span style="color: #438288;">managedObjectContext</span><span style="color: #000000;">;<span style="white-space: pre;"> </span> </span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #7a0eac;"><span style="color: #000000;"> </span>NSPersistentStoreCoordinator<span style="color: #000000;"> *</span><span style="color: #438288;">persistentStoreCoordinator</span><span style="color: #000000;">;</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #7a0eac;"><span style="color: #000000;"> </span>UIWindow<span style="color: #000000;"> *</span><span style="color: #438288;">window</span><span style="color: #000000;">;</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #7a0eac;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;"><span style="color: #438288;">User</span> *user;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;">}</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;"><span style="color: #c900a4;">@property</span> (<span style="color: #c900a4;">nonatomic</span>, <span style="color: #c900a4;">retain</span>, <span style="color: #c900a4;">readonly</span>) NSManagedObjectModel *managedObjectModel;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;"><span style="color: #c900a4;">@property</span> (<span style="color: #c900a4;">nonatomic</span>, <span style="color: #c900a4;">retain</span>, <span style="color: #c900a4;">readonly</span>) NSManagedObjectContext *managedObjectContext;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;"><span style="color: #c900a4;">@property</span> (<span style="color: #c900a4;">nonatomic</span>, <span style="color: #c900a4;">retain</span>, <span style="color: #c900a4;">readonly</span>) NSPersistentStoreCoordinator *persistentStoreCoordinator;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #c900a4;">@property<span style="color: #000000;"> (</span>nonatomic<span style="color: #000000;">, </span>retain<span style="color: #000000;">) </span>IBOutlet<span style="color: #000000;"> UIWindow *window;</span></p>
<div><span style="font-family: Menlo, 'Times New Roman', 'Bitstream Charter', Times, serif; font-size: small;"><span style="line-height: normal;"> </span></span></div>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #c900a4;">@property<span style="color: #000000;"> (</span>nonatomic<span style="color: #000000;">, </span>retain<span style="color: #000000;">) User *user;</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #c900a4;"><span style="color: #000000;"><br />
</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #c900a4;">
</blockquote>
<p>ApplicationAppDelegate.m</p>
<blockquote>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #df0002;"><span style="color: #7d4726;">#import </span>&#8220;ApplicationAppDelegate.h&#8221;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;"><span style="color: #c900a4;">@implementation</span> ApplicationAppDelegate</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #c900a4;">@synthesize<span style="color: #000000;"> window;</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;"><span style="color: #c900a4;">@synthesize</span> user;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #7d4726;">#pragma mark -</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #7d4726;">#pragma mark Application lifecycle</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;">- (<span style="color: #c900a4;">void</span>)applicationDidFinishLaunching:(<span style="color: #7a0eac;">UIApplication</span> *)application {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #438288;"><span style="color: #000000;"> [</span>window<span style="color: #000000;"> </span><span style="color: #450084;">addSubview</span><span style="color: #000000;">:</span>tabBarController<span style="color: #000000;">.</span><span style="color: #450084;">view</span><span style="color: #000000;">];</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px;"><span style="white-space: pre;"> </span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008d00;"><span style="color: #000000;"> </span>// Override point for customization after app launch</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #450084;"><span style="color: #000000;"><span style="white-space: pre;"> </span>[</span><span style="color: #438288;">window</span><span style="color: #000000;"> </span>makeKeyAndVisible<span style="color: #000000;">];</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #450084;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;"><span style="color: #c900a4;"> </span>user = [[<span style="color: #438288;">User</span> <span style="color: #450084;">alloc</span>] <span style="color: #450084;">init</span>];</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;">}</p>
</blockquote>
<p>Then from any view controller you can access the Delegate variable as such.</p>
<p>SomeViewController.m</p>
<blockquote>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #7a0eac;">UIApplication<span style="color: #000000;"> *app = [</span>UIApplication<span style="color: #000000;"> </span><span style="color: #450084;">sharedApplication</span><span style="color: #000000;">];</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #7a0eac;"><span style="color: #000000;">app.user.username = @&#8221;Username&#8221;;</span></p>
</blockquote>
<p>While this can be done and will work. The overall use of the AppDelegate for this functionality is incorrect. The AppDelegate should be used for the following 2 reasons:</p>
<ul style="padding-left: 26px;">
<li>implemenations of the <code style="font-family: Courier, monospace; font-size: 12px;">NSApplication</code> delegate methods (including<code style="font-family: Courier, monospace; font-size: 12px;">applicationDidFinishLaunching:</code> to finalize application construction)</li>
<li>handling menu items for items that don&#8217;t exist in a window (for example, opening the application Preferences window)</li>
</ul>
<p>So, on that note, lets implement a Singleton. The &#8216;correct&#8217; way of creating a single global instance of an object that can be accessed and manipulated by the view controllers.</p>
<hr /><strong>Singleton Method</strong></p>
<p>First we need to make some changes to our User class as such:</p>
<p>User.h</p>
<blockquote>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #df0002;"><span style="color: #7d4726;">#import </span>&lt;Foundation/Foundation.h&gt;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;"><span style="color: #c900a4;">@interface</span> User : NSObject {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #7a0eac;"><span style="color: #000000;"><span style="white-space: pre;"> </span></span>NSString<span style="color: #000000;"><span style="white-space: pre;"> </span>*</span><span style="color: #438288;">username</span><span style="color: #000000;">;</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #7a0eac;"><span style="color: #000000;"><span style="white-space: pre;"> </span></span>NSString<span style="color: #000000;"><span style="white-space: pre;"> </span>*</span><span style="color: #438288;">password</span><span style="color: #000000;">;</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008d00;"><span style="color: #000000;"><span style="white-space: pre;"> </span></span><span style="color: #7a0eac;">NSString</span><span style="color: #000000;"><span style="white-space: pre;"> </span>*</span><span style="color: #438288;">accountType</span><span style="color: #000000;">;</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;">}</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;"><span style="color: #c900a4;">@property</span> (<span style="color: #c900a4;">nonatomic</span>, <span style="color: #c900a4;">retain</span>) NSString *username;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;"><span style="color: #c900a4;">@property</span> (<span style="color: #c900a4;">nonatomic</span>, <span style="color: #c900a4;">retain</span>) NSString *password;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;"><span style="color: #c900a4;">@property</span> (<span style="color: #c900a4;">nonatomic</span>, <span style="color: #c900a4;">retain</span>) NSString *accountType;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;">+ (<span style="color: #438288;">User</span> *)sharedUser;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #c900a4;">@end</p>
</blockquote>
<p>User.m</p>
<blockquote>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #7d4726;">#import <span style="color: #df0002;">&#8220;User.h&#8221;</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;"><span style="color: #c900a4;">static</span> User *sharedUser = <span style="color: #c900a4;">nil</span>;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #c900a4;">@implementation<span style="color: #000000;"> User</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #c900a4;">@synthesize<span style="color: #000000;"> username;</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #c900a4;">@synthesize<span style="color: #000000;"> password;</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;"><span style="color: #c900a4;">@synthesize</span> accountType;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #7d4726;">#pragma mark -</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #7d4726;">#pragma mark Singleton Methods</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;">+ (<span style="color: #438288;">User</span> *)sharedUser {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #438288;"><span style="color: #000000;"><span style="white-space: pre;"> </span></span><span style="color: #c900a4;">if</span><span style="color: #000000;">(</span>sharedUser<span style="color: #000000;"> == </span><span style="color: #c900a4;">nil</span><span style="color: #000000;">){</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #450084;"><span style="color: #000000;"><span style="white-space: pre;"> </span></span><span style="color: #438288;">sharedUser</span><span style="color: #000000;"> = [[</span><span style="color: #c900a4;">super</span><span style="color: #000000;"> </span>allocWithZone<span style="color: #000000;">:</span><span style="color: #c900a4;">NULL</span><span style="color: #000000;">] </span>init<span style="color: #000000;">];</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;"><span style="white-space: pre;"> </span>}</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #438288;"><span style="color: #000000;"><span style="white-space: pre;"> </span></span><span style="color: #c900a4;">return</span><span style="color: #000000;"> </span>sharedUser<span style="color: #000000;">;</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;">}</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;">+ (<span style="color: #c900a4;">id</span>)allocWithZone:(<span style="color: #7a0eac;">NSZone</span> *)zone {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #275a5e;"><span style="color: #000000;"><span style="white-space: pre;"> </span></span><span style="color: #c900a4;">return</span><span style="color: #000000;"> [[</span><span style="color: #c900a4;">self</span><span style="color: #000000;"> </span>sharedManager<span style="color: #000000;">] </span><span style="color: #450084;">retain</span><span style="color: #000000;">];</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;">}</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;">- (<span style="color: #c900a4;">id</span>)copyWithZone:(<span style="color: #7a0eac;">NSZone</span> *)zone {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #c900a4;"><span style="color: #000000;"><span style="white-space: pre;"> </span></span>return<span style="color: #000000;"> </span>self<span style="color: #000000;">;</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;">}</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;">- (<span style="color: #c900a4;">id</span>)retain {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #c900a4;"><span style="color: #000000;"><span style="white-space: pre;"> </span></span>return<span style="color: #000000;"> </span>self<span style="color: #000000;">;</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;">}</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;">- (<span style="color: #c900a4;">unsigned</span>)retainCount {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #7d4726;"><span style="color: #c900a4;"> return</span><span style="color: #000000;"> </span>NSUIntegerMax<span style="color: #000000;">;</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;">}</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;">- (<span style="color: #c900a4;">void</span>)release {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008d00;">//do nothing</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;">}</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;">- (<span style="color: #c900a4;">id</span>)autorelease {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #c900a4;"><span style="color: #000000;"><span style="white-space: pre;"> </span></span>return<span style="color: #000000;"> </span>self<span style="color: #000000;">;</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;">}</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #c900a4;">@end</p>
</blockquote>
<p>Now, in any view controller we need to access/manipulate the &#8216;global&#8217; user we can do:</p>
<p>SomeViewController.m</p>
<blockquote>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #df0002;"><span style="color: #7d4726;">#import </span>&#8220;SomeViewController.h&#8221;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #7d4726;">#import <span style="color: #df0002;">&#8220;User.h&#8221;</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #c900a4;">@implementation<span style="color: #000000;"> SomeViewController</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;">- (<span style="color: #c900a4;">void</span>)someFunction{</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #438288;"><span style="color: #000000;"><span style="white-space: pre;"> </span></span>User<span style="color: #000000;"> *</span>user<span style="color: #000000;"> = [</span>User<span style="color: #000000;"> </span><span style="color: #275a5e;">sharedUser</span><span style="color: #000000;">];</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #438288;"><span style="color: #000000;"><span style="white-space: pre;"> </span></span>user<span style="color: #000000;">.</span>username<span style="color: #000000;"> = </span><span style="color: #df0002;">@&#8221;Username&#8221;</span><span style="color: #000000;">;</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo;">}</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #c900a4;">@end</p>
</blockquote>
<p>References and resources that made this post and my learning possible.</p>
<ul>
<li><a title="Cocoa Fundamentals Guide" href="http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html" target="_blank">Cocoa&#8217;s Fundamentals Guide</a> (by Apple)</li>
<li><a title="Cocoa With Love" href="http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html" target="_blank">Cocoa With Love</a></li>
<li><a title="Matt Galloway" href="http://iphone.galloway.me.uk/iphone-sdktutorials/singleton-classes/" target="_blank">Matt Galloway</a></li>
<li><a title="Stack Overflow" href="http://stackoverflow.com/questions/338734/iphone-proper-usage-of-application-delegate" target="_blank">Stack Overflow</a></li>
</ul>
<p>- Updated: 11.17.09 &#8211; Reviewed Apple&#8217;s documentation on singletons and made the proper changes. <a title="Apple's Singleton Documentation" href="http://developer.apple.com/iphone/library/DOCUMENTATION/General/Conceptual/DevPedia-CocoaCore/Singleton.html" target="_blank">Apple&#8217;s Singleton Documentation</a></p>
]]></content:encoded>
			<wfw:commentRss>http://derekneely.com/2009/11/iphone-development-global-variables/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Image Gallery (Plogger)</title>
		<link>http://derekneely.com/2009/05/image-gallery-plogger/</link>
		<comments>http://derekneely.com/2009/05/image-gallery-plogger/#comments</comments>
		<pubDate>Mon, 25 May 2009 05:28:42 +0000</pubDate>
		<dc:creator>derek</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Gallery]]></category>
		<category><![CDATA[Images]]></category>
		<category><![CDATA[Plogger]]></category>

		<guid isPermaLink="false">http://derekneely.com/?p=174</guid>
		<description><![CDATA[DerekNeely.com needed an image gallery. Quick search result turned up Plogger. Thus far it&#8217;s been a really nice light-weight image gallery. Not much there ATM but feel free to stop by! DerekNeely.com &#8211; Image Gallery]]></description>
			<content:encoded><![CDATA[<p>DerekNeely.com needed an image gallery. Quick search result turned up Plogger. Thus far it&#8217;s been a really nice light-weight image gallery.</p>
<p>Not much there ATM but feel free to stop by! <a title="Image Gallery" href="http://gallery.derekneely.com/" target="_blank">DerekNeely.com &#8211; Image Gallery</a></p>
]]></content:encoded>
			<wfw:commentRss>http://derekneely.com/2009/05/image-gallery-plogger/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Icon Converter (iConvert)</title>
		<link>http://derekneely.com/2009/05/icon-converter/</link>
		<comments>http://derekneely.com/2009/05/icon-converter/#comments</comments>
		<pubDate>Sat, 23 May 2009 22:50:39 +0000</pubDate>
		<dc:creator>derek</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Icon]]></category>

		<guid isPermaLink="false">http://derekneely.com/?p=91</guid>
		<description><![CDATA[While working on a little Mac ScreenSaver/Locking work -around I found this little site for converting and image file to various icon formats: iConvert]]></description>
			<content:encoded><![CDATA[<p>While working on a little Mac ScreenSaver/Locking work -around I found this little site for converting and image file to various icon formats:</p>
<p><a title="iConvert" href="http://iconverticons.com" target="_blank">iConvert</a></p>
]]></content:encoded>
			<wfw:commentRss>http://derekneely.com/2009/05/icon-converter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

