<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>[iPhone developer:tips];</title>
	
	<link>http://iPhoneDeveloperTips.com</link>
	<description>Tips and Tricks for iPhone developers</description>
	<lastBuildDate>Sun, 08 Nov 2009 02:55:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/IphoneDeveloperTips" type="application/rss+xml" /><feedburner:emailServiceId>IphoneDeveloperTips</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
		<title>Book Giveaway: Head First iPhone Development – Winner Announcement</title>
		<link>http://feedproxy.google.com/~r/IphoneDeveloperTips/~3/HtSyAEawj1w/book-giveaway-head-first-iphone-development-winner-announcement.html</link>
		<comments>http://iPhoneDeveloperTips.com/giveaway/book-giveaway-head-first-iphone-development-winner-announcement.html#comments</comments>
		<pubDate>Fri, 06 Nov 2009 15:50:12 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Giveaway]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=4557</guid>
		<description><![CDATA[On Monday of this week I announced a new contest for the month of November &#8211; Each Friday iPhone Developer Tips will giveaway an ebook copy of O&#8217;Reilly&#8217;s Head First iPhone Development. 
The giveaway works like this:
Each Monday in November I’ll write a tip that is based on ideas and code in the book. Through [...]]]></description>
			<content:encoded><![CDATA[<p>On Monday of this week I <a href="   http://iphonedevelopertips.com/user-interface/adjust-textfield-hidden-by-keyboard.html">announced a new contest</a> for the month of November &#8211; Each Friday iPhone Developer Tips will giveaway an ebook copy of O&#8217;Reilly&#8217;s Head First iPhone Development. </p>
<p>The giveaway works like this:</p>
<p><a  target="_blank"  href="http://iphonedevelopertips.com/?wp_ct=12"><img border="0" class="alignleft" style="margin: 5px;" src="http://iphonedevelopertips.com/images/ads/Book-Headfirst.jpg" alt="Head First iPhone Development" /></a>Each Monday in November I’ll write a tip that is based on ideas and code in the book. Through out the week, you can register for the book giveaway by referring a friend to the tip. The deadline to enter the drawing for the week ends on Thursdays, at 6:00PM CST.</p>
<p>I will then draw a name from those who registered and announce the winner on each Friday in November<br />
<br/></p>
<h5>Winner for this week</h5>
<p>The winner of the eBook for this week is: Bill Mietelski &#8211; here&#8217;s a little more about Bill:</p>
<p><img border="0" class="alignleft" style="margin: 5px;"  src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/11/billm.png" /><br />
<em>&#8220;I&#8217;m currently a Software Engineer at a leading national academic medical center in the Chicagoland area, working on statistical research studies. I heard about your website &#038; the eBook giveaway on Twitter (via @OReillyMedia).&#8221;<br/><br/>&#8220;I don&#8217;t have an iPhone (yet) but I&#8217;m thinking about pulling the trigger soon. I&#8217;m a *huge* fan of O&#8217;Reilly&#8217;s Head First series of books, and I couldn&#8217;t think of a better way to get started than with a copy of Head First iPhone Development, so I entered the giveaway.&#8221;<br/><br/>&#8220;Now that I have the book, maybe you can have an actual iPhone giveaway :-)&#8221;<br />
</em></p>
<p>Bill sent me an impressive picture of his collection of O&#8217;Reilly books. I thought I had a nice collection, however, looks like I have some catching up to do.</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/11/books.jpg" /></p>
<p>Congratulations Bill!</p>
<p>Thank you to everyone who registered in the drawing. If you didn&#8217;t win this week, please visit iPhone Developer Tips again on Monday the 9th for another tip from Head First iPhone Development, and you can register in the drawing for Friday, November 13th.</p>
<img src="http://feeds.feedburner.com/~r/IphoneDeveloperTips/~4/HtSyAEawj1w" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/giveaway/book-giveaway-head-first-iphone-development-winner-announcement.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://iPhoneDeveloperTips.com/giveaway/book-giveaway-head-first-iphone-development-winner-announcement.html</feedburner:origLink></item>
		<item>
		<title>Download, Create and Display an Image from URL</title>
		<link>http://feedproxy.google.com/~r/IphoneDeveloperTips/~3/Qzf-0-mGxfE/download-and-create-an-image-from-a-url.html</link>
		<comments>http://iPhoneDeveloperTips.com/cocoa/download-and-create-an-image-from-a-url.html#comments</comments>
		<pubDate>Wed, 04 Nov 2009 08:07:19 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[uiimage]]></category>
		<category><![CDATA[uiimageview]]></category>
		<category><![CDATA[url]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=4476</guid>
		<description><![CDATA[This tip will show the steps to download and display an image from a remote resource. This is handy if you need to add an image as a subview, yet, the image is not part of your application bundle. 
URL to Remote Image
We start by creating a URL to the remote resource:

NSURL *url = &#91;NSURL [...]]]></description>
			<content:encoded><![CDATA[<p>This tip will show the steps to download and display an image from a remote resource. This is handy if you need to add an image as a subview, yet, the image is not part of your application bundle. </p>
<h5>URL to Remote Image</h5>
<p>We start by creating a URL to the remote resource:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>url <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span> 
   <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://iphonedevelopertips.com/images/logo-iphone-dev-tips.png&quot;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<h5>Create UIImage from NSData</h5>
<p>The next step is to build a <strong>UIImage</strong> using the data downloaded from the URL, which consists of an <strong>NSData</strong> object that holds the remote image contents:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">UIImage <span style="color: #002200;">*</span>image <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageWithData<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSData</span> dataWithContentsOfURL<span style="color: #002200;">:</span>url<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<h5>Putting it Together</h5>
<p>Here&#8217;s how to wrap it all together, adding the remote image as a subview to an existing view by creating a <strong>UIImageView</strong> from the above <strong>UIImage</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>url <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span> 
   <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://iphonedevelopertips.com/images/logo-iphone-dev-tips.png&quot;</span><span style="color: #002200;">&#93;</span>;
UIImage <span style="color: #002200;">*</span>image <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageWithData<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSData</span> dataWithContentsOfURL<span style="color: #002200;">:</span>url<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>; 
<span style="color: #002200;">&#91;</span>self.view addSubview<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIImageView alloc<span style="color: #002200;">&#93;</span> initWithImage<span style="color: #002200;">:</span>image<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<img src="http://feeds.feedburner.com/~r/IphoneDeveloperTips/~4/Qzf-0-mGxfE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/cocoa/download-and-create-an-image-from-a-url.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://iPhoneDeveloperTips.com/cocoa/download-and-create-an-image-from-a-url.html</feedburner:origLink></item>
		<item>
		<title>Book Giveaway: Head First iPhone Development – Adjust TextField Hidden by Keyboard</title>
		<link>http://feedproxy.google.com/~r/IphoneDeveloperTips/~3/DLA2nk9oEaQ/adjust-textfield-hidden-by-keyboard.html</link>
		<comments>http://iPhoneDeveloperTips.com/user-interface/adjust-textfield-hidden-by-keyboard.html#comments</comments>
		<pubDate>Mon, 02 Nov 2009 07:44:00 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[User Interface]]></category>
		<category><![CDATA[hidden]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[keyboard]]></category>
		<category><![CDATA[textfield]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=4423</guid>
		<description><![CDATA[Welcome to the first in a series of tips that are based on content from one of O&#8217;Reilly&#8217;s latest iPhone books, Head First iPhone Development, written by Dan and Tracey Pilone. Each Monday in November I&#8217;ll write a tip that is based on ideas and code in the book.
Book Giveaway &#8211; Every Friday in November
O&#8217;Reilly [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome to the first in a series of tips that are based on content from one of O&#8217;Reilly&#8217;s latest iPhone books, <a  target="_blank"  href="http://iphonedevelopertips.com/?wp_ct=12">Head First iPhone Development</a>, written by Dan and Tracey Pilone. Each Monday in November I&#8217;ll write a tip that is based on ideas and code in the book.</p>
<h5>Book Giveaway &#8211; Every Friday in November</h5>
<p>O&#8217;Reilly and iPhone Developer Tips are collaborating to <strong>give away a free ebook (electronic copy) of Head First iPhone Development</strong> each Friday in November. You can register in this weeks drawing by referring a friend to this tip &#8211; see the Refer-a-Friend button near the bottom of this post.<br />
<span id="more-4423"></span></p>
<h5>UITextField Hidden by Keyboard</h5>
<p>Have you ever had one or more textfields on screen, that when tapped, become hidden by the on-screen keyboard? This tip will show an example of how to adjust textfields that are contained within a scrollview such that they are always visible, even when entering text. </p>
<p><a  target="_blank"  href="http://iphonedevelopertips.com/?wp_ct=12"><img border="0" class="alignleft" style="margin: 5px;" src="http://iphonedevelopertips.com/images/ads/Book-Headfirst.jpg" alt="Head First iPhone Development" /></a> The inspiration for this tip comes from Chapter 6, Saving, Editing and Sorting Data. The code example in the book is created over a series of chapters &#8211; to provide a complete working example for this tip, I&#8217;ll write a small working example from the ground up that you can download and build using Xcode. I&#8217;ll write all the code for the UI without Interface Builder, which will provide a little more insight into how all the pieces fit together.</p>
<h5>The UI Problem</h5>
<p>Let&#8217;s get started &#8211; When working with UITextFields, depending on the layout of your screen, good chance the keyboard will overlay your textfield when a user taps on the field to start editing. For example, the screenshot below shows two textfields, both displayed within a scrollview.</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/11/keyboard1.png" /></p>
<p>When the user selects either textfield, the keyboard will appear, trouble is, the keyboard may overlay the textfields as shown here:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/11/keyboard2.png" /></p>
<h5>Scrollview with Two UITextFields</h5>
<p>For this code example, I&#8217;ll wrap two UITextFields inside a scrollview. When the keyboard is shown, the scrollview size is adjust based on the size of the keyboard, allowing the textfield selected to be visible on the display. </p>
<p>Let&#8217;s look at the view controller that will contain the textfields and the scrollview:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> test_appViewController <span style="color: #002200;">:</span> UIViewController  &lt;UIScrollViewDelegate, UITextFieldDelegate&gt;
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">BOOL</span>           keyboardVisible;
  CGPoint        offset;
  UIScrollView  <span style="color: #002200;">*</span>scrollview;
  UITextField    <span style="color: #002200;">*</span>textfield;
  UITextField    <span style="color: #002200;">*</span>textfield2;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The variable <strong>keyboardVisible</strong> will keep the state of whether or not the keyboard is visible. The variable <strong>offset</strong> will keep track of current offset of the scrollview, before the keyboard is shown, so when the keyboard is dismissed, we can return the scrollview to its original position.</p>
<h5>UI Layout</h5>
<p>The code that follows show the layout of the user interface, a view that contains a scrollview, which in turn, contains two textfields.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#define SCROLLVIEW_HEIGHT 460</span>
<span style="color: #6e371a;">#define SCROLLVIEW_WIDTH  320</span>
&nbsp;
<span style="color: #6e371a;">#define SCROLLVIEW_CONTENT_HEIGHT 720</span>
<span style="color: #6e371a;">#define SCROLLVIEW_CONTENT_WIDTH  320</span>
&nbsp;
...
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>init
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> 
  <span style="color: #002200;">&#123;</span>
    self.view <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIView alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIScreen mainScreen<span style="color: #002200;">&#93;</span> applicationFrame<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    self.view.backgroundColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor whiteColor<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Create scrollview</span>
    scrollview <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIScrollView alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;      
    scrollview.contentSize <span style="color: #002200;">=</span> CGSizeMake<span style="color: #002200;">&#40;</span>SCROLLVIEW_CONTENT_WIDTH, SCROLLVIEW_CONTENT_HEIGHT<span style="color: #002200;">&#41;</span>;
    scrollview.frame <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span>, SCROLLVIEW_WIDTH, SCROLLVIEW_HEIGHT<span style="color: #002200;">&#41;</span>;
    scrollview.scrollsToTop <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
    scrollview.delegate <span style="color: #002200;">=</span> self;
    <span style="color: #002200;">&#91;</span>self.view addSubview<span style="color: #002200;">:</span>scrollview<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Create textfield 1</span>
    textfield <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITextField alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">20</span>, <span style="color: #2400d9;">240</span>, <span style="color: #2400d9;">280</span>, <span style="color: #2400d9;">30</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
    textfield.placeholder <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Textfield 1&quot;</span>;
    textfield.delegate <span style="color: #002200;">=</span> self;
    textfield.returnKeyType <span style="color: #002200;">=</span> UIReturnKeyDone;
    textfield.borderStyle <span style="color: #002200;">=</span> UITextBorderStyleBezel;
    textfield.enablesReturnKeyAutomatically <span style="color: #002200;">=</span> TRUE;
    <span style="color: #002200;">&#91;</span>scrollview addSubview<span style="color: #002200;">:</span>textfield<span style="color: #002200;">&#93;</span>;  
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Create textfield 2</span>
    textfield2 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITextField alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">20</span>, <span style="color: #2400d9;">290</span>, <span style="color: #2400d9;">280</span>, <span style="color: #2400d9;">30</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
    textfield2.placeholder <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Textfield 2&quot;</span>;
    textfield2.delegate <span style="color: #002200;">=</span> self;
    textfield2.returnKeyType <span style="color: #002200;">=</span> UIReturnKeyDone;
    textfield2.borderStyle <span style="color: #002200;">=</span> UITextBorderStyleBezel;
    textfield2.enablesReturnKeyAutomatically <span style="color: #002200;">=</span> TRUE;
    <span style="color: #002200;">&#91;</span>scrollview addSubview<span style="color: #002200;">:</span>textfield2<span style="color: #002200;">&#93;</span>;  
  <span style="color: #002200;">&#125;</span>
&nbsp;
  <span style="color: #a61390;">return</span> self;  
<span style="color: #002200;">&#125;</span></pre></div></div>

<h5>Keyboard Notifications</h5>
<p>The magic behind all of this begins by registering to receive notifications when the keyboard is shown and when it is hidden. You can read more about notifications on this iPhone Developer Tip: <a  target="_blank"  href="http://iphonedevelopertips.com/cocoa/basics-of-notifications.html">Basics of Notifications</a>.</p>
<p>To add the notifications for the keyboard, here&#8217;s all we need to do:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> addObserver<span style="color: #002200;">:</span>self
   selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span> <span style="color: #002200;">&#40;</span>keyboardDidShow<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span>
   name<span style="color: #002200;">:</span> UIKeyboardDidShowNotification object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> addObserver<span style="color: #002200;">:</span>self 
   selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span> <span style="color: #002200;">&#40;</span>keyboardDidHide<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span>
	name<span style="color: #002200;">:</span> UIKeyboardDidHideNotification object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>These calls request that the methods <strong>keyboardDidShow</strong> and <strong>keyboardDidHide</strong> be called when the notifications <strong>UIKeyboardDidShowNotification</strong> and <strong>UIKeyboardDidHideNotification</strong>, respectively, are fired.</p>
<h5>Keyboard Visible Code</h5>
<p>The OS will trigger the notification <strong>UIKeyboardDidShowNotification</strong> when a user taps in a textfield, which will in turn call the method shown below. The first check is to see if the keyboard is currently visible, if so, we&#8217;re done. If not, access the dictionary for the notification so we can get to the keyboard size &#8211; this is important as the keyboard size can vary depending on the textfield.</p>
<p>With the keyboard size known, we can now adjust (shrink) the scrollview frame so the keyboard and scrollview share the screen.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> keyboardDidShow<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNotification</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>notif 
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// If keyboard is visible, return</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>keyboardVisible<span style="color: #002200;">&#41;</span> 
  <span style="color: #002200;">&#123;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Keyboard is already visible. Ignoring notification.&quot;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">return</span>;
  <span style="color: #002200;">&#125;</span>
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Get the size of the keyboard.</span>
  <span style="color: #400080;">NSDictionary</span><span style="color: #002200;">*</span> info <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>notif userInfo<span style="color: #002200;">&#93;</span>;
  <span style="color: #400080;">NSValue</span><span style="color: #002200;">*</span> aValue <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>info objectForKey<span style="color: #002200;">:</span>UIKeyboardBoundsUserInfoKey<span style="color: #002200;">&#93;</span>;
  CGSize keyboardSize <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>aValue CGRectValue<span style="color: #002200;">&#93;</span>.size;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Save the current location so we can restore</span>
  <span style="color: #11740a; font-style: italic;">// when keyboard is dismissed</span>
  offset <span style="color: #002200;">=</span> scrollview.contentOffset;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Resize the scroll view to make room for the keyboard</span>
  CGRect viewFrame <span style="color: #002200;">=</span> scrollview.frame;
  viewFrame.size.height <span style="color: #002200;">-=</span> keyboardSize.height;
  scrollview.frame <span style="color: #002200;">=</span> viewFrame;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Keyboard is now visible</span>
  keyboardVisible <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>One line of code to point is line 17, where we save the current offset of the scrollview so we can restore the position once the keyboard is hidden.</p>
<h5>Keyboard Hidden Code</h5>
<p>Upon dismissing the keyboard, the code below will be called. The basic idea is to reset the size of the scrollview to its original size, and follow this by updating the scrollview offset to where it was prior to the keyboard being shown.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> keyboardDidHide<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNotification</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>notif 
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Is the keyboard already shown</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>keyboardVisible<span style="color: #002200;">&#41;</span> 
  <span style="color: #002200;">&#123;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Keyboard is already hidden. Ignoring notification.&quot;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">return</span>;
  <span style="color: #002200;">&#125;</span>
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Reset the height of the scroll view to its original value</span>
  scrollview.frame <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span>, SCROLLVIEW_WIDTH, SCROLLVIEW_HEIGHT<span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Reset the scrollview to previous location</span>
  scrollview.contentOffset <span style="color: #002200;">=</span> offset;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Keyboard is no longer visible</span>
  keyboardVisible <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;	
<span style="color: #002200;">&#125;</span></pre></div></div>

<h5>Final User Interface</h5>
<p>The end result is shown below &#8211; when the keyboard is shown, the scrollview is adjusted to the textfields are visible.</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/11/keyboard3.png" /></p>
<h5>Xcode Project Source Code</h5>
<p>You can download the Xcode project with source code <a href="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/11/Xcode-AdjustTextfield.zip">here</a>.</p>
<h5>Book Giveaway</h5>
<p>Sorry, the drawing for the free ebook is now closed.</p>
<img src="http://feeds.feedburner.com/~r/IphoneDeveloperTips/~4/DLA2nk9oEaQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/user-interface/adjust-textfield-hidden-by-keyboard.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://iPhoneDeveloperTips.com/user-interface/adjust-textfield-hidden-by-keyboard.html</feedburner:origLink></item>
		<item>
		<title>iPhone File System: Creating, Renaming and Deleting Files</title>
		<link>http://feedproxy.google.com/~r/IphoneDeveloperTips/~3/UrbdbMkBF4k/iphone-file-system-creating-renaming-and-deleting-files.html</link>
		<comments>http://iPhoneDeveloperTips.com/data-file-management/iphone-file-system-creating-renaming-and-deleting-files.html#comments</comments>
		<pubDate>Tue, 27 Oct 2009 11:04:55 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Data / File Management]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=4380</guid>
		<description><![CDATA[The iPhone provides a comprehensive set of operations for working with files and directories. NSFileManager includes methods for querying the contents of directories, creating, renaming and deleting contents, as well as getting/setting file attributes (readable, writeable, etc).
Point to the Documents Directory
Each application has its own sandbox in which you can read/write files. Files written to [...]]]></description>
			<content:encoded><![CDATA[<p>The iPhone provides a comprehensive set of operations for working with files and directories. <strong>NSFileManager</strong> includes methods for querying the contents of directories, creating, renaming and deleting contents, as well as getting/setting file attributes (readable, writeable, etc).</p>
<h5>Point to the Documents Directory</h5>
<p>Each application has its own sandbox in which you can read/write files. Files written to the sandbox are persistent across invocations of the application, including across application updates.</p>
<p>You can locate the Documents directory in the sandbox as shown below:<br />
<span id="more-4380"></span></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// For error information</span>
<span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Create file manager</span>
<span style="color: #400080;">NSFileManager</span> <span style="color: #002200;">*</span>fileMgr <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFileManager</span> defaultManager<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Point to Document directory</span>
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>documentsDirectory <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>NSHomeDirectory<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> 
         stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Documents&quot;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<h5>Creating a File</h5>
<p>With the documents directory available, we can now use that path to create a new file in the sandbox and write a few lines of text:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// File we want to create in the documents directory </span>
<span style="color: #11740a; font-style: italic;">// Result is: /Documents/file1.txt</span>
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>filePath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>documentsDirectory 
         stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;file1.txt&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// String to write</span>
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>str <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;iPhone Developer Tips<span style="color: #2400d9;">\n</span>http://iPhoneDevelopTips,com&quot;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Write the file</span>
<span style="color: #002200;">&#91;</span>str writeToFile<span style="color: #002200;">:</span>filePath atomically<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span> 
         encoding<span style="color: #002200;">:</span>NSUTF8StringEncoding error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Show contents of Documents directory</span>
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Documents directory: %@&quot;</span>,
         <span style="color: #002200;">&#91;</span>fileMgr contentsOfDirectoryAtPath<span style="color: #002200;">:</span>documentsDirectory error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>We build a path to the file we want to create (file1.txt), initialize a string to write into the file, and write out the contents. The last line shows a directory listing of what is in the Documents directory after we create the file, see the figure below:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/10/filemgr1.png" /></p>
<h5>Renaming a File</h5>
<p>To rename a file we move the file to a new path. The code below creates the destination path we are after, requests to move the file, and shows the Documents directory after the move.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Rename the file, by moving the file</span>
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>filePath2 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>documentsDirectory 
                                 stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;file2.txt&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Attempt the move</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>fileMgr moveItemAtPath<span style="color: #002200;">:</span>filePath toPath<span style="color: #002200;">:</span>filePath2 error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span> <span style="color: #002200;">!=</span> <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Unable to move file: %@&quot;</span>, <span style="color: #002200;">&#91;</span>error localizedDescription<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Show contents of Documents directory</span>
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Documents directory: %@&quot;</span>, 
         <span style="color: #002200;">&#91;</span>fileMgr contentsOfDirectoryAtPath<span style="color: #002200;">:</span>documentsDirectory error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>After moving the file, the output should look similar to the image below:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/10/filemgr2.png" /></p>
<h5>Deleting a File</h5>
<p>To round out this tip, let&#8217;s look at how to delete a file:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Attempt to delete the file at filePath2</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>fileMgr removeItemAtPath<span style="color: #002200;">:</span>filePath2 error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span> <span style="color: #002200;">!=</span> <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Unable to delete file: %@&quot;</span>, <span style="color: #002200;">&#91;</span>error localizedDescription<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Show contents of Documents directory</span>
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Documents directory: %@&quot;</span>,
         <span style="color: #002200;">&#91;</span>fileMgr contentsOfDirectoryAtPath<span style="color: #002200;">:</span>documentsDirectory error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>Once the file is deleted, as expected, the Documents directory is now empty:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/10/filemgr3.png" /></p>
<p>These examples touch the surface of working with files. Check out the documentation for <strong>NSFileManager</strong> for all the specifics.</p>
<img src="http://feeds.feedburner.com/~r/IphoneDeveloperTips/~4/UrbdbMkBF4k" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/data-file-management/iphone-file-system-creating-renaming-and-deleting-files.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://iPhoneDeveloperTips.com/data-file-management/iphone-file-system-creating-renaming-and-deleting-files.html</feedburner:origLink></item>
		<item>
		<title>Vimov and iSimulate – iPhone Developer Tool</title>
		<link>http://feedproxy.google.com/~r/IphoneDeveloperTips/~3/LSRCO1oAT4M/vimov-and-isimulate.html</link>
		<comments>http://iPhoneDeveloperTips.com/tools/vimov-and-isimulate.html#comments</comments>
		<pubDate>Fri, 23 Oct 2009 07:08:28 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=4346</guid>
		<description><![CDATA[I&#8217;m happy to announce a new sponsor of iPhone Developer Tips, vimov, the company behind an excellent developer tool, iSimulate.
In a previous post I wrote about my experience using iSimulate in an iPhone project I was working on at that time: Testing Multi-Touch, Accelerometer and GPS using the Xcode Simulator. In addition iPhone Developer Tips [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m happy to announce a new sponsor of iPhone Developer Tips, <a href="http://www.vimov.com/" target="_blank">vimov</a>, the company behind an excellent developer tool, <a href="http://iphonedevelopertips.com/?wp_ct=15">iSimulate</a>.</p>
<p>In a previous post I wrote about my experience using iSimulate in an iPhone project I was working on at that time: <a href=" http://iphonedevelopertips.com/tools/isimulate-testing-multi-touch-events-accelerometer-movements-using-xcode-simulator.html" target="_blank">Testing Multi-Touch, Accelerometer and GPS using the Xcode Simulator</a>. In addition iPhone Developer Tips hosted a  contest for a free copy of iSimulate: <a href="http://iphonedevelopertips.com/giveaway/isimulate-giveaway-we-have-a-winner.html" target="_blank">iSimulate Giveaway</a>.</p>
<p>In a nutshell, iSimulate is an iPhone application that sends multi-touch events, accelerometer data and GPS location information to the iPhone Simulator wirelessly, which is very handy, as the Xcode simulator does not offer such support to exercise your code.</p>
<p><a href="http://iphonedevelopertips.com/?wp_ct=15"><img border="0" class="alignleft" style="margin: 5px;" src="http://iphonedevelopertips.com/images/ads/isimulate125x125.png" alt="iSimulate" width="125" height="125" /></a>The latest release of iSimulate, includes support for streaming your application as video to a device as well as recognizing orientation changes and notifying your application running in the iPhone Simulator.</p>
<p>iSimulate consists of an SDK library, which is free, that creates a listening server in the simulator that communicates with an iPhone/iPod running the iSimulate client. The client captures all data from the accelerometer sensor, the touch events, the location and device ID and streams them to the server. The iSimulate client is available for purchase <a href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=306908756&amp;mt=8" target="_blank">on the App Store</a>.</p>
<p>iSimulate is a great addition to any iPhone Developer&#8217;s toolbox.</p>
<img src="http://feeds.feedburner.com/~r/IphoneDeveloperTips/~4/LSRCO1oAT4M" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/tools/vimov-and-isimulate.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://iPhoneDeveloperTips.com/tools/vimov-and-isimulate.html</feedburner:origLink></item>
		<item>
		<title>UIActionSheet to Display Text Only Messages</title>
		<link>http://feedproxy.google.com/~r/IphoneDeveloperTips/~3/BnbOwjkA49o/uiactionsheet-to-display-text-only-messages.html</link>
		<comments>http://iPhoneDeveloperTips.com/general/uiactionsheet-to-display-text-only-messages.html#comments</comments>
		<pubDate>Wed, 21 Oct 2009 11:58:30 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=4321</guid>
		<description><![CDATA[UIActionSheet offers a really handy basic menu system, where each button presents an option for the user to choose from. For example, the figure below shows how I am using UIActionSheet to show three menu options for a shopping list view I am working on.


I&#8217;ve found another good use for UIActionSheet, to show text messages [...]]]></description>
			<content:encoded><![CDATA[<p><strong>UIActionSheet</strong> offers a really handy basic menu system, where each button presents an option for the user to choose from. For example, the figure below shows how I am using <strong>UIActionSheet</strong> to show three menu options for a shopping list view I am working on.</p>
<p><img width="200" src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/10/menu1.png" /><br />
<span id="more-4321"></span></p>
<p>I&#8217;ve found another good use for <strong>UIActionSheet</strong>, to show text messages with minimal coding and overhead. You might use this idea to show content sensitive help text, or maybe display a message that is shown only once upon the first run of an application.</p>
<p>You can get an idea of the look that I&#8217;m after from the figure below. In this example I&#8217;m showing nothing more than three paragraphs of text and a button to dismiss the view.</p>
<p><img width="200" src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/10/menu2.png" /></p>
<p>It&#8217;s almost embarrassing to show the code as there&#8217;s really nothing to it:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">    UIActionSheet <span style="color: #002200;">*</span>msg <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIActionSheet alloc<span style="color: #002200;">&#93;</span> 
        initWithTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Lorem ipsum dolor...text goes here&quot;</span> 
        delegate<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> 
        cancelButtonTitle<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span>  destructiveButtonTitle<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> 
        otherButtonTitles<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Okay&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span> 
        autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>msg showInView<span style="color: #002200;">:</span>window<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>We have no need for a delegate in this case, and by setting all but the &#8220;Okay&#8221; button to nil, we have just one button.</p>
<p>The beauty of using a <strong>UIActionSheet</strong> for this type of information is that presentation is handled for you (sliding into place), all other UI elements are obscured (a modal presentation) and it&#8217;s ridiculously simple to implement.</p>
<img src="http://feeds.feedburner.com/~r/IphoneDeveloperTips/~4/BnbOwjkA49o" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/general/uiactionsheet-to-display-text-only-messages.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://iPhoneDeveloperTips.com/general/uiactionsheet-to-display-text-only-messages.html</feedburner:origLink></item>
		<item>
		<title>Encapsulating Begin Ignoring Events and End Ignoring Events</title>
		<link>http://feedproxy.google.com/~r/IphoneDeveloperTips/~3/WHX1lacAvKE/encapsulating-begin-ignoring-events-and-end-ignoring-events.html</link>
		<comments>http://iPhoneDeveloperTips.com/event-handling/encapsulating-begin-ignoring-events-and-end-ignoring-events.html#comments</comments>
		<pubDate>Mon, 19 Oct 2009 07:53:52 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Event Handling]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=4301</guid>
		<description><![CDATA[There are times when it&#8217;s really handy to stop event processing momentarily so you can perform an update to the UI (e.g. sliding a custom view into place using a timed animation) or you otherwise need to save the user from themselves by momentarily stopping touch events.
I&#8217;ve written previously on How to Suspend / Ignore [...]]]></description>
			<content:encoded><![CDATA[<p>There are times when it&#8217;s really handy to stop event processing momentarily so you can perform an update to the UI (e.g. sliding a custom view into place using a timed animation) or you otherwise need to save the user from themselves by momentarily stopping touch events.</p>
<p>I&#8217;ve written previously on <a  target="_blank"  href="http://iphonedevelopertips.com/event-handling/suspend-touch-events.html">How to Suspend / Ignore Touch Events</a>, which is as simple as:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Begin ignoring events</span>
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplicationsharedApplication<span style="color: #002200;">&#93;</span> beginIgnoringInteractionEvents<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Stop ignoring events</span>
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplicationsharedApplication<span style="color: #002200;">&#93;</span> endIgnoringInteractionEvents<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p><span id="more-4301"></span><br />
The problem with the above is that depending on when and where you make these calls, you can end up with the lines sprinkled about inside your code, which can make it troublesome to match one call with the other.</p>
<p>For example, if I make a call to begin ignoring events, and then move to a new view in which I then make a request to stop ignoring events, I now have the begin ignorning call in a different class from the end ignorning call. A big deal? Definitely not, however, when you see one call without the other, it can be both hard to know if you have a matching set, not to mention the questions that may arise when you revisit your code a few months down the road.</p>
<p>I get around this by writing a very simple set of methods for stopping and stopping the ignore events process. Here&#8217;s how this looks:</p>
<h5>Interface Definition for SomeClass</h5>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> SomeClass <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> 
<span style="color: #002200;">&#123;</span>
  ...
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>enableEvents;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>startIgnoringEvents;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>stopIgnoringEventsAfter<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>seconds;</pre></div></div>

<p>Notice that the three methods shown are class methods. The implementation for SomeClass follows:</p>
<h5>Implementation for SomeClass</h5>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@implementation</span> SomeClass
&nbsp;
...
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>enableEvents
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span> endIgnoringInteractionEvents<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>startIgnoringEvents
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span> beginIgnoringInteractionEvents<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>stopIgnoringEventsAfter<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>seconds
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span>self performSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>enableEvents<span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> afterDelay<span style="color: #002200;">:</span>seconds<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<h5>How to Start and Stop Ingoring Events</h5>
<p>Now that I have these helper methods, I can keep all references to start and stop ignoring of events in one place, here&#8217;s how this might look:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Stop events momentarily...</span>
<span style="color: #002200;">&#91;</span>SomeClass startIgnoringEvents<span style="color: #002200;">&#93;</span>;    
&nbsp;
<span style="color: #11740a; font-style: italic;">// This is the activity I don't want interrupted</span>
<span style="color: #002200;">&#91;</span>dropDownViewController showMsg<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Stop ignoring events</span>
<span style="color: #002200;">&#91;</span>ShoppingListViewController stopIgnoringEventsAfter<span style="color: #002200;">:</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Again, these extra steps are not required, however, keeping related code together can be a good thing.</p>
<img src="http://feeds.feedburner.com/~r/IphoneDeveloperTips/~4/WHX1lacAvKE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/event-handling/encapsulating-begin-ignoring-events-and-end-ignoring-events.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://iPhoneDeveloperTips.com/event-handling/encapsulating-begin-ignoring-events-and-end-ignoring-events.html</feedburner:origLink></item>
		<item>
		<title>Killer Xcode Tips and Tricks – Tips 1 to 10</title>
		<link>http://feedproxy.google.com/~r/IphoneDeveloperTips/~3/3CDh0IOfm_E/xcode-tips-and-tricks-tips-1-to-10.html</link>
		<comments>http://iPhoneDeveloperTips.com/xcode/xcode-tips-and-tricks-tips-1-to-10.html#comments</comments>
		<pubDate>Fri, 16 Oct 2009 13:33:12 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Xcode]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=4221</guid>
		<description><![CDATA[My world is surrounded by sticky notes, both electronic and paper, doing my best to keep track of Xcode shortcuts, tips and tricks. I strive to use the mouse as little as possible when coding, and without these tips I&#8217;d be lost. What better place to keep track of this stuff than to write about [...]]]></description>
			<content:encoded><![CDATA[<p>My world is surrounded by sticky notes, both electronic and paper, doing my best to keep track of Xcode shortcuts, tips and tricks. I strive to use the mouse as little as possible when coding, and without these tips I&#8217;d be lost. What better place to keep track of this stuff than to write about them here.</p>
<p>This is the first post of what I hope is many where we can share tips and tricks on working with Xcode.<br />
<span id="more-4221"></span></p>
<h5>Tip #1 &#8211; Split Editor View Vertically</h5>
<p>If you like to have multiple code windows open at once, the Split Editor option is your friend (see the little square grid icon shown below).</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/10/split1.png" /></p>
<p>By default, the windows are split horizontally. If you prefer to view your code side-by-side (nice for comparing), here&#8217;s how to tell the split to be vertical</p>
<blockquote>
<ol>
<li>Hold down <strong>Option</strong> key when clicking the Split Window icon</li>
</ol>
</blockquote>
<h5>Tip #2 &#8211; Comment Out a Block of Code </h5>
<p>You can quickly comment out a block of code as follows:</p>
<blockquote>
<ol>
<li>Select one or more lines of code to comment</li>
<li><strong>Command-/</strong></li>
</ol>
</blockquote>
<p>To uncomment a block of code, repeat the steps above.</p>
<h5>Tip #3 &#8211; Toggle Between .h and .m Files (aka Switch to Counterpart)</h5>
<p>Within your current code window, you can swap between the .h and .m file as follows:</p>
<blockquote>
<ol>
<li><strong>Command-Option Up-Arrow</strong></li>
</ol>
</blockquote>
<h5>Tip #4 &#8211; Map Keys to Actions (Key Bindings)</h5>
<p>The Switch to Counterpart tip above is a real time saver. However, I often find that if I map logical key-strokes to actions I&#8217;m much more likely to get into the habit of using them. For example, I mapped the Switch to Counterpart to <strong>Option-S</strong>, as in Swap or Switch. </p>
<p>Here&#8217;s how to set key bindings in Xcode</p>
<blockquote>
<ol>
<li>From the Xcode menu choose <strong>Prefereces</strong></li>
<li>Select <strong>Key Bindings</strong></li>
<li>Click on one of the <strong>Actions</strong> in the list</li>
<li>Tap on the <strong>Keys</strong> column on the right</li>
<li>Enter the keystroke you want to map to the action</li>
<li>Click <strong>Ok</strong> to save your changes</li>
</ol>
</blockquote>
<h5>Tip #5 &#8211; Jump to API Documentation</h5>
<p>Showing relevant API documentation for anything within the SDK(s) is as simple as:</p>
<blockquote>
<ol>
<li><strong>Option Double-Click</strong> on relevant code </li>
</ol>
</blockquote>
<p>For example, in the screenshot below, after Option double-clicking on <strong>UIToolbar</strong>, a popup window shows a summary of the class information.</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/10/toolbar.png" /></p>
<h5>Tip #6 &#8211; Traverse File History</h5>
<p>As you open and edit various files, Xcode keeps a history list of your actions, not unlike when using a web-browser. You can move through the list using the directional arrows as shown below:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/10/history.png" /></p>
<p>The keystoke equivalent for the above is:</p>
<blockquote>
<ol>
<li><strong>Option-Command Left-Arrow</strong> goto to previous file</li>
<li><strong>Option-Command Right-Arrow</strong> goto to next file</li>
</ol>
</blockquote>
<h5>Tip #7 &#8211; Set a Bookmark</h5>
<p>I can&#8217;t imagine coding without having the option to set a bookmark as a placeholder for what I&#8217;m working on. I do this regularly when I need to segue to another file to find a snippet or otherwise poke around outside the current file I&#8217;m working on.</p>
<p>Setting a bookmark is as simple as:</p>
<blockquote>
<ol>
<li><strong>Control-D</strong></li>
<li>Enter a name for the bookmark</li>
</ol>
</blockquote>
<h5>Tip #8 &#8211; Jump to Bookmark</h5>
<p>There are two options to jump to a bookmark. First, you can select the bookmark icon in the upper right corner of the Editor window.</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/10/bookmarks.png" /></p>
<p>You can bring up the same menu as shown in the above figure using this keystoke:</p>
<blockquote>
<ol>
<li><strong>Control-4</strong></li>
</ol>
</blockquote>
<h5>Tip #9 &#8211; Indent / Un-indent Code</h5>
<p>You can indent a line of code or a selected block of code as follows:</p>
<blockquote>
<ol>
<li><strong>Command-[</strong> move code left</li>
<li><strong>Command-]</strong> move code right</li>
</ol>
</blockquote>
<p>The above works regardless of where you are in a line of code, in other words, you don&#8217;t have to be at the beginning of the line.</p>
<h5>Tip #10 &#8211; Zoom Editor</h5>
<p>You can toggle between Detail view and Editor view by clicking the Editor button as shown here:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/10/zoomeditor.png" /></p>
<p>The keystroke equivalent of the above is:</p>
<blockquote>
<ol>
<li><strong>Shift-Command-E</strong></li>
</ol>
</blockquote>
<h5>Have an Xcode Tip to Share?</h5>
<p>If you have a favorite tip or trick, please post a comment or <a href="http://iphonedevelopertips.com/contact">send tips to me here</a>. <strong>Killer Xcode Tips and Tricks – Tips 11 to 20</strong> will be posted next week, and suggestions are welcome!</p>
<img src="http://feeds.feedburner.com/~r/IphoneDeveloperTips/~4/3CDh0IOfm_E" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/xcode/xcode-tips-and-tricks-tips-1-to-10.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://iPhoneDeveloperTips.com/xcode/xcode-tips-and-tricks-tips-1-to-10.html</feedburner:origLink></item>
		<item>
		<title>How Long was a Touch Event – Detecting When a Touch Event Starts and Ends</title>
		<link>http://feedproxy.google.com/~r/IphoneDeveloperTips/~3/71P6ilzkroE/how-long-was-a-touch-event-detecting-when-a-touch-event-starts-and-ends.html</link>
		<comments>http://iPhoneDeveloperTips.com/event-handling/how-long-was-a-touch-event-detecting-when-a-touch-event-starts-and-ends.html#comments</comments>
		<pubDate>Wed, 14 Oct 2009 08:43:41 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Event Handling]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=4201</guid>
		<description><![CDATA[Here&#8217;s a quick tip that shows how to determine the length of time from a begin touch event to an end touch event. I used a similar snippet of code recently when I had to determine if a user tapped on an image or held their finger on the image for a pre-determined amount of [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a quick tip that shows how to determine the length of time from a begin touch event to an end touch event. I used a similar snippet of code recently when I had to determine if a user tapped on an image or held their finger on the image for a pre-determined amount of time.</p>
<p>Start by creating a variable of type <strong>NSTimeInterval</strong> to hold the time the touch event began.<br />
<span id="more-4201"></span></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Define this as an instance variable</span>
NSTimeInterval touchStartTime;
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>touchesBegan<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>touches withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>event;
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// When the touch event was detected</span>
  touchStartTime <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>event timestamp<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Inside the <strong>touchesBegan</strong> event, with some simple math, one can determine how long from the start event to the end.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>touchesEnded<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>touches withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>event;
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Calculate how long touch lasted</span>
  NSTimeInterval touchTimeDuration <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>event timestamp<span style="color: #002200;">&#93;</span> <span style="color: #002200;">-</span> touchStartTime;
&nbsp;
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Touch duration: %3.2f seconds&quot;</span>, touchTimeDuration<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<img src="http://feeds.feedburner.com/~r/IphoneDeveloperTips/~4/71P6ilzkroE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/event-handling/how-long-was-a-touch-event-detecting-when-a-touch-event-starts-and-ends.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://iPhoneDeveloperTips.com/event-handling/how-long-was-a-touch-event-detecting-when-a-touch-event-starts-and-ends.html</feedburner:origLink></item>
		<item>
		<title>Where Does Xcode Simulator Write Files?</title>
		<link>http://feedproxy.google.com/~r/IphoneDeveloperTips/~3/7QUGEcUV5IQ/where-does-xcode-simulator-write-files.html</link>
		<comments>http://iPhoneDeveloperTips.com/xcode/where-does-xcode-simulator-write-files.html#comments</comments>
		<pubDate>Mon, 12 Oct 2009 09:33:43 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Xcode]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=4166</guid>
		<description><![CDATA[When working with Xcode and running apps in the Simulator, looking at files written to your system is often a necessity to verify all is well. For example, I&#8217;m working on a project at the moment that requires updating a plist (XML file) when the application exits. Once the content is written to the plist, [...]]]></description>
			<content:encoded><![CDATA[<p>When working with Xcode and running apps in the Simulator, looking at files written to your system is often a necessity to verify all is well. For example, I&#8217;m working on a project at the moment that requires updating a plist (XML file) when the application exits. Once the content is written to the plist, as part of the test cycle I need to open the file and make sure the content is written and updated as I expect.<br />
<span id="more-4166"></span></p>
<h5>Path to Xcode Simulator Files</h5>
<p>The path to the simulator files is here:</p>
<p><strong><span style="color: #5615d5;">/Users/<em>USER_NAME</em>/Library/Application Support/iPhone Simulator/User/Applications</span><br />
</strong></p>
<p><strong>USER_NAME</strong> is the current user logged in on the machine. There is a unique identifier created by the simulator, one value for each application. The figure below shows how this looks on my machine, notice there are two entries in the <strong>Applications</strong> folder, one for each of the current applications I am working on.</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/10/xcode1.png" alt="" /></p>
<p>In the application <strong>App-Vol1</strong> you can see that I am creating a custom plist file <strong>Favorites.plist</strong> and storing the file in the <strong>Documents</strong> directory. This is the file I referred to earlier that I update whenever the user quits the application.</p>
<h5>Easy Access to Simulator Files</h5>
<p>To make access to the simulator files quick and easy, I keep an entry on the left side of Finder titled <strong>iPhone Simulator</strong>. Simply navigate your way to the simulator files and drag/drop the folder on the left sidebar. You can rename this folder by right-clicking and choosing <strong>Rename</strong>.</p>
<img src="http://feeds.feedburner.com/~r/IphoneDeveloperTips/~4/7QUGEcUV5IQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/xcode/where-does-xcode-simulator-write-files.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://iPhoneDeveloperTips.com/xcode/where-does-xcode-simulator-write-files.html</feedburner:origLink></item>
	</channel>
</rss>
