<?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/" version="2.0">

<channel>
	<title>iPhone Tutorial | iPhone iOS4 iPad SDK Development &amp; Programming Blog</title>
	
	<link>http://www.edumobile.org/iphone</link>
	<description />
	<lastBuildDate>Sat, 25 May 2013 09:51:14 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/TheIphoneDeveloperResource" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="theiphonedeveloperresource" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Map view with annotations and its views</title>
		<link>http://www.edumobile.org/iphone/iphone-programming-tutorials/map-view-with-annotations-and-its-views/</link>
		<comments>http://www.edumobile.org/iphone/iphone-programming-tutorials/map-view-with-annotations-and-its-views/#comments</comments>
		<pubDate>Sat, 25 May 2013 09:51:14 +0000</pubDate>
		<dc:creator>Sushant</dc:creator>
				<category><![CDATA[Create an app]]></category>
		<category><![CDATA[iPhone Programming Tutorials]]></category>
		<category><![CDATA[iphone tutorials]]></category>
		<category><![CDATA[Map view with annotations and its views]]></category>
		<category><![CDATA[Map view with annotations and its views iphone]]></category>
		<category><![CDATA[Map view with annotations example]]></category>
		<category><![CDATA[Map view with annotations iphone]]></category>

		<guid isPermaLink="false">http://www.edumobile.org/iphone/?p=5045</guid>
		<description><![CDATA[In this tutorial I will be looking into the MapKit, a new API’s made available by Apple in the iOS 3.0 and later versions. The MapKit allows simple access to the map seen in the maps application. Using GoogleMaps as its engine the map kit allows for a developer to make their own custom map [...]]]></description>
			<content:encoded><![CDATA[<p>In this tutorial I will be looking into the MapKit, a new API’s made available by Apple in the iOS 3.0 and later versions. The MapKit allows simple access to the map seen in the maps application. Using GoogleMaps as its engine the map kit allows for a developer to make their own custom map interface to fit their own application. Today I will be reviewing the MapView as well as the Map Annotations that can be used to highlight points of interest in a map. For this purpose I will create my own custom map view along with custom annotations and its views. </p>
<p>Create project in Xcode using single window application template and name it as MapViewSample. Before proceeding further we need to add mapKit and coreLocation frameworks to the application bundle. I am not digging into the details of adding the frameworks into the project because it is quite common in all applications. Now open ViewController.xib file, add a map view outlet and connect it with the associated outlet in .h file. Open ViewController.h file, import MapKit framework into it and also modify it as follows.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="kw1">@interface</span> ViewController <span class="sy0">:</span> UIViewController&lt;MKMapViewDelegate&gt;<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/"><span class="kw5">NSArray</span></a> <span class="sy0">*</span>arr;<br />
<span class="br0">&#125;</span><br />
<span class="kw1">@property</span> <span class="br0">&#40;</span>nonatomic, retain<span class="br0">&#41;</span>IBOutlet MKMapView <span class="sy0">*</span>mapView;</p>
<p><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>initiateStaticAnnotations;</p>
<p><span class="kw1">@end</span></div>
</div>
<p>Now compile and run the code, you will get the following screenshot.</p>
<p><a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/05/Screenshot_12.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/05/Screenshot_12.png" alt="" title="Screenshot_1" width="238" height="450" class="alignnone size-full wp-image-5047" /></a></p>
<p>To make the round shaped corners for the map view we need to import the QuartzCore/CALayer.h header in ViewController.m file and also add the following code in viewDidLoad method of this class to get the desired effect. </p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;">mapView.layer.cornerRadius <span class="sy0">=</span> <span class="nu0">20</span>;<br />
mapView.layer.masksToBounds <span class="sy0">=</span> <span class="kw2">YES</span>;</div>
</div>
<p>Now run the code to see the proper corner radius effect. To add the annotations to this map view, we need to take a new class with subclass of NSObject and name it as MapAnnotation. Open MapAnnotation.h file and change it according to the following code.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="co1">#import &lt;MapKit/MapKit.h&gt;</span></p>
<p><span class="kw1">@interface</span> MapAnnotation <span class="sy0">:</span> <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/"><span class="kw5">NSObject</span></a><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; CLLocationCoordinate2D coordinate; &nbsp;<br />
&nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a> <span class="sy0">*</span>subTitleText; &nbsp;<br />
&nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a> <span class="sy0">*</span>titleText; <br />
<span class="br0">&#125;</span></p>
<p><span class="kw1">@property</span> <span class="br0">&#40;</span>nonatomic, readonly<span class="br0">&#41;</span> CLLocationCoordinate2D coordinate; &nbsp;<br />
<span class="kw1">@property</span> <span class="br0">&#40;</span>readwrite, retain<span class="br0">&#41;</span> <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a> <span class="sy0">*</span>titleText; &nbsp;<br />
<span class="kw1">@property</span> <span class="br0">&#40;</span>readwrite, retain<span class="br0">&#41;</span> <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a> <span class="sy0">*</span>subTitleText; &nbsp;</p>
<p><span class="sy0">-</span><span class="br0">&#40;</span><span class="kw4">id</span><span class="br0">&#41;</span>initWithCoordinate<span class="sy0">:</span><span class="br0">&#40;</span>CLLocationCoordinate2D<span class="br0">&#41;</span> coordinate; &nbsp;<br />
<span class="sy0">-</span> <span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a> <span class="sy0">*</span><span class="br0">&#41;</span>subTitle; &nbsp;<br />
<span class="sy0">-</span> <span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a> <span class="sy0">*</span><span class="br0">&#41;</span>title; &nbsp;<br />
<span class="sy0">-</span><span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>setTitle<span class="sy0">:</span><span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a><span class="sy0">*</span><span class="br0">&#41;</span>strTitle; &nbsp;<br />
<span class="sy0">-</span><span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>setSubTitle<span class="sy0">:</span><span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a><span class="sy0">*</span><span class="br0">&#41;</span>strSubTitle; </p>
<p><span class="kw1">@end</span></div>
</div>
<p>Now open MapAnnotation.m file and include the below code.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="kw1">@implementation</span> MapAnnotation</p>
<p><span class="kw1">@synthesize</span> coordinate, titleText, subTitleText; &nbsp;</p>
<p><span class="sy0">-</span> <span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a> <span class="sy0">*</span><span class="br0">&#41;</span>subtitle<span class="br0">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; <span class="kw1">return</span> subTitleText; &nbsp;<br />
<span class="br0">&#125;</span> &nbsp;<br />
<span class="sy0">-</span> <span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a> <span class="sy0">*</span><span class="br0">&#41;</span>title<span class="br0">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; <span class="kw1">return</span> titleText; &nbsp;<br />
<span class="br0">&#125;</span> &nbsp;</p>
<p><span class="sy0">-</span><span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>setTitle<span class="sy0">:</span><span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a><span class="sy0">*</span><span class="br0">&#41;</span>strTitle <span class="br0">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; self.titleText <span class="sy0">=</span> strTitle; &nbsp;<br />
<span class="br0">&#125;</span> &nbsp;</p>
<p><span class="sy0">-</span><span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>setSubTitle<span class="sy0">:</span><span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a><span class="sy0">*</span><span class="br0">&#41;</span>strSubTitle <span class="br0">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; self.subTitleText <span class="sy0">=</span> strSubTitle; &nbsp;<br />
<span class="br0">&#125;</span> &nbsp;</p>
<p><span class="sy0">-</span><span class="br0">&#40;</span><span class="kw4">id</span><span class="br0">&#41;</span>initWithCoordinate<span class="sy0">:</span><span class="br0">&#40;</span>CLLocationCoordinate2D<span class="br0">&#41;</span> c<span class="br0">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; coordinate<span class="sy0">=</span>c; &nbsp;<br />
&nbsp; &nbsp; <span class="kw1">return</span> self; &nbsp;<br />
<span class="br0">&#125;</span> &nbsp;</p>
<p><span class="kw1">@end</span></div>
</div>
<p>Now import the MapAnnotation class in ViewController.m file, add the initiateStaticAnnotations method in it and call this method from viewDidLoad method of this class. Implement initiateStaticAnnotations method in the following way.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>initiateStaticAnnotations<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; CLLocationCoordinate2D location1; <br />
&nbsp; &nbsp; &nbsp; &nbsp; location1.latitude <span class="sy0">=</span> <span class="nu0">32.805745</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; location1.longitude <span class="sy0">=</span> <span class="sy0">-</span><span class="nu0">96.811523</span>; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; MapAnnotation <span class="sy0">*</span>addAnnotation1 <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>MapAnnotation alloc<span class="br0">&#93;</span> initWithCoordinate<span class="sy0">:</span>location1<span class="br0">&#93;</span>; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>addAnnotation1 setTitle<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;First annotation&quot;</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>addAnnotation1 setSubTitle<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;3708 Brown Street, Dallas, TX&quot;</span><span class="br0">&#93;</span>; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; CLLocationCoordinate2D location2; <br />
&nbsp; &nbsp; &nbsp; &nbsp; location2.latitude <span class="sy0">=</span> <span class="nu0">14.349548</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; location2.longitude <span class="sy0">=</span> <span class="sy0">-</span><span class="nu0">14.501953</span>; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; MapAnnotation <span class="sy0">*</span>addAnnotation2 <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>MapAnnotation alloc<span class="br0">&#93;</span> initWithCoordinate<span class="sy0">:</span>location2<span class="br0">&#93;</span>; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>addAnnotation2 setTitle<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;Second annotation&quot;</span><span class="br0">&#93;</span>; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>addAnnotation2 setSubTitle<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;Tambacounda, Senegal&quot;</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; CLLocationCoordinate2D location3; <br />
&nbsp; &nbsp; &nbsp; &nbsp; location3.latitude <span class="sy0">=</span> <span class="nu0">47.338823</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; location3.longitude <span class="sy0">=</span> <span class="sy0">-</span><span class="nu0">120.541992</span>; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; MapAnnotation <span class="sy0">*</span>addAnnotation3 <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>MapAnnotation alloc<span class="br0">&#93;</span> initWithCoordinate<span class="sy0">:</span>location3<span class="br0">&#93;</span>; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>addAnnotation3 setTitle<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;Third annotation&quot;</span><span class="br0">&#93;</span>; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>addAnnotation3 setSubTitle<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;Wenatchee National Forest, Peshastin, WA 98847, USA&quot;</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; CLLocationCoordinate2D location4; <br />
&nbsp; &nbsp; &nbsp; &nbsp; location4.latitude <span class="sy0">=</span> <span class="nu0">40.380028</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; location4.longitude <span class="sy0">=</span> <span class="sy0">-</span><span class="nu0">3.603516</span>; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; MapAnnotation <span class="sy0">*</span>addAnnotation4 <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>MapAnnotation alloc<span class="br0">&#93;</span> initWithCoordinate<span class="sy0">:</span>location4<span class="br0">&#93;</span>; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>addAnnotation4 setTitle<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;Fourth annotation&quot;</span><span class="br0">&#93;</span>; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>addAnnotation4 setSubTitle<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;Calle Fuentespina, 1A, 28031 Madrid, Spain&quot;</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; arr <span class="sy0">=</span> <span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/"><span class="kw5">NSArray</span></a> arrayWithObjects<span class="sy0">:</span>addAnnotation1,addAnnotation2,addAnnotation3,addAnnotation4,<span class="kw2">nil</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>mapView addAnnotations<span class="sy0">:</span>arr<span class="br0">&#93;</span>; <br />
<span class="br0">&#125;</span></div>
</div>
<p>In this method I have taken 4 locations/annotations. To show these annotations in the respected places, we need to implement map view delegate method for annotations in the ViewController.m as follows.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="sy0">-</span> <span class="br0">&#40;</span>MKAnnotationView <span class="sy0">*</span><span class="br0">&#41;</span>mapView<span class="sy0">:</span><span class="br0">&#40;</span>MKMapView <span class="sy0">*</span><span class="br0">&#41;</span>map viewForAnnotation<span class="sy0">:</span><span class="br0">&#40;</span><span class="kw4">id</span> &lt;MKAnnotation&gt;<span class="br0">&#41;</span>annotation<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; MKPinAnnotationView <span class="sy0">*</span>annView<span class="sy0">=</span><span class="br0">&#91;</span><span class="br0">&#91;</span>MKPinAnnotationView alloc<span class="br0">&#93;</span> initWithAnnotation<span class="sy0">:</span>annotation reuseIdentifier<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;MyPin&quot;</span><span class="br0">&#93;</span>; &nbsp;<br />
&nbsp; &nbsp; annView.animatesDrop<span class="sy0">=</span>TRUE; &nbsp;<br />
&nbsp; &nbsp; annView.canShowCallout <span class="sy0">=</span> <span class="kw2">YES</span>; &nbsp;<br />
&nbsp; &nbsp; <span class="br0">&#91;</span>annView setSelected<span class="sy0">:</span><span class="kw2">YES</span><span class="br0">&#93;</span>; &nbsp;<br />
&nbsp; &nbsp; annView.pinColor <span class="sy0">=</span> MKPinAnnotationColorRed; &nbsp;<br />
&nbsp; &nbsp; annView.calloutOffset <span class="sy0">=</span> CGPointMake<span class="br0">&#40;</span>15, 15<span class="br0">&#41;</span>; &nbsp;<br />
&nbsp; &nbsp; <span class="kw1">return</span> annView;<br />
<span class="br0">&#125;</span></div>
</div>
<p>Now build and run the code, you will see the two annotations. After tapping on one annotation you will get the following screen. Drag the map view to the left side to see the remaining two annotations and its views.<br />
</br></br><br />
<a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/05/Screenshot_22.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/05/Screenshot_22-159x300.png" alt="" title="Screenshot_2" width="238" height="450" class="alignleft size-medium wp-image-5048" /></a><a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/05/Screenshot_31.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/05/Screenshot_31-159x300.png" alt="" title="Screenshot_3" width="238" height="450" class="aligncentre size-medium wp-image-5049" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.edumobile.org/iphone/iphone-programming-tutorials/map-view-with-annotations-and-its-views/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Notifications in iPhone Programming</title>
		<link>http://www.edumobile.org/iphone/iphone-beginner-tutorials/notifications-in-iphone-programming/</link>
		<comments>http://www.edumobile.org/iphone/iphone-beginner-tutorials/notifications-in-iphone-programming/#comments</comments>
		<pubDate>Tue, 21 May 2013 11:05:04 +0000</pubDate>
		<dc:creator>Sushant</dc:creator>
				<category><![CDATA[How to make an app]]></category>
		<category><![CDATA[iPhone Beginner Tutorials]]></category>
		<category><![CDATA[iPhone Programming free apps codes]]></category>
		<category><![CDATA[iPhone Programming Tutorials]]></category>
		<category><![CDATA[Notifications example iPhone]]></category>
		<category><![CDATA[Notifications in iPhone]]></category>
		<category><![CDATA[Notifications in iPhone Programming]]></category>
		<category><![CDATA[Notifications iPhone]]></category>

		<guid isPermaLink="false">http://www.edumobile.org/iphone/?p=5036</guid>
		<description><![CDATA[In this tutorial I will explain how local notifications can be performed in iOS application. Here there’s three important steps: 1. Create a new observer to listen for the notification (event) to happen. 2. Post the notification when our event happens. 3. Remove the observer when we no longer need it. From apple documentation, NSNotification [...]]]></description>
			<content:encoded><![CDATA[<p>In this tutorial I will explain how local notifications can be performed in iOS application. Here there’s three important steps:</p>
<p>       1.    Create a new observer to listen for the notification (event) to happen.<br />
       2.    Post the notification when our event happens.<br />
       3.    Remove the observer when we no longer need it.</p>
<p>From apple documentation, NSNotification objects encapsulate information so that it can be broadcast to other objects by an NSNotificationCenter object. An NSNotification object (referred to as a notification) contains a name, an object, and an optional dictionary. The name is a tag identifying the notification. The object is any object that the poster of the notification wants to send to observers of that notification (typically, it is the object that posted the notification). The dictionary stores other related objects, if any. NSNotification objects are immutable objects.</br><br />
NSNotificationCenter object (or simply, notification centre) provides a mechanism for broadcasting information within a program. An NSNotificationCenter object is essentially a notification dispatch table. Objects register with a notification centre to receive notifications (NSNotification objects) using the addObserver:selector:name:object: or addObserverForName:object:queue:usingBlock: methods. Each invocation of this method specifies a set of notifications. Therefore, objects may register as observers of different notification sets by calling these methods several times.</br><br />
Each running Cocoa program has a default notification centre. You typically don’t create your own. An NSNotificationCenter object can deliver notifications only within a single program. If you want to post a notification to other processes or receive notifications from other processes, use an instance of NSDistributedNotificationCenter. We can create a notification object with either of the following class methods</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="sy0">+</span> <span class="br0">&#40;</span><span class="kw4">id</span><span class="br0">&#41;</span>notificationWithName<span class="sy0">:</span><span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a> <span class="sy0">*</span><span class="br0">&#41;</span>aName object<span class="sy0">:</span><span class="br0">&#40;</span><span class="kw4">id</span><span class="br0">&#41;</span>anObject<br />
<span class="sy0">+</span> <span class="br0">&#40;</span><span class="kw4">id</span><span class="br0">&#41;</span>notificationWithName<span class="sy0">:</span><span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a> <span class="sy0">*</span><span class="br0">&#41;</span>aName object<span class="sy0">:</span><span class="br0">&#40;</span><span class="kw4">id</span><span class="br0">&#41;</span>anObject userInfo<span class="sy0">:</span><span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/"><span class="kw5">NSDictionary</span></a> <span class="sy0">*</span><span class="br0">&#41;</span>userInfo</div>
</div>
<p></br><br />
Here first method Returns a new notification object with a specified name and object. Name is the new notification name and object is the object for new notification. And the second method returns a notification object with a specified name, object, and user information. The user information dictionary for the new notification. May be nil. However, you don’t usually create your own notifications directly. The NSNotificationCenter methods will take care of that. Following methods explain the basis of observing the notification.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>addObserver<span class="sy0">:</span><span class="br0">&#40;</span><span class="kw4">id</span><span class="br0">&#41;</span>notificationObserver selector<span class="sy0">:</span><span class="br0">&#40;</span><span class="kw4">SEL</span><span class="br0">&#41;</span>notificationSelector name<span class="sy0">:</span><span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a> <span class="sy0">*</span><span class="br0">&#41;</span>notificationName object<span class="sy0">:</span><span class="br0">&#40;</span><span class="kw4">id</span><span class="br0">&#41;</span>notificationSender<br />
<span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">id</span><span class="br0">&#41;</span>addObserverForName<span class="sy0">:</span><span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a> <span class="sy0">*</span><span class="br0">&#41;</span>name object<span class="sy0">:</span><span class="br0">&#40;</span><span class="kw4">id</span><span class="br0">&#41;</span>obj queue<span class="sy0">:</span><span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSOperationQueue_Class/"><span class="kw5">NSOperationQueue</span></a> <span class="sy0">*</span><span class="br0">&#41;</span>queue usingBlock<span class="sy0">:</span><span class="br0">&#40;</span><span class="kw4">void</span> <span class="br0">&#40;</span><span class="sy0">^</span><span class="br0">&#41;</span><span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSNotification_Class/"><span class="kw5">NSNotification</span></a> <span class="sy0">*</span><span class="br0">&#41;</span><span class="br0">&#41;</span>block</div>
</div>
<p></br><br />
As per the documentation this method adds an entry to the receiver’s dispatch table with an observer, a notification selector and optional criteria: notification name and sender. Second method adds an entry to the receiver’s dispatch table with a notification queue and a block to add to the queue, and optional criteria: notification name and sender. The parameter &#8220;block&#8221; will be executed when notification is successfully posted. If a given notification triggers more than one observer block, the blocks may all be executed concurrently with respect to one another (but on their given queue or on the current thread).The following example shows how you can register to receive locale change notifications.</br></p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/"><span class="kw5">NSNotificationCenter</span></a> <span class="sy0">*</span>center <span class="sy0">=</span> <span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/"><span class="kw5">NSNotificationCenter</span></a> defaultCenter<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSOperationQueue_Class/"><span class="kw5">NSOperationQueue</span></a> <span class="sy0">*</span>mainQueue <span class="sy0">=</span> <span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSOperationQueue_Class/"><span class="kw5">NSOperationQueue</span></a> mainQueue<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#91;</span>center addObserverForName<span class="sy0">:</span>NSCurrentLocaleDidChangeNotification object<span class="sy0">:</span><span class="kw2">nil</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;queue<span class="sy0">:</span>mainQueue usingBlock<span class="sy0">:^</span><span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSNotification_Class/"><span class="kw5">NSNotification</span></a> <span class="sy0">*</span>note<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NSLog<span class="br0">&#40;</span><span class="co3">@</span><span class="st0">&quot;The user&#8217;s locale changed to: %@&quot;</span>, <span class="br0">&#91;</span><span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSLocale_Class/"><span class="kw5">NSLocale</span></a> currentLocale<span class="br0">&#93;</span> localeIdentifier<span class="br0">&#93;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><span class="br0">&#93;</span>;</div>
</div>
<p></br><br />
I will explain the above methods with the help of example. First Launch Xcode and click File > New > Project. Select an iOS Single View Application and click &#8220;Next&#8221;. Name your product &#8220;Notifications&#8221; and enter a name for your Company Identifier, such as &#8220;com.companyName.notifications.&#8221; Choose the iPhone device family and click &#8220;Next.&#8221; Choose a location to store your project and click &#8220;Create.&#8221; Open ViewController.h file, modify it as follows.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="kw1">@interface</span> ViewController <span class="sy0">:</span> UIViewController</p>
<p><span class="kw1">@property</span> <span class="br0">&#40;</span>nonatomic, strong<span class="br0">&#41;</span> IBOutlet UIButton <span class="sy0">*</span>button;</p>
<p><span class="sy0">-</span> <span class="br0">&#40;</span>IBAction<span class="br0">&#41;</span>button<span class="sy0">:</span><span class="br0">&#40;</span><span class="kw4">id</span><span class="br0">&#41;</span>sender;</p>
<p><span class="kw1">@end</span></div>
</div>
<p>In the same way change the ViewController.m file as follows.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="kw1">@implementation</span> ViewController</p>
<p><span class="kw1">@synthesize</span> button;</p>
<p><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>viewDidLoad<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="br0">&#91;</span>super viewDidLoad<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="br0">&#91;</span><span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/"><span class="kw5">NSNotificationCenter</span></a> defaultCenter<span class="br0">&#93;</span> addObserver<span class="sy0">:</span>self selector<span class="sy0">:</span><span class="kw1">@selector</span><span class="br0">&#40;</span>hideButton<span class="sy0">:</span><span class="br0">&#41;</span> name<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;buttonHide&quot;</span> object<span class="sy0">:</span><span class="kw2">nil</span><span class="br0">&#93;</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>didReceiveMemoryWarning<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="br0">&#91;</span>super didReceiveMemoryWarning<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="co2">// Dispose of any resources that can be recreated.</span><br />
<span class="br0">&#125;</span></p>
<p><span class="sy0">-</span> <span class="br0">&#40;</span>IBAction<span class="br0">&#41;</span>button<span class="sy0">:</span><span class="br0">&#40;</span><span class="kw4">id</span><span class="br0">&#41;</span>sender<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="br0">&#91;</span><span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/"><span class="kw5">NSNotificationCenter</span></a> defaultCenter<span class="br0">&#93;</span> postNotificationName<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;buttonHide&quot;</span> object<span class="sy0">:</span><span class="kw2">nil</span><span class="br0">&#93;</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>hideButton<span class="sy0">:</span><span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSNotification_Class/"><span class="kw5">NSNotification</span></a> <span class="sy0">*</span><span class="br0">&#41;</span>notification<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; self.button.hidden <span class="sy0">=</span> <span class="kw2">YES</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw1">@end</span></div>
</div>
<p></br><br />
There are four important parts of the NSNotificationCenter method addObserver:selector:name:object:. The argument for addObserver: is the object that wants to know when a certain notification happens. The argument for selector: is the method that gets called when the notification happens. The argument for name: is the title of the notification the observer wants to know about; it must be unique. The final piece of the method is object:. Its argument is the object attached to the notification and is often nil depending on the context of the notification.</br><br />
There are three important parts of the NSNotificationCenter method postNotificationName:object:userInfo:. The argument for postNotificationName: is the title of the notification that was registered in the previous addObserver:selector:name:object: method. The argument for object:, again, is the object posting the notification and in this case is nil. The argument foruserInfo is an NSDictionary that can be used to send additional information with the notification.userInfo can be nil, but in this instance we want to know the orientation of the device. In order to send it with the notification the information is packaged inside a dictionary. Now compile and run the code, the UIButton will hide after tapping on it, because button hide notification will fire.</p>
<p><a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/05/Screenshot_11.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/05/Screenshot_11.png" alt="" title="Screenshot_1" width="198" height="372" class="alignleft size-full wp-image-5037" /></a></p>
<p><a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/05/Screenshot_21.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/05/Screenshot_21.png" alt="" title="Screenshot_2" width="198" height="372" class="aligncenter size-full wp-image-5038" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.edumobile.org/iphone/iphone-beginner-tutorials/notifications-in-iphone-programming/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Tabbed Application from Scratch for iPhone</title>
		<link>http://www.edumobile.org/iphone/iphone-apps/a-tabbed-application-from-scratch-for-iphone/</link>
		<comments>http://www.edumobile.org/iphone/iphone-apps/a-tabbed-application-from-scratch-for-iphone/#comments</comments>
		<pubDate>Thu, 16 May 2013 10:11:40 +0000</pubDate>
		<dc:creator>Sushant</dc:creator>
				<category><![CDATA[iPhone Apps]]></category>
		<category><![CDATA[iPhone Beginner Tutorials]]></category>
		<category><![CDATA[iPhone Programming Tutorials]]></category>
		<category><![CDATA[A Tabbed Application]]></category>
		<category><![CDATA[A Tabbed Application example]]></category>
		<category><![CDATA[A Tabbed Application from Scratch]]></category>
		<category><![CDATA[A Tabbed Application from Scratch for iPhone]]></category>
		<category><![CDATA[free iphone apps code and tutorials]]></category>
		<category><![CDATA[iPhone apps tutorials]]></category>
		<category><![CDATA[iPhone Tabbed Application from Scratch]]></category>

		<guid isPermaLink="false">http://www.edumobile.org/iphone/?p=3689</guid>
		<description><![CDATA[Even though Xcode provides a template for building a tabbed application, we often want to start from scratch when building a tabbed app. So let&#8217;s see how it&#8217;s done. Start Xcode, select “Create a new Xcode project,” and click Next. Choose the Empty Application template, click Next, and name the project TabBar. Select options as [...]]]></description>
			<content:encoded><![CDATA[<p>Even though Xcode provides a template for building a tabbed application, we often want to start from scratch when building a tabbed app. So let&#8217;s see how it&#8217;s done.</p>
<p>Start Xcode, select “Create a new Xcode project,” and click Next. Choose the Empty Application template, click Next, and name the project TabBar. Select options as shown here:</p>
<p><a href="http://www.edumobile.org/iphone/wp-content/uploads/2012/07/Image0015.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2012/07/Image0015.png" alt="" title="Image001" width="536" height="339" class="alignnone size-full wp-image-3691" /></a></p>
<p>Click Next, choose a location to save the project, and click Create.</p>
<p>Once the project has been created, add two images to the project using any available drawing software. I&#8217;ve created these images using Gimp (a free vector drawing application). These images should be 30 x 30 pixels, and for best effect should be drawn in black on a transparent background. You can either create your own or use these:</p>
<p><a href="http://www.edumobile.org/iphone/wp-content/uploads/2012/07/Image0025.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2012/07/Image0025.png" alt="" title="Image002" width="30" height="30" class="alignnone size-full wp-image-3692" /></a></p>
<p><a href="http://www.edumobile.org/iphone/wp-content/uploads/2012/07/Image0035.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2012/07/Image0035.png" alt="" title="Image003" width="30" height="30" class="aligncenter size-full wp-image-3693" /></a></p>
<p>When you save these images, rename them first.png and second.png. If you are using some other format than .png for your images, make sure to adjust the extension in the code. </p>
<p>Add two new UIViewController classes to the project by selecting File | New > File&#8230; Name them FirstViewController and SecondViewController; make sure that they both are subclasses of UIViewController, and allow .xib files to be created for both as shown:</p>
<p><a href="http://www.edumobile.org/iphone/wp-content/uploads/2012/07/Image0044.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2012/07/Image0044.png" alt="" title="Image004" width="550" height="298" class="aligncenter size-full wp-image-3694" /></a></p>
<p>Open the .xib files for these view controllers, and drag a UINavigationBar to the top of each. Title the first bar “First View” and the second “Second View.” This step is not strictly necessary, but will give a nice title to each of our view controllers.</p>
<p>Now open the AppDelegate.h file, and add two properties for the view controllers, as well as one for the UITabBarController as shown:</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="co1">#import &lt;UIKit/UIKit.h&gt;</span><br />
<span class="co1">#import &quot;FirstViewController.h&quot;</span><br />
<span class="co1">#import &quot;SecondViewController.h&quot;</span></p>
<p><span class="kw1">@interface</span> AppDelegate <span class="sy0">:</span> UIResponder &lt;UIApplicationDelegate&gt;</p>
<p><span class="kw1">@property</span> <span class="br0">&#40;</span>strong, nonatomic<span class="br0">&#41;</span> UIWindow <span class="sy0">*</span>window;<br />
<span class="kw1">@property</span> <span class="br0">&#40;</span>strong, nonatomic<span class="br0">&#41;</span> UITabBarController <span class="sy0">*</span>tabBarController;<br />
<span class="kw1">@property</span> <span class="br0">&#40;</span>strong, nonatomic<span class="br0">&#41;</span> FirstViewController <span class="sy0">*</span>firstViewController;<br />
<span class="kw1">@property</span> <span class="br0">&#40;</span>strong, nonatomic<span class="br0">&#41;</span> SecondViewController <span class="sy0">*</span>secondViewController;</p>
<p><span class="kw1">@end</span></div>
</div>
<p>All the set up for the application will be done in the AppDelegate.m file, in the application: didFinishLaunchingWithOptions: method. Here is the listing:</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="co1">#import &quot;AppDelegate.h&quot;</span></p>
<p><span class="kw1">@implementation</span> AppDelegate</p>
<p><span class="kw1">@synthesize</span> window <span class="sy0">=</span> _window;<br />
<span class="kw1">@synthesize</span> tabBarController <span class="sy0">=</span> _tabBarController;<br />
<span class="kw1">@synthesize</span> firstViewController <span class="sy0">=</span> _firstViewController;<br />
<span class="kw1">@synthesize</span> secondViewController <span class="sy0">=</span> _secondViewController;</p>
<p><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">BOOL</span><span class="br0">&#41;</span>application<span class="sy0">:</span><span class="br0">&#40;</span>UIApplication <span class="sy0">*</span><span class="br0">&#41;</span>application didFinishLaunchingWithOptions<span class="sy0">:</span><span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/"><span class="kw5">NSDictionary</span></a> <span class="sy0">*</span><span class="br0">&#41;</span>launchOptions<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; self.window <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>UIWindow alloc<span class="br0">&#93;</span> initWithFrame<span class="sy0">:</span><span class="br0">&#91;</span><span class="br0">&#91;</span>UIScreen mainScreen<span class="br0">&#93;</span> bounds<span class="br0">&#93;</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="co2">// Override point for customization after application launch.</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co2">// instantiate the view controllers:</span><br />
&nbsp; &nbsp; self.firstViewController <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>FirstViewController alloc<span class="br0">&#93;</span> initWithNibName<span class="sy0">:</span><span class="kw2">nil</span> bundle<span class="sy0">:</span><span class="kw2">nil</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; self.secondViewController <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>SecondViewController alloc<span class="br0">&#93;</span> initWithNibName<span class="sy0">:</span><span class="kw2">nil</span> bundle<span class="sy0">:</span><span class="kw2">nil</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co2">// set the titles for the view controllers:</span><br />
&nbsp; &nbsp; self.firstViewController.title <span class="sy0">=</span> <span class="co3">@</span><span class="st0">&quot;First&quot;</span>;<br />
&nbsp; &nbsp; self.secondViewController.title <span class="sy0">=</span> <span class="co3">@</span><span class="st0">&quot;Second&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co2">// set the images to appear in the tab bar:</span><br />
&nbsp; &nbsp; self.firstViewController.tabBarItem.image <span class="sy0">=</span> <span class="br0">&#91;</span>UIImage imageNamed<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;first.png&quot;</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; self.secondViewController.tabBarItem.image <span class="sy0">=</span> <span class="br0">&#91;</span>UIImage imageNamed<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;second.png&quot;</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co2">// instantiate the tab bar controller:</span><br />
&nbsp; &nbsp; self.tabBarController <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>UITabBarController alloc<span class="br0">&#93;</span> init<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co2">// set the tab bar&#8217;s view controllers array:</span><br />
&nbsp; &nbsp; self.tabBarController.viewControllers <span class="sy0">=</span> <span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/"><span class="kw5">NSArray</span></a> arrayWithObjects<span class="sy0">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self.firstViewController, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self.secondViewController,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw2">nil</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co2">// add the tab bar to the application window as a subview:</span><br />
&nbsp; &nbsp; <span class="br0">&#91;</span>self.window addSubview<span class="sy0">:</span>self.tabBarController.view<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co2">// make the application window key and visible:</span><br />
&nbsp; &nbsp; <span class="br0">&#91;</span>self.window makeKeyAndVisible<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">YES</span>;<br />
<span class="br0">&#125;</span><br />
…</div>
</div>
<p>After synthesizing the properties we just created, we first instantiate the two view controllers, each with a nib name of nil (meaning to use the .xib file we created for each controller), and a bundle of nil (meaning to use this application&#8217;s bundle). A title is set for each view controller; this title will appear in the tab (in the tab bar) for each controller. Next, we set the image for each controller as it will appear on the tab bar. These are the images we created earlier; make sure that they have been added to the project. </p>
<p>Now that the view controllers are instantiated, with titles and images for each, we need a tab bar controller to put them on. We instantiate one, then add the view controllers to the UITabBarController&#8217;s viewControllers array, using the class method arrayWithObjects (from NSArray). Finally, we add the tab bar&#8217;s view to the main application window as a subview and make the main window key and visible. </p>
<p>Here is the application running:</p>
<p><a href="http://www.edumobile.org/iphone/wp-content/uploads/2012/07/TabBar.jpg"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2012/07/TabBar.jpg" alt="" title="TabBar" width="591" height="330" class="alignnone size-full wp-image-3690" /></a></p>
<p>Notice that iOS has taken the black on transparent images we provided and applied color to them in a stylistic manner. </p>
<p>UITabBarController should always be used as the top level controller for an application. A view controller inside a tab bar can be of any type, even a navigation controller. But we should not place a tab bar controller inside a navigation controller. This is because tab bars are meant to be on the screen (for access by the user) throughout the life of the application.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.edumobile.org/iphone/iphone-apps/a-tabbed-application-from-scratch-for-iphone/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Loading HTML data using UIWebView in iPhone Development</title>
		<link>http://www.edumobile.org/iphone/iphone-programming-tutorials/loading-html-data-using-uiwebview-in-iphone-development/</link>
		<comments>http://www.edumobile.org/iphone/iphone-programming-tutorials/loading-html-data-using-uiwebview-in-iphone-development/#comments</comments>
		<pubDate>Sat, 11 May 2013 09:12:19 +0000</pubDate>
		<dc:creator>Sushant</dc:creator>
				<category><![CDATA[iPhone Beginner Tutorials]]></category>
		<category><![CDATA[iPhone Programming Tutorials]]></category>
		<category><![CDATA[iphone development]]></category>
		<category><![CDATA[iphone tutorials]]></category>
		<category><![CDATA[Loading HTML data UIWebView iPhone]]></category>
		<category><![CDATA[Loading HTML data using UIWebView]]></category>
		<category><![CDATA[Loading HTML data using UIWebView example]]></category>
		<category><![CDATA[Loading HTML data using UIWebView iPhone]]></category>
		<category><![CDATA[Loading HTML UIWebView iPhone]]></category>
		<category><![CDATA[loading html xcode sdk]]></category>

		<guid isPermaLink="false">http://www.edumobile.org/iphone/?p=4948</guid>
		<description><![CDATA[Suppose if we want to populate the large amount of static data in iOS application then we generally use text view object to represent that data. Text view object is feasible for some situations. But if we want the different font sizes and different font colors to different paragraphs then text view is not at [...]]]></description>
			<content:encoded><![CDATA[<p>Suppose if we want to populate the large amount of static data in iOS application then we generally use text view object to represent that data. Text view object is feasible for some situations. But if we want the different font sizes and different font colors to different paragraphs then text view is not at all feasible. In that situations we can use UIWebview to load the HTML data. Before loading we need to construct the proper HTML data with required fonts and colors. Create a new project in Xcode using single window application template and name it as WebviewToHtmlText. Before proceeding further we need to add a navigation controller to the AppDelegate and make ViewController as its root view. We can do this either by adding UINavigationController and its root view in AppDelegate.m file or by using nib file of ViewController class. I am not digging into the details of the navigation code because it is quite common in all applications. Now open ViewController.xib file, add a text view outlet and connect it with the associated outlet in .h file. Save the HTML text you want to populate in web view as a .txt file and add that to the project bundle, name it as web_text.txt. Now open viewDidLoad of .m file and add the following code.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;">self.title <span class="sy0">=</span> <span class="co3">@</span><span class="st0">&quot;Text view&quot;</span>;<br />
self.textString <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a> alloc<span class="br0">&#93;</span> initWithContentsOfFile<span class="sy0">:</span><span class="br0">&#91;</span><span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSBundle_Class/"><span class="kw5">NSBundle</span></a> mainBundle<span class="br0">&#93;</span> pathForResource<span class="sy0">:</span> <span class="co3">@</span><span class="st0">&quot;web_text&quot;</span> ofType<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;txt&quot;</span><span class="br0">&#93;</span> encoding<span class="sy0">:</span>NSUTF8StringEncoding error<span class="sy0">:</span><span class="kw2">nil</span><span class="br0">&#93;</span>;<br />
UIBarButtonItem <span class="sy0">*</span>barButton <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>UIBarButtonItem alloc<span class="br0">&#93;</span>initWithTitle<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;Web view&quot;</span> style<span class="sy0">:</span>UIBarButtonItemStyleBordered target<span class="sy0">:</span>self action<span class="sy0">:</span><span class="kw1">@selector</span><span class="br0">&#40;</span>loadWebView<span class="sy0">:</span><span class="br0">&#41;</span><span class="br0">&#93;</span>;<br />
self.navigationItem.rightBarButtonItem <span class="sy0">=</span> barButton;<br />
aTextView.text <span class="sy0">=</span> self.textString;</div>
</div>
<p>Here we have converted the content of web_text.txt into NSString using initWithContentsOfFile method. Now compile and run the code, web_text.txt file loaded into text view as follows.</p>
<p><a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/05/Screenshot_1.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/05/Screenshot_1.png" alt="" title="Screenshot_1" width="300" height="570" class="alignnone size-full wp-image-4949" /></a></p>
<p>Don&#8217;t hit Web view bar button because we have not implemented the corresponding action method. Our requirement is to populate the HTML content of web_text.txt into web view using UIWebView outlet in some other controller. Add the new controller named WebViewController by right clicking on project file and select new file option.<br />
<a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/05/Screenshot_2.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/05/Screenshot_2.png" alt="" title="Screenshot_2" width="300" height="321" class="alignnone size-full wp-image-4950" /></a></p>
<p>Open WebViewController.xib file, add UIWebView outlet and connect it with the corresponding object in WebViewController class. WebViewController.h file will look like this.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="kw1">@interface</span> WebViewController <span class="sy0">:</span> UIViewController&lt;UIWebViewDelegate&gt;</p>
<p><span class="kw1">@property</span> <span class="br0">&#40;</span>nonatomic, strong<span class="br0">&#41;</span> IBOutlet UIWebView <span class="sy0">*</span>aWebView;<br />
<span class="kw1">@property</span> <span class="br0">&#40;</span>nonatomic, strong<span class="br0">&#41;</span> <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a> <span class="sy0">*</span>webText;</p>
<p><span class="kw1">@end</span></div>
</div>
<p>Add the following line of code in initWithNibName method of WebViewController.m file.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;">webText <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a> alloc<span class="br0">&#93;</span>init<span class="br0">&#93;</span>;</div>
</div>
<p>Now it is require to load web view with HTML text from web_text.txt file. This can be done by adding the following code.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="br0">&#91;</span>self.aWebView loadHTMLString<span class="sy0">:</span>webText baseURL<span class="sy0">:</span><span class="kw2">nil</span><span class="br0">&#93;</span>;</div>
</div>
<p>Now we need to connect WebViewController with ViewController class. For that add the following code at the end of ViewController.m file.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>loadWebView<span class="sy0">:</span><span class="br0">&#40;</span><span class="kw4">id</span><span class="br0">&#41;</span>sender<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; WebViewController <span class="sy0">*</span>webController <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>WebViewController alloc<span class="br0">&#93;</span>initWithNibName<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;WebViewController&quot;</span> bundle<span class="sy0">:</span><span class="kw2">nil</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; webController.webText <span class="sy0">=</span> self.textString;<br />
&nbsp; &nbsp; webController.title <span class="sy0">=</span> <span class="co3">@</span><span class="st0">&quot;Web view&quot;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#91;</span>self.navigationController pushViewController<span class="sy0">:</span>webController animated<span class="sy0">:</span><span class="kw2">YES</span><span class="br0">&#93;</span>;<br />
<span class="br0">&#125;</span></div>
</div>
<p>Build and run the code, observe that the same text you will see in text view will populate in web view with different colour and fonts after tapping web view button.<br />
<a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/05/Screenshot_3.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/05/Screenshot_3.png" alt="" title="Screenshot_3" width="300" height="570" class="alignnone size-full wp-image-4951" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.edumobile.org/iphone/iphone-programming-tutorials/loading-html-data-using-uiwebview-in-iphone-development/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Toolbar animation along with the keyboard</title>
		<link>http://www.edumobile.org/iphone/iphone-apps/toolbar-animation-along-with-the-keyboard/</link>
		<comments>http://www.edumobile.org/iphone/iphone-apps/toolbar-animation-along-with-the-keyboard/#comments</comments>
		<pubDate>Tue, 07 May 2013 11:53:21 +0000</pubDate>
		<dc:creator>Sushant</dc:creator>
				<category><![CDATA[How to make an app]]></category>
		<category><![CDATA[iPhone Apps]]></category>
		<category><![CDATA[iPhone Game Programming]]></category>
		<category><![CDATA[Toolbar animation]]></category>
		<category><![CDATA[Toolbar animation keypad example]]></category>
		<category><![CDATA[Toolbar animation keypad free iphone tutorial]]></category>
		<category><![CDATA[Toolbar animation keypad iphone]]></category>
		<category><![CDATA[Toolbar animation keypad iphone programming]]></category>
		<category><![CDATA[Toolbar animation keypad iphone tutorials]]></category>
		<category><![CDATA[Toolbar animation with keypad]]></category>

		<guid isPermaLink="false">http://www.edumobile.org/iphone/?p=4933</guid>
		<description><![CDATA[There are some situations in iOS applications like animating bottom tool bar with the keyboard. That is, we need to animate the tool bar on top of keyboard when showing and hiding the keyboard. This can be done by using keyboard notifications in iOS. In this tutorial I will explain how to present the key [...]]]></description>
			<content:encoded><![CDATA[<p>There are some situations in iOS applications like animating bottom tool bar with the keyboard. That is, we need to animate the tool bar on top of keyboard when showing and hiding the keyboard. This can be done by using keyboard notifications in iOS. In this tutorial I will explain how to present the key board with tool bar on top of it with curve animations. For this we need to have some background knowledge in iOS local notifications. </p>
<p>Create a new project in Xcode using single view application template and name it as ToolbarAnimation. Now open ViewController.xib file, add UILable, UITextfield and UIToolbar outlets and also connect them with the associated objects in ViewController class. .xib file look like this</p>
<p><a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/05/ScreenShot_1.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/05/ScreenShot_1.png" alt="" title="ScreenShot_1" width="198" height="372" class="alignnone size-full wp-image-4934" /></a></p>
<p>Build and run the code you will see the following screens.<br />
<a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/05/ScreenShot_2.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/05/ScreenShot_2.png" alt="" title="ScreenShot_2" width="198" height="372" class="alignleft size-full wp-image-4935" /></a> <a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/05/ScreenShot_3.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/05/ScreenShot_3.png" alt="" title="ScreenShot_3" width="198" height="372" class="aligncenter size-full wp-image-4936" /></a><br />
<br/></p>
<p>Observe that, tool bar is at the bottom of the screen. Once you tapped on text field we need the tool bar to be animated with the keyboard. Here second screenshot is not showing the desired result. For this we need to register the controller with two keyboard notifications UIKeyboardWillShowNotification, UIKeyboardWillHideNotification. Before continue with notifications, we need to learn some basic things about NSNotificationCenter. As per the documentation, an NSNotificationCenter object (or simply, notification center) provides a mechanism for broadcasting information within a program. An NSNotificationCenter object is essentially a notification dispatch table. To register an object with a notification center to receive notifications we need to call addObserver:selector:name:object method. Open viewDidLoad method of ViewController.m file and register the notifications as follows.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="br0">&#91;</span><span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/"><span class="kw5">NSNotificationCenter</span></a> defaultCenter<span class="br0">&#93;</span> addObserver<span class="sy0">:</span>self selector<span class="sy0">:</span><span class="kw1">@selector</span><span class="br0">&#40;</span>keyboardWillShow<span class="sy0">:</span><span class="br0">&#41;</span> name<span class="sy0">:</span>UIKeyboardWillShowNotification object<span class="sy0">:</span><span class="kw2">nil</span><span class="br0">&#93;</span>;<br />
<span class="br0">&#91;</span><span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/"><span class="kw5">NSNotificationCenter</span></a> defaultCenter<span class="br0">&#93;</span> addObserver<span class="sy0">:</span>self selector<span class="sy0">:</span><span class="kw1">@selector</span><span class="br0">&#40;</span>keyboardWillHide<span class="sy0">:</span><span class="br0">&#41;</span> name<span class="sy0">:</span>UIKeyboardWillHideNotification object<span class="sy0">:</span><span class="kw2">nil</span><span class="br0">&#93;</span>;</div>
</div>
<p>For selector parameter we have given two selector names as keyboardWillShow: and keyboardWillHide: these selectors specifies the message the receiver sends notificationObserver to notify it of the notification posting. Hence we need to implement these two selectors such that they can perform a particular task when a notification is posted. Here when keyboard will show notification is posted, we want the tool bar to be animated on top of keyboard. This can be achieved by adding following code.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span> keyboardWillShow<span class="sy0">:</span> <span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSNotification_Class/"><span class="kw5">NSNotification</span></a> <span class="sy0">*</span><span class="br0">&#41;</span>notification<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; UIViewAnimationCurve animationCurve <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span><span class="br0">&#91;</span>notification userInfo<span class="br0">&#93;</span> valueForKey<span class="sy0">:</span> UIKeyboardAnimationCurveUserInfoKey<span class="br0">&#93;</span> intValue<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; NSTimeInterval animationDuration <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span><span class="br0">&#91;</span>notification userInfo<span class="br0">&#93;</span> valueForKey<span class="sy0">:</span> UIKeyboardAnimationDurationUserInfoKey<span class="br0">&#93;</span> doubleValue<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; CGRect keyboardBounds <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSValue_Class/"><span class="kw5">NSValue</span></a> <span class="sy0">*</span><span class="br0">&#41;</span><span class="br0">&#91;</span><span class="br0">&#91;</span>notification userInfo<span class="br0">&#93;</span> objectForKey<span class="sy0">:</span> UIKeyboardFrameBeginUserInfoKey<span class="br0">&#93;</span> CGRectValue<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>UIView beginAnimations<span class="sy0">:</span><span class="kw2">nil</span> context<span class="sy0">:</span> <span class="kw2">nil</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>UIView setAnimationCurve<span class="sy0">:</span>animationCurve<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>UIView setAnimationDuration<span class="sy0">:</span>animationDuration<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>aToolBar setFrame<span class="sy0">:</span>CGRectMake<span class="br0">&#40;</span>0.0f, self.view.frame.size.height <span class="sy0">-</span> keyboardBounds.size.height <span class="sy0">-</span> aToolBar.frame.size.height, &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; aToolBar.frame.size.width, aToolBar.frame.size.height<span class="br0">&#41;</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>UIView commitAnimations<span class="br0">&#93;</span>;<br />
<span class="br0">&#125;</span></div>
</div>
<p>The above code establishes the synchronization between keyboard and tool bar. We can write the same code to implement the keyboardWillHide method except tool bar frame. i,e Copy the same code with the following tool bar frame. </p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="br0">&#91;</span>aToolBar setFrame<span class="sy0">:</span>CGRectMake<span class="br0">&#40;</span>0.0f, self.view.frame.size.height <span class="sy0">-</span> 46.0f, aToolBar.frame.size.width, aToolBar.frame.size.height<span class="br0">&#41;</span><span class="br0">&#93;</span>;</div>
</div>
<p>Now add the following action method at the end of .m file and connect it with dismiss button placed in tool bar item.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="sy0">-</span> <span class="br0">&#40;</span>IBAction<span class="br0">&#41;</span>dismiss<span class="sy0">:</span><span class="br0">&#40;</span><span class="kw4">id</span><span class="br0">&#41;</span>sender<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="br0">&#91;</span>aTextField resignFirstResponder<span class="br0">&#93;</span>;<br />
<span class="br0">&#125;</span></div>
</div>
<p>Now build and run the code you will see the tool bar animation along with the keyboard. If you tap on dismiss button then tool bar will again animate along with keyboard hide animation.<br />
<a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/05/ScreenShot_4.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/05/ScreenShot_4.png" alt="" title="ScreenShot_4" width="198" height="372" class="alignnone size-full wp-image-4937" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.edumobile.org/iphone/iphone-apps/toolbar-animation-along-with-the-keyboard/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Orientation Changes for iPhone</title>
		<link>http://www.edumobile.org/iphone/iphone-programming-tutorials/orientation-changes-for-iphone/</link>
		<comments>http://www.edumobile.org/iphone/iphone-programming-tutorials/orientation-changes-for-iphone/#comments</comments>
		<pubDate>Fri, 03 May 2013 06:43:08 +0000</pubDate>
		<dc:creator>Sushant</dc:creator>
				<category><![CDATA[iPhone Game Programming]]></category>
		<category><![CDATA[iPhone Programming Tutorials]]></category>
		<category><![CDATA[free iPhone Orientation tutorials]]></category>
		<category><![CDATA[iPhone Orientation tutorials]]></category>
		<category><![CDATA[Orientation Changes for iPhone]]></category>
		<category><![CDATA[Orientation Changes iPhone]]></category>
		<category><![CDATA[Orientation iPhone]]></category>

		<guid isPermaLink="false">http://www.edumobile.org/iphone/?p=4222</guid>
		<description><![CDATA[There are two ways to change the orientation of controls when the device orientation changes. The simplest way is to use the controls found in the Size Inspector (shown here) to set behavior for individual controls. If these settings do not allow the control that you need, the only other option is to change the [...]]]></description>
			<content:encoded><![CDATA[<p>There are two ways to change the orientation of controls when the device orientation changes. The simplest way is to use the controls found in the Size Inspector (shown here) to set behavior for individual controls. </p>
<p><a href="http://www.edumobile.org/iphone/wp-content/uploads/2012/09/Image0011.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2012/09/Image0011.png" alt="" title="Image001" width="261" height="298" class="alignnone size-full wp-image-4223" /></a></p>
<p>If these settings do not allow the control that you need, the only other option is to change the view controller (and the view) when the orientation of the device changes. In this blog, we&#8217;ll show you how to do this. Let&#8217;s get started!</p>
<p>Start Xcode, select “Create a new Xcode project,” then choose the Empty Application template. Click Next, name the project “Orientation,” and choose options as shown:</p>
<p><a href="http://www.edumobile.org/iphone/wp-content/uploads/2012/09/Image0021.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2012/09/Image0021.png" alt="" title="Image002" width="569" height="393" class="alignnone size-full wp-image-4224" /></a></p>
<p>Click Next, choose a location to save the project, and then click Create.</p>
<p>The first thing we will need to do is add a UIViewController (with a corresponding .xib). This will become the view controller for the Portrait orientation. We will then add another view controller for landscape orientation. </p>
<p>To add these view controllers, select File | New > File&#8230; from the menu, select the Objective – C class template, and make sure the “Subclass Of” box reads “UIViewController.” Name the first view controller “PortraitViewController” and the second “LandscapeViewController,” and for each, create a nib file for the user interface. Save each in the default location. When you are finished, the navigator should look like this:</p>
<p><a href="http://www.edumobile.org/iphone/wp-content/uploads/2012/09/Image0031.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2012/09/Image0031.png" alt="" title="Image003" width="256" height="234" class="alignnone size-full wp-image-4225" /></a></p>
<p>Open PortraitViewController.xib and delete the view. Drag a new UIView object to the screen, open the Size Inspector panel and set the height of the view to 480 as shown:</p>
<p><a href="http://www.edumobile.org/iphone/wp-content/uploads/2012/09/Image004.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2012/09/Image004.png" alt="" title="Image004" width="261" height="301" class="alignnone size-full wp-image-4226" /></a></p>
<p>(This is necessary because we cannot change the height of a template generated top level view.) Right – click the File&#8217;s Owner object, and drag from the circle to the right of the view object to the new View just created. Change the color of the view, and drag a label to the view, changing its text to “Portrait.” </p>
<p>In the LandscapeViewController.xib file, first delete the view as above, then drag a new UIView object to the screen. In the size inspector, change the width to 480 and the height to 320. Give this view a different background color than the portrait View, drag a label to the view, and change its text to “Landscape.” Remember to bind the new view to the view object in the File&#8217;s Owner as we did for the PortraitViewController. When finished, the two views should look like this:</p>
<p><a href="http://www.edumobile.org/iphone/wp-content/uploads/2012/09/Orientation1.jpg"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2012/09/Orientation1.jpg" alt="" title="Orientation1" width="564" height="301" class="alignnone size-full wp-image-4227" /></a></p>
<p>Open LandscapeViewController.m and make this change to the shouldAutorotateToInterfaceOrientation: method:</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">BOOL</span><span class="br0">&#41;</span>shouldAutorotateToInterfaceOrientation<span class="sy0">:</span><span class="br0">&#40;</span>UIInterfaceOrientation<span class="br0">&#41;</span>interfaceOrientation<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> <span class="br0">&#40;</span>interfaceOrientation <span class="sy0">==</span> <span class="br0">&#40;</span>UIInterfaceOrientationLandscapeLeft |<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UIDeviceOrientationLandscapeRight<span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
</div>
<p>This method must return YES for all supported orientations for the view controller. Here, we&#8217;re supporting both landscape orientations. Note the use of the bitwise OR operator ( | ) here. </p>
<p>Now open AppDelegate.h and make these changes:</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="co1">#import &lt;UIKit/UIKit.h&gt;</span><br />
<span class="co1">#import &quot;PortraitViewController.h&quot;</span></p>
<p><span class="kw1">@interface</span> AppDelegate <span class="sy0">:</span> UIResponder &lt;UIApplicationDelegate&gt;</p>
<p><span class="kw1">@property</span> <span class="br0">&#40;</span>strong, nonatomic<span class="br0">&#41;</span> UIWindow <span class="sy0">*</span>window;<br />
<span class="kw1">@property</span> <span class="br0">&#40;</span>strong, nonatomic<span class="br0">&#41;</span> PortraitViewController <span class="sy0">*</span>rootVC;</p>
<p><span class="kw1">@end</span></div>
</div>
<p>We&#8217;ve imported PortraitViewController.h and set up a property named rootVC. Make these changes in the applicationDidFinishLaunching: withOptions: method of AppDelegate.m:</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="co1">#import &quot;AppDelegate.h&quot;</span></p>
<p><span class="kw1">@implementation</span> AppDelegate</p>
<p><span class="kw1">@synthesize</span> window <span class="sy0">=</span> _window;<br />
<span class="kw1">@synthesize</span> rootVC <span class="sy0">=</span> _rootVC;</p>
<p><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">BOOL</span><span class="br0">&#41;</span>application<span class="sy0">:</span><span class="br0">&#40;</span>UIApplication <span class="sy0">*</span><span class="br0">&#41;</span>application didFinishLaunchingWithOptions<span class="sy0">:</span><span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/"><span class="kw5">NSDictionary</span></a> <span class="sy0">*</span><span class="br0">&#41;</span>launchOptions<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; self.window <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>UIWindow alloc<span class="br0">&#93;</span> initWithFrame<span class="sy0">:</span><span class="br0">&#91;</span><span class="br0">&#91;</span>UIScreen mainScreen<span class="br0">&#93;</span> bounds<span class="br0">&#93;</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="co2">// Override point for customization after application launch.</span><br />
&nbsp; &nbsp; self.rootVC <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>PortraitViewController alloc<span class="br0">&#93;</span> init<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; self.window.backgroundColor <span class="sy0">=</span> <span class="br0">&#91;</span>UIColor whiteColor<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#91;</span>self.window addSubview<span class="sy0">:</span>self.rootVC.view<span class="br0">&#93;</span>; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; <span class="br0">&#91;</span>self.window makeKeyAndVisible<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">YES</span>;<br />
<span class="br0">&#125;</span></div>
</div>
<p>After synthesizing the rootVC, we instantiate it using the init method, rather than initWithNibName: bundle:. PortraitViewController will call through to super&#8217;s initWithNibName: bundle: ; we&#8217;ll see that shortly. </p>
<p>Now open PortraitViewController.h and make these changes:</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="co1">#import &lt;UIKit/UIKit.h&gt;</span><br />
<span class="co1">#import &quot;LandscapeViewController.h&quot;</span></p>
<p><span class="kw1">@interface</span> PortraitViewController <span class="sy0">:</span> UIViewController</p>
<p><span class="kw1">@property</span> <span class="br0">&#40;</span>nonatomic, assign<span class="br0">&#41;</span> <span class="kw4">BOOL</span> viewIsLandscape;<br />
<span class="kw1">@property</span> <span class="br0">&#40;</span>nonatomic, strong<span class="br0">&#41;</span> LandscapeViewController <span class="sy0">*</span>landscapeVC;</p>
<p><span class="kw1">@end</span></div>
</div>
<p>We&#8217;ve imported LandscapeViewController, and made a property of it (landscapeVC). Make these changes to PortraitViewController.m:</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="co1">#import &quot;PortraitViewController.h&quot;</span></p>
<p><span class="kw1">@interface</span> PortraitViewController <span class="br0">&#40;</span><span class="br0">&#41;</span></p>
<p><span class="kw1">@end</span></p>
<p><span class="kw1">@implementation</span> PortraitViewController</p>
<p><span class="kw1">@synthesize</span> viewIsLandscape;<br />
<span class="kw1">@synthesize</span> landscapeVC;</p>
<p><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">id</span><span class="br0">&#41;</span>init<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; self <span class="sy0">=</span> <span class="br0">&#91;</span>super initWithNibName<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;PortraitViewController&quot;</span> bundle<span class="sy0">:</span><span class="kw2">nil</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>self<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; self.viewIsLandscape <span class="sy0">=</span> <span class="kw2">NO</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; self.landscapeVC <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>LandscapeViewController alloc<span class="br0">&#93;</span> initWithNibName<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;LandscapeViewController&quot;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bundle<span class="sy0">:</span><span class="kw2">nil</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span><span class="br0">&#91;</span>UIDevice currentDevice<span class="br0">&#93;</span> beginGeneratingDeviceOrientationNotifications<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span><span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/"><span class="kw5">NSNotificationCenter</span></a> defaultCenter<span class="br0">&#93;</span> addObserver<span class="sy0">:</span>self<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;selector<span class="sy0">:</span><span class="kw1">@selector</span><span class="br0">&#40;</span>orientationChanged<span class="sy0">:</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;name<span class="sy0">:</span>UIDeviceOrientationDidChangeNotification<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;object<span class="sy0">:</span><span class="kw2">nil</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> self;<br />
<span class="br0">&#125;</span></p>
<p><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>orientationChanged<span class="sy0">:</span><span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSNotification_Class/"><span class="kw5">NSNotification</span></a> <span class="sy0">*</span><span class="br0">&#41;</span>notification<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; UIDeviceOrientation devOrientation <span class="sy0">=</span> <span class="br0">&#91;</span>UIDevice currentDevice<span class="br0">&#93;</span>.orientation;<br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>UIDeviceOrientationIsLandscape<span class="br0">&#40;</span>devOrientation<span class="br0">&#41;</span> <span class="sy0">&amp;&amp;</span> <span class="sy0">!</span>self.viewIsLandscape <span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>self presentModalViewController<span class="sy0">:</span>self.landscapeVC animated<span class="sy0">:</span><span class="kw2">YES</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; self.viewIsLandscape <span class="sy0">=</span> <span class="kw2">YES</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="kw1">if</span> <span class="br0">&#40;</span>UIDeviceOrientationIsPortrait<span class="br0">&#40;</span>devOrientation<span class="br0">&#41;</span> <span class="sy0">&amp;&amp;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self.viewIsLandscape<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>self dismissModalViewControllerAnimated<span class="sy0">:</span><span class="kw2">YES</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; self.viewIsLandscape <span class="sy0">=</span> <span class="kw2">NO</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span><br />
<span class="co2">//&#8230;</span></div>
</div>
<p>(Leave the rest of the methods of the ViewController alone, just add these methods at the top, below the @implementation line). Here, we&#8217;ve overridden init to call super&#8217;s initWithNibName: bundle:, we&#8217;ve provided PortraitViewController.xib as the nib name. In the init method, we&#8217;ve also subscribed to the UIDeviceOrientationDidChangeNotification in the default notification center and provided the method “orientationChanged:” to be fired when this event occurs. </p>
<p>In orientationChanged, we detect the current device orientation, and either present the landscapeVC object as a modal view controller (if the device entered landscape orientation), or dismiss it (if the device entered portrait orientation). Note the use of the viewIsLandscape property to control the display of the proper view controller.</p>
<p>Now run the project. When the orientation changes, you should see the changes to the view take place:</p>
<p><a href="http://www.edumobile.org/iphone/wp-content/uploads/2012/09/Orientation2.jpg"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2012/09/Orientation2.jpg" alt="" title="Orientation2" width="564" height="301" class="alignnone size-full wp-image-4228" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.edumobile.org/iphone/iphone-programming-tutorials/orientation-changes-for-iphone/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Multiple row selection in iPhone table view</title>
		<link>http://www.edumobile.org/iphone/iphone-apps/multiple-row-selection-in-iphone-table-view/</link>
		<comments>http://www.edumobile.org/iphone/iphone-apps/multiple-row-selection-in-iphone-table-view/#comments</comments>
		<pubDate>Tue, 30 Apr 2013 11:07:33 +0000</pubDate>
		<dc:creator>Sushant</dc:creator>
				<category><![CDATA[iPhone Apps]]></category>
		<category><![CDATA[iPhone Game Programming]]></category>
		<category><![CDATA[Multiple row selection]]></category>
		<category><![CDATA[Multiple row selection in table view]]></category>
		<category><![CDATA[Multiple row selection table view]]></category>
		<category><![CDATA[Multiple row selection table view iphone]]></category>
		<category><![CDATA[Multiple row selection table view tutorials]]></category>

		<guid isPermaLink="false">http://www.edumobile.org/iphone/?p=4909</guid>
		<description><![CDATA[In this tutorial I will explain the most powerful feature of UITableView, called multiple selection of rows. Create a new project in Xcode using single window application template and name it as TableViewMultiSelection. Before proceeding further we need to add a navigation controller to the AppDelegate and make ViewController as its root view. We can [...]]]></description>
			<content:encoded><![CDATA[<p>In this tutorial I will explain the most powerful feature of UITableView, called multiple selection of rows. Create a new project in Xcode using single window application template and name it as TableViewMultiSelection. Before proceeding further we need to add a navigation controller to the AppDelegate and make ViewController as its root view. We can do this either by adding UINavigationController and its root view in AppDelegate.m file or by using nib file of ViewController class. I am not digging into the details of the navigation code because it is quite common in all applications. Now open ViewController.xib file, add a table view outlet and connect it with the associated outlet in .h file. Open ViewController.h file and modify it as follows.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="kw1">@interface</span> ViewController <span class="sy0">:</span> UIViewController&lt;UITableViewDataSource, UITableViewDelegate, UIActionSheetDelegate&gt;</p>
<p><span class="kw1">@property</span> <span class="br0">&#40;</span>nonatomic, strong<span class="br0">&#41;</span> IBOutlet UITableView <span class="sy0">*</span>tableView;<br />
<span class="kw1">@property</span> <span class="br0">&#40;</span>nonatomic, strong<span class="br0">&#41;</span> <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/"><span class="kw5">NSMutableArray</span></a> <span class="sy0">*</span>dataArray;<br />
<span class="kw1">@property</span> <span class="br0">&#40;</span>nonatomic, strong<span class="br0">&#41;</span> UIBarButtonItem <span class="sy0">*</span>editButton;<br />
<span class="kw1">@property</span> <span class="br0">&#40;</span>nonatomic, strong<span class="br0">&#41;</span> UIBarButtonItem <span class="sy0">*</span>cancelButton;<br />
<span class="kw1">@property</span> <span class="br0">&#40;</span>nonatomic, strong<span class="br0">&#41;</span> UIBarButtonItem <span class="sy0">*</span>addButton;<br />
<span class="kw1">@property</span> <span class="br0">&#40;</span>nonatomic, strong<span class="br0">&#41;</span> UIBarButtonItem <span class="sy0">*</span>deleteButton;</p>
<p><span class="kw1">@end</span></div>
</div>
<p>Multiple selection of rows in UITableView are enabled by adding the following line in ViewDidLoad of ViewController.m file.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;">self.tableView.allowsMultipleSelectionDuringEditing <span class="sy0">=</span> <span class="kw2">YES</span>;</div>
</div>
<p>The default value of this property is set to NO. Generally this property returns a boolean value that controls whether users can select more than one cell simultaneously in editing mode. If you set to YES, check marks appear next to selected rows in editing mode. In addition, UITableView does not query for editing styles when it goes into editing mode. If you call indexPathsForSelectedRows, you can get the index paths that identify the selected rows. Now we need to allocate edit, add, cancel and delete bar buttons and its action methods. For that we need to modify the ViewDidLoad method by adding the following lines of code.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;">self.dataArray <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/"><span class="kw5">NSMutableArray</span></a> alloc<span class="br0">&#93;</span> initWithObjects<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;Item 1&quot;</span>,<span class="co3">@</span><span class="st0">&quot;Item 2&quot;</span>,<span class="co3">@</span><span class="st0">&quot;Item 3&quot;</span>,<span class="co3">@</span><span class="st0">&quot;Item 4&quot;</span>,<span class="co3">@</span><span class="st0">&quot;Item 5&quot;</span>,<span class="kw2">nil</span><span class="br0">&#93;</span>;<br />
self.editButton <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>UIBarButtonItem alloc<span class="br0">&#93;</span> initWithTitle<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;Edit&quot;</span> style<span class="sy0">:</span>UIBarButtonItemStyleBordered target<span class="sy0">:</span>self action<span class="sy0">:</span><span class="kw1">@selector</span><span class="br0">&#40;</span>editButton<span class="sy0">:</span><span class="br0">&#41;</span><span class="br0">&#93;</span>;<br />
self.cancelButton <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>UIBarButtonItem alloc<span class="br0">&#93;</span> initWithTitle<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;Cancel&quot;</span> style<span class="sy0">:</span>UIBarButtonItemStyleBordered target<span class="sy0">:</span>self action<span class="sy0">:</span><span class="kw1">@selector</span><span class="br0">&#40;</span>cancelButton<span class="sy0">:</span><span class="br0">&#41;</span><span class="br0">&#93;</span>;<br />
self.addButton <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>UIBarButtonItem alloc<span class="br0">&#93;</span> initWithTitle<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;Add&quot;</span> style<span class="sy0">:</span>UIBarButtonItemStyleBordered target<span class="sy0">:</span>self action<span class="sy0">:</span><span class="kw1">@selector</span><span class="br0">&#40;</span>addButton<span class="sy0">:</span><span class="br0">&#41;</span><span class="br0">&#93;</span>;<br />
self.deleteButton <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>UIBarButtonItem alloc<span class="br0">&#93;</span> initWithTitle<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;Delete&quot;</span> style<span class="sy0">:</span>UIBarButtonItemStyleBordered target<span class="sy0">:</span>self action<span class="sy0">:</span><span class="kw1">@selector</span><span class="br0">&#40;</span>deleteButton<span class="sy0">:</span><span class="br0">&#41;</span><span class="br0">&#93;</span>;<br />
self.deleteButton.tintColor <span class="sy0">=</span> <span class="br0">&#91;</span>UIColor redColor<span class="br0">&#93;</span>;<br />
<span class="br0">&#91;</span>self.navigationItem setRightBarButtonItem<span class="sy0">:</span>self.editButton<span class="br0">&#93;</span>;<br />
<span class="br0">&#91;</span>self.navigationItem setLeftBarButtonItem<span class="sy0">:</span>self.addButton<span class="br0">&#93;</span>;</div>
</div>
<p>The last two lines of code indicates edit and add buttons are placed in right and left sides of navigation bar respectively. Implement the edit action method as follows.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>editButton<span class="sy0">:</span><span class="br0">&#40;</span><span class="kw4">id</span><span class="br0">&#41;</span>sender<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; self.navigationItem.rightBarButtonItem <span class="sy0">=</span> self.cancelButton;<br />
&nbsp; &nbsp; self.deleteButton.title <span class="sy0">=</span> kDeleteAllTitle;<br />
&nbsp; &nbsp; self.navigationItem.leftBarButtonItem <span class="sy0">=</span> self.deleteButton;<br />
&nbsp; &nbsp; <span class="br0">&#91;</span>self.tableView setEditing<span class="sy0">:</span><span class="kw2">YES</span> animated<span class="sy0">:</span><span class="kw2">YES</span><span class="br0">&#93;</span>;<br />
<span class="br0">&#125;</span></div>
</div>
<p>After taping the edit button, immediately it will turn up as a cancel button. The number of rows in table view will be the length of dataArray object. Modify the cellForRowAtIndexPath method in such a way to display the content of dataArray object based on its index. Following code line will display each rows content.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;">cell.textLabel.text <span class="sy0">=</span> <span class="br0">&#91;</span>self.dataArray objectAtIndex<span class="sy0">:</span>indexPath.row<span class="br0">&#93;</span>;</div>
</div>
<p>Build and run the code you will see the following screenshot flow. Screenshot_1 appears as a first screen, if you tap on edit button you will see the screenshot_2 and after selecting first two rows you will see the screenshot_3. </p>
<table width="100%">
<tr>
<td><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/ScreenShot_12.png" alt="" title="ScreenShot_1" width="197" height="345" class="alignleft size-full wp-image-4910" /></a><a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/ScreenShot_22.png"></td>
<td><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/ScreenShot_22.png" alt="" title="ScreenShot_2" width="197" height="345" class="alignnone size-full wp-image-4911" /></a><a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/ScreenShot_32.png"></td>
<td><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/ScreenShot_32.png" alt="" title="ScreenShot_3" width="197" height="345" class="alignright size-full wp-image-4912" /></td>
</tr>
</table>
<p>Now we will customize the delete button text to show the number of selected rows as its text. Add the following lines of code on top of ViewController.m file.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="kw4">static</span> <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a> <span class="sy0">*</span>kDeleteAllTitle <span class="sy0">=</span> <span class="co3">@</span><span class="st0">&quot;Delete All&quot;</span>;<br />
<span class="kw4">static</span> <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a> <span class="sy0">*</span>kDeletePartialTitle <span class="sy0">=</span> <span class="co3">@</span><span class="st0">&quot;Delete (%d)&quot;</span>;</div>
</div>
<p>Implement didDeselectRowAtIndexPath and didSelectRowAtIndexPath as follows.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>tableView<span class="sy0">:</span><span class="br0">&#40;</span>UITableView <span class="sy0">*</span><span class="br0">&#41;</span>tableView didDeselectRowAtIndexPath<span class="sy0">:</span><span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSIndexPath_Class/"><span class="kw5">NSIndexPath</span></a> <span class="sy0">*</span><span class="br0">&#41;</span>indexPath<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>self.tableView.isEditing<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/"><span class="kw5">NSArray</span></a> <span class="sy0">*</span>selectedRows <span class="sy0">=</span> <span class="br0">&#91;</span>self.tableView indexPathsForSelectedRows<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; self.deleteButton.title <span class="sy0">=</span> <span class="br0">&#40;</span>selectedRows.count <span class="sy0">==</span> 0<span class="br0">&#41;</span> ?<br />
&nbsp; &nbsp; &nbsp; &nbsp; kDeleteAllTitle <span class="sy0">:</span> <span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a> stringWithFormat<span class="sy0">:</span>kDeletePartialTitle, selectedRows.count<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
<p><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>tableView<span class="sy0">:</span><span class="br0">&#40;</span>UITableView <span class="sy0">*</span><span class="br0">&#41;</span>tableView didSelectRowAtIndexPath<span class="sy0">:</span><span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSIndexPath_Class/"><span class="kw5">NSIndexPath</span></a> <span class="sy0">*</span><span class="br0">&#41;</span>indexPath<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>self.tableView.isEditing<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/"><span class="kw5">NSArray</span></a> <span class="sy0">*</span>selectedRows <span class="sy0">=</span> <span class="br0">&#91;</span>self.tableView indexPathsForSelectedRows<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a> <span class="sy0">*</span>deleteButtonTitle <span class="sy0">=</span> <span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a> stringWithFormat<span class="sy0">:</span>kDeletePartialTitle, selectedRows.count<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>selectedRows.count <span class="sy0">==</span> self.dataArray.count<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; deleteButtonTitle <span class="sy0">=</span> kDeleteAllTitle;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; self.deleteButton.title <span class="sy0">=</span> deleteButtonTitle;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>Role of above delegate methods is to modify the text of delete button based on number selected or unselected rows. Finally we ned to implement the action methods of delete and add buttons. In delete button action method, we use UIActionSheet to populate set of options like delete selected rows or cancel the deleted action. And after that we implement UIActionSheet&#8217;s delegate method to perform delete or cancel actions by registering its delegate protocol. Delete and add action methods look like this.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>deleteButton<span class="sy0">:</span><span class="br0">&#40;</span><span class="kw4">id</span><span class="br0">&#41;</span>sender<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a> <span class="sy0">*</span>actionTitle <span class="sy0">=</span> <span class="br0">&#40;</span><span class="br0">&#91;</span><span class="br0">&#91;</span>self.tableView indexPathsForSelectedRows<span class="br0">&#93;</span> count<span class="br0">&#93;</span> <span class="sy0">==</span> <span class="nu0">1</span><span class="br0">&#41;</span> ?<br />
&nbsp; &nbsp; <span class="co3">@</span><span class="st0">&quot;Are you sure you want to remove this item?&quot;</span> <span class="sy0">:</span> <span class="co3">@</span><span class="st0">&quot;Are you sure you want to remove these items?&quot;</span>; <br />
&nbsp; &nbsp; UIActionSheet <span class="sy0">*</span>actionSheet <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>UIActionSheet alloc<span class="br0">&#93;</span> initWithTitle<span class="sy0">:</span>actionTitle delegate<span class="sy0">:</span>self cancelButtonTitle<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;Cancel&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;destructiveButtonTitle<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;OK&quot;</span> otherButtonTitles<span class="sy0">:</span><span class="kw2">nil</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp;actionSheet.actionSheetStyle <span class="sy0">=</span> UIActionSheetStyleDefault;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#91;</span>actionSheet showInView<span class="sy0">:</span>self.view<span class="br0">&#93;</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>addButton<span class="sy0">:</span><span class="br0">&#40;</span><span class="kw4">id</span><span class="br0">&#41;</span>sender<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="br0">&#91;</span>self.tableView beginUpdates<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#91;</span>self.dataArray addObject<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;New Item&quot;</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/"><span class="kw5">NSArray</span></a> <span class="sy0">*</span>paths <span class="sy0">=</span> <span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/"><span class="kw5">NSArray</span></a> arrayWithObject<span class="sy0">:</span><span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSIndexPath_Class/"><span class="kw5">NSIndexPath</span></a> indexPathForRow<span class="sy0">:</span><span class="br0">&#40;</span>self.dataArray.count <span class="sy0">-</span> 1<span class="br0">&#41;</span> inSection<span class="sy0">:</span>0<span class="br0">&#93;</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#91;</span>self.tableView insertRowsAtIndexPaths<span class="sy0">:</span>paths withRowAnimation<span class="sy0">:</span><span class="kw2">NO</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#91;</span>self.tableView endUpdates<span class="br0">&#93;</span>; &nbsp;<br />
&nbsp; &nbsp; <span class="br0">&#91;</span>self.tableView scrollToRowAtIndexPath<span class="sy0">:</span><span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSIndexPath_Class/"><span class="kw5">NSIndexPath</span></a> indexPathForRow<span class="sy0">:</span><span class="br0">&#40;</span>self.dataArray.count <span class="sy0">-</span> 1<span class="br0">&#41;</span> inSection<span class="sy0">:</span>0<span class="br0">&#93;</span> atScrollPosition<span class="sy0">:</span> UITableViewScrollPositionBottom animated<span class="sy0">:</span><span class="kw2">YES</span><span class="br0">&#93;</span>; <br />
&nbsp; &nbsp; self.editButton.enabled <span class="sy0">=</span> <span class="br0">&#40;</span>self.dataArray.count &gt; 0<span class="br0">&#41;</span> ? <span class="kw2">YES</span> <span class="sy0">:</span> <span class="kw2">NO</span>;<br />
<span class="br0">&#125;</span></div>
</div>
<p>Now build and run the code. You will be able to perform delete action for selected number of rows or we can delete all available rows of table view.<br />
<a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/ScreenShot_42.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/ScreenShot_42.png" alt="" title="ScreenShot_4" width="300" height="595" class="alignnone size-full wp-image-4913" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.edumobile.org/iphone/iphone-apps/multiple-row-selection-in-iphone-table-view/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Table view with insert and delete rows in iPhone</title>
		<link>http://www.edumobile.org/iphone/iphone-apps/table-view-with-insert-and-delete-rows-in-iphone/</link>
		<comments>http://www.edumobile.org/iphone/iphone-apps/table-view-with-insert-and-delete-rows-in-iphone/#comments</comments>
		<pubDate>Fri, 26 Apr 2013 12:32:23 +0000</pubDate>
		<dc:creator>Sushant</dc:creator>
				<category><![CDATA[How to make an app]]></category>
		<category><![CDATA[iPhone Apps]]></category>
		<category><![CDATA[iPhone Beginner Tutorials]]></category>
		<category><![CDATA[free iPhone tutorials]]></category>
		<category><![CDATA[Table view with insert and delete rows]]></category>
		<category><![CDATA[Table view with insert and delete rows example]]></category>
		<category><![CDATA[Table view with insert and delete rows in iPhone]]></category>
		<category><![CDATA[Table view with insert and delete rows iPhone]]></category>
		<category><![CDATA[Table view with insert and delete rows iPhone tutorials]]></category>
		<category><![CDATA[Table view with insert and delete rows tutorials]]></category>

		<guid isPermaLink="false">http://www.edumobile.org/iphone/?p=4885</guid>
		<description><![CDATA[A very common feature for almost all iOS applications is UITableView and its data source and delegate protocol. In this tutorial I will explain how to edit table view rows with little animations. i.e I will discuss how to delete and insert a row within the table view. Open Xcode and proceed with the single [...]]]></description>
			<content:encoded><![CDATA[<p>A very common feature for almost all iOS applications is UITableView and its data source and delegate protocol. In this tutorial I will explain how to edit table view rows with little animations. i.e I will discuss how to delete and insert a row within the table view.<br />
Open Xcode and proceed with the single view application template. In AppDelegate.m file modify the code such that ViewController becomes the root view of the navigation controller by adding the following code.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;">UINavigationController <span class="sy0">*</span>navController <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>UINavigationController alloc<span class="br0">&#93;</span>initWithRootViewController<span class="sy0">:</span> self.viewController<span class="br0">&#93;</span>;<br />
self.window.rootViewController <span class="sy0">=</span> navController;</div>
</div>
<p>In ViewController.xib file drag UITableView object and connect it with the associated outlet in .h file. Also register with UITableView data source and delegate protocols. Now open ViewController.h file and modify as follows.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="kw1">@interface</span> ViewController <span class="sy0">:</span> UIViewController&lt;UITableViewDelegate, UITableViewDataSource&gt;</p>
<p><span class="kw1">@property</span> <span class="br0">&#40;</span>nonatomic, strong<span class="br0">&#41;</span> IBOutlet UITableView <span class="sy0">*</span>aTableView;<br />
<span class="kw1">@property</span> <span class="br0">&#40;</span>nonatomic, strong<span class="br0">&#41;</span> <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/"><span class="kw5">NSMutableArray</span></a> <span class="sy0">*</span>dataArray;</p>
<p><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>addORDeleteRows;</p>
<p><span class="kw1">@end</span></div>
</div>
<p>Open viewDidLoad method of ViewController.m file and add the below code.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;">self.dataArray <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/"><span class="kw5">NSMutableArray</span></a> alloc<span class="br0">&#93;</span> initWithObjects<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;Tiger&quot;</span>,<span class="co3">@</span><span class="st0">&quot;Leopard&quot;</span>,<span class="co3">@</span><span class="st0">&quot;Snow Leopard&quot;</span>,<span class="co3">@</span><span class="st0">&quot;Lion&quot;</span>,<span class="kw2">nil</span><span class="br0">&#93;</span>;<br />
self.title <span class="sy0">=</span> <span class="co3">@</span><span class="st0">&quot;Data Table&quot;</span>;<br />
UIBarButtonItem <span class="sy0">*</span>addButton <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>UIBarButtonItem alloc<span class="br0">&#93;</span> initWithTitle<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;Edit&quot;</span> style<span class="sy0">:</span> UIBarButtonItemStyleBordered target<span class="sy0">:</span>self action<span class="sy0">:</span><span class="kw1">@selector</span><span class="br0">&#40;</span>addORDeleteRows<span class="br0">&#41;</span><span class="br0">&#93;</span>;<span class="br0">&#91;</span>self.navigationItem setLeftBarButtonItem<span class="sy0">:</span>addButton<span class="br0">&#93;</span>;</div>
</div>
<p>Here dataArray contains string objects which we display in table view. Our purpose of this tutorial is editing table view in such a way to delete or insert rows. For that we need a bar button on top of navigation bar. Hence I added UIBarButtonItem named Edit and registered its action with addORDeleteRows method. Now we need to implement this method by adding the following lines of code.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>addORDeleteRows<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>self.editing<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>super setEditing<span class="sy0">:</span><span class="kw2">NO</span> animated<span class="sy0">:</span><span class="kw2">NO</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>aTableView setEditing<span class="sy0">:</span><span class="kw2">NO</span> animated<span class="sy0">:</span><span class="kw2">NO</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>aTableView reloadData<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>self.navigationItem.leftBarButtonItem setTitle<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;Edit&quot;</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>self.navigationItem.leftBarButtonItem setStyle<span class="sy0">:</span>UIBarButtonItemStylePlain<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>super setEditing<span class="sy0">:</span><span class="kw2">YES</span> animated<span class="sy0">:</span><span class="kw2">YES</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>aTableView setEditing<span class="sy0">:</span><span class="kw2">YES</span> animated<span class="sy0">:</span><span class="kw2">YES</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>aTableView reloadData<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>self.navigationItem.leftBarButtonItem setTitle<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;Done&quot;</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>self.navigationItem.leftBarButtonItem setStyle<span class="sy0">:</span>UIBarButtonItemStyleDone<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>From the above code, the property &#8220;editing&#8221; is a boolean type variable which indicates whether table view is in edit mode or not. Else part of if statement contains other variation of Edit button. i.e. after tapping edit button, table view becomes editable and our button text changed to Done. Now we need to implement the table view data source methods for number of rows and its content. Add the following code into your project.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="sy0">-</span> <span class="br0">&#40;</span>NSInteger<span class="br0">&#41;</span>tableView<span class="sy0">:</span><span class="br0">&#40;</span>UITableView <span class="sy0">*</span><span class="br0">&#41;</span>tableView numberOfRowsInSection<span class="sy0">:</span><span class="br0">&#40;</span>NSInteger<span class="br0">&#41;</span>section<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> count <span class="sy0">=</span> <span class="br0">&#91;</span>self.dataArray count<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>self.editing<span class="br0">&#41;</span> count<span class="sy0">++</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> count;<br />
<span class="br0">&#125;</span></p>
<p><span class="sy0">-</span> <span class="br0">&#40;</span>UITableViewCell <span class="sy0">*</span><span class="br0">&#41;</span>tableView<span class="sy0">:</span><span class="br0">&#40;</span>UITableView <span class="sy0">*</span><span class="br0">&#41;</span>tableView cellForRowAtIndexPath<span class="sy0">:</span><span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSIndexPath_Class/"><span class="kw5">NSIndexPath</span></a> <span class="sy0">*</span><span class="br0">&#41;</span>indexPath <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw4">static</span> <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a> <span class="sy0">*</span>CellIdentifier <span class="sy0">=</span> <span class="co3">@</span><span class="st0">&quot;Cell&quot;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; UITableViewCell <span class="sy0">*</span>cell <span class="sy0">=</span> <span class="br0">&#91;</span>tableView dequeueReusableCellWithIdentifier<span class="sy0">:</span>CellIdentifier<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>cell <span class="sy0">==</span> <span class="kw2">nil</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; cell <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>UITableViewCell alloc<span class="br0">&#93;</span>initWithStyle<span class="sy0">:</span>UITableViewCellStyleDefault reuseIdentifier<span class="sy0">:</span>CellIdentifier<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cell.editingAccessoryType <span class="sy0">=</span> <span class="kw2">YES</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw4">int</span> count <span class="sy0">=</span> <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>self.editing <span class="sy0">&amp;&amp;</span> indexPath.row <span class="sy0">!=</span> 0<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count <span class="sy0">=</span> <span class="nu0">1</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>indexPath.row <span class="sy0">==</span> <span class="br0">&#40;</span><span class="br0">&#91;</span>self.dataArray count<span class="br0">&#93;</span><span class="br0">&#41;</span> <span class="sy0">&amp;&amp;</span> self.editing<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cell.textLabel.text <span class="sy0">=</span> <span class="co3">@</span><span class="st0">&quot;Append a new row&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> cell;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; cell.textLabel.text <span class="sy0">=</span> <span class="br0">&#91;</span>self.dataArray objectAtIndex<span class="sy0">:</span>indexPath.row<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; cell.accessoryType <span class="sy0">=</span> UITableViewCellAccessoryDisclosureIndicator;<br />
&nbsp; &nbsp; <span class="kw1">return</span> cell;<br />
<span class="br0">&#125;</span></div>
</div>
<p>The numberOfRowsInSection method returns one extra row if the table view is in edit mode otherwise it returns the dataArray count. In edit mode cellForRowAtIndexPath method returns an extra cell with content &#8220;Append a new row&#8221; at the end of table view. Now compile and run the code you will see the following screens in normal and edit modes.<br />
			<a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/Screen-Shot_11.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/Screen-Shot_11.png" alt="" title="Screen Shot_1" width="198" height="372" class="alignleft size-full wp-image-4886" /></a>                                                                 <a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/Screen-Shot_21.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/Screen-Shot_21.png" alt="" title="Screen Shot_2" "198" height="372" class="alignnone size-full wp-image-4887" /></a></p>
<p></br><br />
In edit mode all the rows are preceded with &#8220;-&#8221; icon including last row. The purpose of last row is to add the new row at the end of table view. So we need to change the icon of last row. This is possible by adding editingStyleForRowAtIndexPath delegate method.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="sy0">-</span> <span class="br0">&#40;</span>UITableViewCellEditingStyle<span class="br0">&#41;</span>tableView<span class="sy0">:</span><span class="br0">&#40;</span>UITableView <span class="sy0">*</span><span class="br0">&#41;</span>aTableView editingStyleForRowAtIndexPath<span class="sy0">:</span><span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSIndexPath_Class/"><span class="kw5">NSIndexPath</span></a> <span class="sy0">*</span><span class="br0">&#41;</span>indexPath<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>self.editing <span class="sy0">==</span> <span class="kw2">NO</span> || <span class="sy0">!</span>indexPath<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> UITableViewCellEditingStyleNone;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>self.editing <span class="sy0">&amp;&amp;</span> indexPath.row <span class="sy0">==</span> <span class="br0">&#40;</span><span class="br0">&#91;</span>self.dataArray count<span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> UITableViewCellEditingStyleInsert;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> UITableViewCellEditingStyleDelete;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw1">return</span> UITableViewCellEditingStyleNone;<br />
<span class="br0">&#125;</span></div>
</div>
<p>Now compile and run the code you will see the following screen.<br />
						<a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/Screen-Shot_31.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/Screen-Shot_31.png" alt="" title="Screen Shot_3" "198" height="372" class="alignnone size-full wp-image-4888" /></a></p>
<p>No action will be performed if you tap either on &#8220;-&#8221; icon&#8217;s Delete button or &#8220;+&#8221; icon. To delete a row or insert a row at the end of table view we need to over write the UITableViewDataSource method commitEditingStyle as follows.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>tableView<span class="sy0">:</span><span class="br0">&#40;</span>UITableView <span class="sy0">*</span><span class="br0">&#41;</span>tableView commitEditingStyle<span class="sy0">:</span><span class="br0">&#40;</span>UITableViewCellEditingStyle<span class="br0">&#41;</span> editingStyle forRowAtIndexPath<span class="sy0">:</span><span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSIndexPath_Class/"><span class="kw5">NSIndexPath</span></a> <span class="sy0">*</span><span class="br0">&#41;</span>indexPath<br />
<span class="br0">&#123;</span> &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>editingStyle <span class="sy0">==</span> UITableViewCellEditingStyleDelete<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>self.dataArray removeObjectAtIndex<span class="sy0">:</span>indexPath.row<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>aTableView reloadData<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw1">else</span> <span class="kw1">if</span> <span class="br0">&#40;</span>editingStyle <span class="sy0">==</span> UITableViewCellEditingStyleInsert<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>self.dataArray insertObject<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;New version&quot;</span> atIndex<span class="sy0">:</span><span class="br0">&#91;</span>self.dataArray count<span class="br0">&#93;</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>aTableView reloadData<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>Build and run the code, now you will able to insert and delete the rows.<br />
					        <a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/Screen-Shot_41.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/Screen-Shot_41.png" alt="" title="Screen Shot_4" "198" height="372" class="alignnone size-full wp-image-4889" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.edumobile.org/iphone/iphone-apps/table-view-with-insert-and-delete-rows-in-iphone/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tab bar controller tutorial for iPhone</title>
		<link>http://www.edumobile.org/iphone/iphone-apps/tab-bar-controller-tutorial-for-iphone/</link>
		<comments>http://www.edumobile.org/iphone/iphone-apps/tab-bar-controller-tutorial-for-iphone/#comments</comments>
		<pubDate>Tue, 23 Apr 2013 13:08:52 +0000</pubDate>
		<dc:creator>Sushant</dc:creator>
				<category><![CDATA[How to make an app]]></category>
		<category><![CDATA[iPhone Apps]]></category>
		<category><![CDATA[iPhone Business]]></category>
		<category><![CDATA[Tab bar controller]]></category>
		<category><![CDATA[Tab bar controller iPhone]]></category>
		<category><![CDATA[Tab bar controller tutorial]]></category>
		<category><![CDATA[Tab bar controller tutorial for iPhone]]></category>
		<category><![CDATA[Tab bar controller tutorial iPhone]]></category>

		<guid isPermaLink="false">http://www.edumobile.org/iphone/?p=4868</guid>
		<description><![CDATA[In iOS, UITabBarController works as an access point for different view controllers and its views. The tab bar contains set of tabs corresponding to the view controllers that we need to populate. Each tab of a tab bar controller is associated with a custom view controller. When the user selects a specific tab, the tab [...]]]></description>
			<content:encoded><![CDATA[<p>In iOS, UITabBarController works as an access point for different view controllers and its views. The tab bar contains set of tabs corresponding to the view controllers that we need to populate. Each tab of a tab bar controller is associated with a custom view controller. When the user selects a specific tab, the tab bar controller displays the root view of the corresponding view controller, replacing any previous views. Using viewController property of tab bar, we can configure the tabs of a tab bar controller. The order in which you specify the view controllers determines the order in which they appear in the tab bar. Another property selectedViewController determines which controller is selected at a particular time. In this tutorial, I will explain UITabBarController with UINavigationControllers as its tabs.</p>
<p>Open Xcode, start a new project with TabBar application template as follows.</p>
<p><a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/Screen-Shot_1.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/Screen-Shot_1.png" alt="" title="Screen Shot_1" width="574" height="366" class="alignnone size-full wp-image-4869" /></a></p>
<p>By default Xcode creates a tab bar with two tabs and corresponding viewControllers named FirstViewController and SecondViewController with .xib files. It also creates tab bar tabs background images named first and second with .png extension in retina and non retina mode. Delete second.png and rename first.png as tab_image.png. Because I am going to add one more custom tab such that all the three tabs will have the same background image.  Now add a UIButton in first and second viewController .xib files. Build and run the code, you will get the following output. </p>
<table>
<tr>
<td> <a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/Screen-Shot_2.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/Screen-Shot_2.png" alt="" title="Screen Shot_2" width="230" height="450" class="alignnone size-full wp-image-4870" /></a> </td>
<td><a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/Screen-Shot_3.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/Screen-Shot_3.png" alt="" title="Screen Shot_3" width="230" height="450" class="alignnone size-full wp-image-4871" /></a></td>
</tr>
</table>
<p>Suppose you want to push some other view controller from First and SecondViewControllers then we need to add UINavigationController with FirstViewController, SecondViewController as their rootViewControllers. For that, open appDelegate.m file and add/modify the following lines of code.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;">UINavigationController <span class="sy0">*</span>navController1 <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>UINavigationController alloc<span class="br0">&#93;</span> initWithRootViewController<span class="sy0">:</span> viewController1<span class="br0">&#93;</span>;<br />
UINavigationController <span class="sy0">*</span>navController2 <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>UINavigationController alloc<span class="br0">&#93;</span> initWithRootViewController<span class="sy0">:</span> viewController2<span class="br0">&#93;</span>;<br />
self.tabBarController.viewControllers <span class="sy0">=</span> @<span class="br0">&#91;</span>navController1, navController2<span class="br0">&#93;</span>;</div>
</div>
<p>Now run the code. You will see the navigation bar on top of two viewControllers.</p>
<p>If you want to navigate from First and SecondViewController to other controllers then we need to add two new viewControllers. Add new viewControllers named ImageViewController and TableViewController. Add an imageView outlet to ImageViewController.xib file and connect it with the imageView outlet in corresponding .h file. Now open TableViewController.xib file, add a tableView outlet and its dataSource, delegate protocols, also connect it with associated outlet placed in .h file. Open viewDidLoad method of ImageViewController.m file and add the following code.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;">imgView.image <span class="sy0">=</span> <span class="br0">&#91;</span>UIImage imageNamed<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;123.jpg&quot;</span><span class="br0">&#93;</span>;</div>
</div>
<p>Now open TableViewController.m file and add the code as follows.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="sy0">-</span> <span class="br0">&#40;</span>NSInteger<span class="br0">&#41;</span>tableView<span class="sy0">:</span><span class="br0">&#40;</span>UITableView <span class="sy0">*</span><span class="br0">&#41;</span>tableView numberOfRowsInSection<span class="sy0">:</span><span class="br0">&#40;</span>NSInteger<span class="br0">&#41;</span>section<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">5</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="sy0">-</span> <span class="br0">&#40;</span>UITableViewCell <span class="sy0">*</span><span class="br0">&#41;</span>tableView<span class="sy0">:</span><span class="br0">&#40;</span>UITableView <span class="sy0">*</span><span class="br0">&#41;</span>tableView cellForRowAtIndexPath<span class="sy0">:</span><span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSIndexPath_Class/"><span class="kw5">NSIndexPath</span></a> <span class="sy0">*</span><span class="br0">&#41;</span>indexPath<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; UITableViewCell <span class="sy0">*</span>cell <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>UITableViewCell alloc<span class="br0">&#93;</span>initWithStyle<span class="sy0">:</span>UITableViewCellStyleDefault reuseIdentifier<span class="sy0">:</span> <span class="kw2">nil</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; cell.textLabel.text <span class="sy0">=</span> <span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a> stringWithFormat<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;Row &#8212;&#8212;&#8212;- %d&quot;</span>,indexPath.row<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="kw1">return</span> cell;<br />
<span class="br0">&#125;</span></div>
</div>
<p>Build and run the code, you will see the tab bar with two navigation controllers. Now tap the buttons placed in respected viewControllers, we will navigate to the ImageViewController and TableViewController. Now I will add a third view controller called ThirdViewController without the navigation controller. Open the appDelegate.m file and modify didFinishLaunchingWithOptions method as follows.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;">self.window <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>UIWindow alloc<span class="br0">&#93;</span> initWithFrame<span class="sy0">:</span><span class="br0">&#91;</span><span class="br0">&#91;</span>UIScreen mainScreen<span class="br0">&#93;</span> bounds<span class="br0">&#93;</span><span class="br0">&#93;</span>;<br />
UIViewController <span class="sy0">*</span>viewController1 <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>FirstViewController alloc<span class="br0">&#93;</span> initWithNibName<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;FirstViewController&quot;</span> bundle<span class="sy0">:</span><span class="kw2">nil</span><span class="br0">&#93;</span>;<br />
UIViewController <span class="sy0">*</span>viewController2 <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>SecondViewController alloc<span class="br0">&#93;</span> initWithNibName<span class="sy0">:</span> <span class="co3">@</span><span class="st0">&quot;SecondViewController&quot;</span> bundle<span class="sy0">:</span><span class="kw2">nil</span><span class="br0">&#93;</span>;<br />
ThirdViewController <span class="sy0">*</span>viewController3 <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>ThirdViewController alloc<span class="br0">&#93;</span>initWithNibName<span class="sy0">:</span> <span class="co3">@</span><span class="st0">&quot;ThirdViewController&quot;</span> bundle<span class="sy0">:</span><span class="kw2">nil</span><span class="br0">&#93;</span>; <br />
UINavigationController <span class="sy0">*</span>navController1 <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>UINavigationController alloc<span class="br0">&#93;</span> initWithRootViewController<span class="sy0">:</span> viewController1<span class="br0">&#93;</span>;<br />
UINavigationController <span class="sy0">*</span>navController2 <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>UINavigationController alloc<span class="br0">&#93;</span> initWithRootViewController<span class="sy0">:</span> viewController2<span class="br0">&#93;</span>;<br />
self.tabBarController <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>UITabBarController alloc<span class="br0">&#93;</span> init<span class="br0">&#93;</span>;<br />
self.tabBarController.viewControllers <span class="sy0">=</span> @<span class="br0">&#91;</span>navController1, navController2, viewController3<span class="br0">&#93;</span>;<br />
self.window.rootViewController <span class="sy0">=</span> self.tabBarController;<br />
<span class="br0">&#91;</span>self.window makeKeyAndVisible<span class="br0">&#93;</span>;<br />
<span class="kw1">return</span> <span class="kw2">YES</span>;</div>
</div>
<p>Open ThirdViewController.xib file and change it according to the following figure.<br />
<a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/Screen-Shot_4.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/Screen-Shot_4.png" alt="" title="Screen Shot_4" width="384" height="549" class="alignnone size-full wp-image-4872" /></a></p>
<p>Now open ThirdViewController.m file and in its initWithNibName method add the following line of code.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;">self.tabBarItem.image <span class="sy0">=</span> <span class="br0">&#91;</span>UIImage imageNamed<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;tab_image&quot;</span><span class="br0">&#93;</span>;</div>
</div>
<p>Compile and run the code, you will see three tabs, first two with navigationControllers and last one without navigationController. </p>
<p>If you want more number of tabs you can add their associated controllers. According to the apple documentation, the tab bar has limited space for displaying tab items. If you add six or more custom view controllers to a tab bar controller, the tab bar controller displays only the first four items plus the standard More item on the tab bar. Tapping the More item brings up a standard interface for selecting the remaining items. The interface for the standard More item includes an Edit button that allows the user to reconfigure the tab bar. By default, the user is allowed to rearrange all items on the tab bar. If you do not want the user to modify some items, though, you can remove the appropriate view controllers from the array in the customizableViewControllers property.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.edumobile.org/iphone/iphone-apps/tab-bar-controller-tutorial-for-iphone/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tutorial on Image Picker controller in iPhone</title>
		<link>http://www.edumobile.org/iphone/how-to-make-an-app-2/tutorial-on-image-picker-controller-in-iphone/</link>
		<comments>http://www.edumobile.org/iphone/how-to-make-an-app-2/tutorial-on-image-picker-controller-in-iphone/#comments</comments>
		<pubDate>Fri, 19 Apr 2013 10:47:17 +0000</pubDate>
		<dc:creator>Sushant</dc:creator>
				<category><![CDATA[How to make an app]]></category>
		<category><![CDATA[Image Picker controller example]]></category>
		<category><![CDATA[Image Picker controller example iPhone]]></category>
		<category><![CDATA[Image Picker controller iPhone]]></category>
		<category><![CDATA[Image Picker controller iPhone tutorials]]></category>
		<category><![CDATA[Image Picker controller tutotials]]></category>

		<guid isPermaLink="false">http://www.edumobile.org/iphone/?p=4857</guid>
		<description><![CDATA[In iOS, most of the applications need the feature of capturing the picture either from camera or from the existing photo library. Further, apps require to upload or save it in application bundle for future use. In this tutorial I will explain one of the most useful feature of iOS called UIImagePickerController and its delegate [...]]]></description>
			<content:encoded><![CDATA[<p>In iOS, most of the applications need the feature of capturing the picture either from camera or from the existing photo library. Further, apps require to upload or save it in application bundle for future use. In this tutorial I will explain one of the most useful feature of iOS called UIImagePickerController and its delegate protocol. I also explain how to save images into iPhone simulator&#8217;s photo library, editing the image while selecting and converting image into NSData for uploading into server or saving in application bundle.</p>
<p>Open Xcode, select “Create a new Xcode project,” and click Next. Choose the Single View Application. Click Next, then you navigate to the next screen. Fill all the fields like product name (ImagePicker), organization name and etc. In the bottom of the screen just uncheck “Use Storyboards”(As I have use the nib files in this application) and “Include Unit Tests”. </p>
<p>Before going further we need to add some images into iPhone simulator&#8217;s photo app. Open the iPhone simulator then open safari browser. Select any image from google images then tap the image and hold it for a while, you will see the action sheet appear as shown in the following figure.</p>
<p><a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/ScreenShot_11.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/ScreenShot_11.png" alt="" title="ScreenShot_1" width="346" height="544" class="alignnone size-full wp-image-4859" /></a></p>
<p>Tap on &#8220;Save Image&#8221; option then image will be automatically saved into photos app.</p>
<p>Now open ViewController.xib file from our project and add a button and connect it with the IBAction in the ViewController.h file. Open the ViewController.m file and add the following code.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="sy0">-</span> <span class="br0">&#40;</span>IBAction<span class="br0">&#41;</span>addImage<span class="sy0">:</span><span class="br0">&#40;</span><span class="kw4">id</span><span class="br0">&#41;</span>sender<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; UIActionSheet <span class="sy0">*</span>actionSheet <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>UIActionSheet alloc<span class="br0">&#93;</span> initWithTitle<span class="sy0">:</span> <span class="kw2">nil</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;delegate<span class="sy0">:</span> self<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cancelButtonTitle<span class="sy0">:</span> <span class="co3">@</span><span class="st0">&quot;Cancel&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;destructiveButtonTitle<span class="sy0">:</span> <span class="kw2">nil</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; otherButtonTitles<span class="sy0">:</span> <span class="co3">@</span><span class="st0">&quot;Take a new photo&quot;</span>, <span class="co3">@</span><span class="st0">&quot;Choose from existing&quot;</span>, <span class="kw2">nil</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#91;</span>actionSheet showInView<span class="sy0">:</span>self.view<span class="br0">&#93;</span>;<br />
<span class="br0">&#125;</span></div>
</div>
<p>This method will be invoked whenever you tap on &#8220;Add Image&#8221; button. In this I use UIActionSheet to populate available options. From apple documentation UIActionSheet class present the user with a set of alternatives for how to proceed with a given task. Now compile and run the code(there may be a warning but continue executing) you will see the screen as follows. </p>
<p><a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/ScreenShot_21.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/ScreenShot_21.png" alt="" title="ScreenShot_2" width="346" height="544" class="alignnone size-full wp-image-4860" /></a></p>
<p>Popup is showing three options. Now we have to write action methods for first two options. For that we have to register our ViewController with UIActionSheetDelegate protocol.  Add the UIActionSheetDelegate method and implement the action methods as follow.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>actionSheet<span class="sy0">:</span><span class="br0">&#40;</span>UIActionSheet <span class="sy0">*</span><span class="br0">&#41;</span>actionSheet clickedButtonAtIndex<span class="sy0">:</span><span class="br0">&#40;</span>NSInteger<span class="br0">&#41;</span>buttonIndex<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">switch</span> <span class="br0">&#40;</span>buttonIndex<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span> 0<span class="sy0">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>self takeNewPhotoFromCamera<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span> 1<span class="sy0">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>self choosePhotoFromExistingImages<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">default</span><span class="sy0">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
<p><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>takeNewPhotoFromCamera<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="br0">&#91;</span>UIImagePickerController isSourceTypeAvailable<span class="sy0">:</span> UIImagePickerControllerSourceTypeCamera<span class="br0">&#93;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; UIImagePickerController <span class="sy0">*</span>controller <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>UIImagePickerController alloc<span class="br0">&#93;</span> init<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; controller.sourceType <span class="sy0">=</span> UIImagePickerControllerSourceTypeCamera;<br />
&nbsp; &nbsp; &nbsp; &nbsp; controller.allowsEditing <span class="sy0">=</span> <span class="kw2">NO</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; controller.mediaTypes <span class="sy0">=</span> <span class="br0">&#91;</span>UIImagePickerController availableMediaTypesForSourceType<span class="sy0">:</span> UIImagePickerControllerSourceTypeCamera<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; controller.delegate <span class="sy0">=</span> self;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>self.navigationController presentViewController<span class="sy0">:</span> controller animated<span class="sy0">:</span> <span class="kw2">YES</span> completion<span class="sy0">:</span> <span class="kw2">nil</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
<p><span class="sy0">-</span><span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>choosePhotoFromExistingImages<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="br0">&#91;</span>UIImagePickerController isSourceTypeAvailable<span class="sy0">:</span> UIImagePickerControllerSourceTypePhotoLibrary<span class="br0">&#93;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; UIImagePickerController <span class="sy0">*</span>controller <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>UIImagePickerController alloc<span class="br0">&#93;</span> init<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; controller.sourceType <span class="sy0">=</span> UIImagePickerControllerSourceTypePhotoLibrary;<br />
&nbsp; &nbsp; &nbsp; &nbsp; controller.allowsEditing <span class="sy0">=</span> <span class="kw2">NO</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; controller.mediaTypes <span class="sy0">=</span> <span class="br0">&#91;</span>UIImagePickerController availableMediaTypesForSourceType<span class="sy0">:</span> UIImagePickerControllerSourceTypePhotoLibrary<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; controller.delegate <span class="sy0">=</span> self;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>self.navigationController presentViewController<span class="sy0">:</span> controller animated<span class="sy0">:</span> <span class="kw2">YES</span> completion<span class="sy0">:</span> <span class="kw2">nil</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>In the last two methods, first we check whether source type is available or not. If available then we present UIImagePickerController. By default allowsEditing property returns NO. If we set it to YES then we navigate to the next controller where we get the option to scale the selected image.<br />
Now open AppDelegate.h and add the following line of code</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="kw1">@property</span> <span class="br0">&#40;</span>strong, nonatomic<span class="br0">&#41;</span> UINavigationController <span class="sy0">*</span>navController;</div>
</div>
<p>In AppDelegate.m, change our ViewController to RootViewController by adding/modifying the following lines of code in &#8220;didFinishLaunchingWithOptions&#8221; method. </p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;">self.viewController <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>ViewController alloc<span class="br0">&#93;</span> initWithNibName<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;ViewController&quot;</span> bundle<span class="sy0">:</span><span class="kw2">nil</span><span class="br0">&#93;</span>;<br />
self.navController <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>UINavigationController alloc<span class="br0">&#93;</span>initWithRootViewController<span class="sy0">:</span>self.viewController<span class="br0">&#93;</span>;<br />
self.window.rootViewController <span class="sy0">=</span> self.navController;</div>
</div>
<p>Now register our ViewController with UINavigationControllerDelegate, UIImagePickerControllerDelegate protocols. Because to present UIImagePickerController we need the UINavigationController delegate protocol. Implement the UIImagePickerControllerDelegate methods in ViewController.m file as follows.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>imagePickerController<span class="sy0">:</span><span class="br0">&#40;</span>UIImagePickerController <span class="sy0">*</span><span class="br0">&#41;</span>picker didFinishPickingMediaWithInfo<span class="sy0">:</span><span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/"><span class="kw5">NSDictionary</span></a> <span class="sy0">*</span><span class="br0">&#41;</span>info<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="br0">&#91;</span>self.navigationController dismissViewControllerAnimated<span class="sy0">:</span> <span class="kw2">YES</span> completion<span class="sy0">:</span> <span class="kw2">nil</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; UIImage <span class="sy0">*</span>image <span class="sy0">=</span> <span class="br0">&#91;</span>info valueForKey<span class="sy0">:</span> UIImagePickerControllerOriginalImage<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/"><span class="kw5">NSData</span></a> <span class="sy0">*</span>imageData <span class="sy0">=</span> UIImageJPEGRepresentation<span class="br0">&#40;</span>image, 0.1<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>imagePickerControllerDidCancel<span class="sy0">:</span><span class="br0">&#40;</span>UIImagePickerController <span class="sy0">*</span><span class="br0">&#41;</span>picker;<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="br0">&#91;</span>self.navigationController dismissViewControllerAnimated<span class="sy0">:</span> <span class="kw2">YES</span> completion<span class="sy0">:</span> <span class="kw2">nil</span><span class="br0">&#93;</span>;<br />
<span class="br0">&#125;</span></div>
</div>
<p>Whenever we select the image, &#8220;didFinishPickingMediaWithInfo&#8221; delegate will be fired. There we can find the selected image reference using UIImagePickerControllerOriginalImage constant. If you want to upload the image to remote server or save it to the application bundle then we need to convert the image into NSData. we have already discuss the save image locally feature in one of our earlier tutorials (http://www.edumobile.org/iphone/miscellaneous/image-download-application-in-iphone/).</p>
<p>Suppose if you want to populate the selected image in a new controller then add the controller named ImageViewController as shown in the following figure.</p>
<p><a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/ScreenShot_31.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/ScreenShot_31.png" alt="" title="ScreenShot_3" width="313" height="456" class="alignnone size-full wp-image-4861" /></a></p>
<p>Open ImageViewController.h file, add UIImageView property and initialize its bounds in associated .m file. Then add the following lines of code in &#8220;didFinishPickingMediaWithInfo&#8221; delegate of ViewController.m file.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;">ImageViewController <span class="sy0">*</span>imageController <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>ImageViewController alloc<span class="br0">&#93;</span>initWithNibName<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;ImageViewController&quot;</span> bundle<span class="sy0">:</span><span class="kw2">nil</span><span class="br0">&#93;</span>;<br />
imageController.selectedImageView.image <span class="sy0">=</span> image;<br />
<span class="br0">&#91;</span>self.navigationController pushViewController<span class="sy0">:</span>imageController animated<span class="sy0">:</span><span class="kw2">YES</span><span class="br0">&#93;</span>;</div>
</div>
<p>Now compile and run the code. After selecting your options you will navigate to the photos app. If you select any image then you will navigate to the new screen where you see the selected image. If you press the &#8220;take a new photo&#8221; button in simulator, it won&#8217;t work. Because simulator is not capable of taking photos.</p>
<p><a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/ScreenShot_41.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/ScreenShot_41.png" alt="" title="ScreenShot_4" width="346" height="544" class="alignnone size-full wp-image-4862" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.edumobile.org/iphone/how-to-make-an-app-2/tutorial-on-image-picker-controller-in-iphone/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Image Download Application in iPhone</title>
		<link>http://www.edumobile.org/iphone/miscellaneous/image-download-application-in-iphone/</link>
		<comments>http://www.edumobile.org/iphone/miscellaneous/image-download-application-in-iphone/#comments</comments>
		<pubDate>Tue, 16 Apr 2013 11:22:46 +0000</pubDate>
		<dc:creator>Sushant</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[Image Download Application]]></category>
		<category><![CDATA[Image Download Application example tutorial]]></category>
		<category><![CDATA[Image Download Application iphone]]></category>
		<category><![CDATA[Image Download Application iPhone tutorial]]></category>
		<category><![CDATA[Image Download Application tutorial]]></category>

		<guid isPermaLink="false">http://www.edumobile.org/iphone/?p=4843</guid>
		<description><![CDATA[Most of the iPhone applications require the images to be downloaded from the web server. Depending upon the web service (API) provided, there are some methods to download the image. In this tutorial we will explain how to download the image from a remote server using NSURLConnection Delegates. Later we will explain how to save [...]]]></description>
			<content:encoded><![CDATA[<p>Most of the iPhone applications require the images to be downloaded from the web server. Depending upon the web service (API) provided, there are some methods to download the image. In this tutorial we will explain how to download the image from a remote server using NSURLConnection Delegates. Later we will explain how to save the image in application bundle (i.e Documents folder) for future use.</p>
<p>Open Xcode, select “Create a new Xcode project,” and click Next. Choose the Single View Application template as shown in the following figure.<br />
<a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/ScreenShot_1.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/ScreenShot_1-1024x720.png" alt="" title="ScreenShot_1" width="524" height="360" class="alignnone size-large wp-image-4844" /></a></p>
<p>Click Next, then you navigate to the next screen. Fill all the fields like product name (ImageDownload), organization name and etc. In the bottom of the screen just uncheck &#8220;Use Storyboards&#8221;(As I have use the nib files in this application) and &#8220;Include Unit Tests&#8221; as shown in the following figure.<br />
<a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/ScreenShot_2.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/ScreenShot_2-1024x720.png" alt="" title="ScreenShot_2" width="524" height="370" class="alignnone size-large wp-image-4845" /></a></p>
<p>Then click Next button.<br />
Open the ViewController.xib file and add the required outlets as shown below.<br />
<a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/ScreenShot_3.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/ScreenShot_3.png" alt="" title="ScreenShot_3" width="376" height="340" class="alignnone size-full wp-image-4846" /></a>            			<a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/ScreenShot_4.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/ScreenShot_4.png" alt="" title="ScreenShot_4" width="367" height="230" class="alignnone size-full wp-image-4847" /></a></p>
<p>Here UIProgressView outlet shows how much percentage of data has been downloaded. After downloading the full data, UIImageView will display the downloaded image.<br />
Finally, connect these outlets with the reference objects in ViewController.h file. Following figure shows the required connections.<br />
<a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/ScreenShot_5.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/ScreenShot_5.png" alt="" title="ScreenShot_5" width="409" height="212" class="alignnone size-full wp-image-4848" /></a><br />
Now open the ViewController.h file and add the following code.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="co1">#import &lt;UIKit/UIKit.h&gt;</span></p>
<p><span class="kw1">@interface</span> ViewController <span class="sy0">:</span> UIViewController&lt;NSURLConnectionDelegate,NSURLConnectionDataDelegate&gt;</p>
<p><span class="kw1">@property</span><span class="br0">&#40;</span>nonatomic, strong<span class="br0">&#41;</span> <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSMutableData_Class/"><span class="kw5">NSMutableData</span></a> <span class="sy0">*</span>imageData;<br />
<span class="kw1">@property</span><span class="br0">&#40;</span>nonatomic, assign<span class="br0">&#41;</span> <span class="kw4">float</span> length;</p>
<p><span class="kw1">@property</span><span class="br0">&#40;</span>nonatomic, strong<span class="br0">&#41;</span> IBOutlet UIProgressView <span class="sy0">*</span>progressView;<br />
<span class="kw1">@property</span><span class="br0">&#40;</span>nonatomic, strong<span class="br0">&#41;</span> IBOutlet UIImageView <span class="sy0">*</span>imageView;<br />
<span class="kw1">@property</span><span class="br0">&#40;</span>nonatomic, strong<span class="br0">&#41;</span> IBOutlet UILabel <span class="sy0">*</span>timeLabel;</p>
<p><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>saveLocally<span class="sy0">:</span><span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/"><span class="kw5">NSData</span></a> <span class="sy0">*</span><span class="br0">&#41;</span>imgData;<br />
<span class="kw1">@end</span></div>
</div>
<p>Here &#8220;imageData&#8221; will contains the data downloaded from server, &#8220;length&#8221; will contains the total image data length. And NSURLConnectionDelegate, NSURLConnectionDataDelegate are the delegate protocols for tracking the image data response, how much data is downloaded, and failure responses.<br />
Now open the ViewController.m file. In viewDidLoad method, add the following lines of code.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;">self.imageData <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSMutableData_Class/"><span class="kw5">NSMutableData</span></a> alloc<span class="br0">&#93;</span>init<span class="br0">&#93;</span>;<br />
<span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/"><span class="kw5">NSURLConnection</span></a> connectionWithRequest<span class="sy0">:</span><span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/"><span class="kw5">NSURLRequest</span></a> requestWithURL<span class="sy0">:</span><span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/"><span class="kw5">NSURL</span></a> URLWithString<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;http://www.raincoast.org/wp-content/uploads/S1I1596grizzlysittingattideline547-10MB.jpg&quot;</span><span class="br0">&#93;</span><span class="br0">&#93;</span>delegate<span class="sy0">:</span>self<span class="br0">&#93;</span>;</div>
</div>
<p>Here we will download the image from URL http://www.raincoast.org/wp-content/uploads/S1I1596grizzlysittingattideline547-10MB.jpg For the best progress of the progress view, we have taken the image of size 10MB. </p>
<p>Now for responding to authentication challenges we need to implement the delegate protocols. From the following code, in &#8220;didReceiveResponse&#8221; delegate we will find the expected length of the total image data for calculating the percentage of image downloaded. This delegate will call only once for each connection. The delegate &#8220;didReceiveData&#8221; will call several times until the entire image data is downloaded. In this delegate, we append the downloaded data to &#8220;imageData&#8221; and also added the code for calculating percentage of image downloaded. &#8220;connectionDidFinishLoading&#8221; delegate will call when entire image data is downloaded. Here we handle the image display part. Implementation of delegate methods is as follows:</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>connection<span class="sy0">:</span><span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/"><span class="kw5">NSURLConnection</span></a> <span class="sy0">*</span><span class="br0">&#41;</span>connection didReceiveResponse<span class="sy0">:</span><span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSURLResponse_Class/"><span class="kw5">NSURLResponse</span></a> <span class="sy0">*</span><span class="br0">&#41;</span>response<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; self.length <span class="sy0">=</span> <span class="br0">&#91;</span>response expectedContentLength<span class="br0">&#93;</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>connection<span class="sy0">:</span><span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/"><span class="kw5">NSURLConnection</span></a> <span class="sy0">*</span><span class="br0">&#41;</span>connection didReceiveData<span class="sy0">:</span><span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/"><span class="kw5">NSData</span></a> <span class="sy0">*</span><span class="br0">&#41;</span>data<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="br0">&#91;</span>self.imageData appendData<span class="sy0">:</span>data<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="kw4">float</span> progress <span class="sy0">=</span> &nbsp;<span class="br0">&#40;</span><span class="kw4">float</span><span class="br0">&#41;</span><span class="br0">&#91;</span>imageData length<span class="br0">&#93;</span><span class="sy0">/</span><span class="br0">&#40;</span><span class="kw4">float</span><span class="br0">&#41;</span>self.length;<br />
&nbsp; &nbsp; self.timeLabel.text <span class="sy0">=</span> <span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a> stringWithFormat<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;%0.2f%%&quot;</span>,progress<span class="sy0">*</span>100<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#91;</span>self.progressView setProgress<span class="sy0">:</span>progress animated<span class="sy0">:</span><span class="kw2">YES</span><span class="br0">&#93;</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>connection<span class="sy0">:</span><span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/"><span class="kw5">NSURLConnection</span></a> <span class="sy0">*</span><span class="br0">&#41;</span>connection didFailWithError<span class="sy0">:</span><span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSError_Class/"><span class="kw5">NSError</span></a> <span class="sy0">*</span><span class="br0">&#41;</span>error<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; NSLog<span class="br0">&#40;</span><span class="co3">@</span><span class="st0">&quot;didFailWithError&quot;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>connectionDidFinishLoading<span class="sy0">:</span><span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/"><span class="kw5">NSURLConnection</span></a> <span class="sy0">*</span><span class="br0">&#41;</span>connection<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; UIImage <span class="sy0">*</span>img <span class="sy0">=</span> <span class="br0">&#91;</span>UIImage imageWithData<span class="sy0">:</span>self.imageData<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; self.imageView.image <span class="sy0">=</span> img;<br />
&nbsp; &nbsp; <span class="br0">&#91;</span>self saveLocally<span class="sy0">:</span>self.imageData<span class="br0">&#93;</span>;<br />
<span class="br0">&#125;</span></div>
</div>
<p>Now compile and run the code. You will see the accurate download progress view and downloaded image as follows:<br />
<a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/ScreenShot_6.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/ScreenShot_6.png" alt="" title="ScreenShot_6" width="296" height="544" class="alignnone size-full wp-image-4849" /></a></p>
<p>Some applications need the local storage of data to avoid the same data to be downloaded several times. To achieve the local storage we need to save this image locally into applications document folder. &#8220;saveLocally:&#8221; method will do the local storage.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>saveLocally<span class="sy0">:</span><span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/"><span class="kw5">NSData</span></a> <span class="sy0">*</span><span class="br0">&#41;</span>imgData<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/"><span class="kw5">NSArray</span></a> <span class="sy0">*</span>paths <span class="sy0">=</span> NSSearchPathForDirectoriesInDomains<span class="br0">&#40;</span>NSDocumentDirectory, NSUserDomainMask, <span class="kw2">YES</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a> <span class="sy0">*</span>documentsDirectory <span class="sy0">=</span> <span class="br0">&#91;</span>paths objectAtIndex<span class="sy0">:</span>0<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/"><span class="kw5">NSDate</span></a> <span class="sy0">*</span>aDate <span class="sy0">=</span> <span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/"><span class="kw5">NSDate</span></a> date<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; NSTimeInterval interval <span class="sy0">=</span> <span class="br0">&#91;</span>aDate timeIntervalSince1970<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a> <span class="sy0">*</span>localFilePath <span class="sy0">=</span> <span class="br0">&#91;</span>documentsDirectory stringByAppendingPathComponent<span class="sy0">:</span><span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a> stringWithFormat<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;%d.jpeg&quot;</span>,<span class="br0">&#40;</span><span class="kw4">int</span><span class="br0">&#41;</span>interval<span class="br0">&#93;</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#91;</span>imgData writeToFile<span class="sy0">:</span>localFilePath atomically<span class="sy0">:</span><span class="kw2">YES</span><span class="br0">&#93;</span>;<br />
<span class="br0">&#125;</span></div>
</div>
<p>Here for each image download, we are giving a deferent name as per the epoch time. Now compile and run the code, you will see the image to be saved in applications document folder. Documents folder for your application is located in the following path /Users/startup/Library/Application Support/iPhone Simulator/6.1/Applications/3747D38D-9D04-4A58-9327-DFEE22740A81/Documents. Following screenshot shows the saved image under documents folder of the application.<br />
<a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/ScreenShot_7.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/04/ScreenShot_7-1024x293.png" alt="" title="ScreenShot_7" width="584" height="353" class="alignnone size-large wp-image-4850" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.edumobile.org/iphone/miscellaneous/image-download-application-in-iphone/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Time Application in iPhone</title>
		<link>http://www.edumobile.org/iphone/iphone-interface-builder/time-application-in-iphone/</link>
		<comments>http://www.edumobile.org/iphone/iphone-interface-builder/time-application-in-iphone/#comments</comments>
		<pubDate>Sat, 13 Apr 2013 04:26:07 +0000</pubDate>
		<dc:creator>Sushant</dc:creator>
				<category><![CDATA[iPhone Interface Builder]]></category>
		<category><![CDATA[Time Application example]]></category>
		<category><![CDATA[Time Application in iPhone]]></category>
		<category><![CDATA[Time Application iPhone]]></category>
		<category><![CDATA[Time Application tutorial iphone]]></category>

		<guid isPermaLink="false">http://www.edumobile.org/iphone/?p=3318</guid>
		<description><![CDATA[In this application we will see how to Current time display in the application. So let see how it will worked. Step 1: Open the Xcode, Create a new project using View Base application. Give the application “TimeApplication”. Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore [...]]]></description>
			<content:encoded><![CDATA[<p>In this application we will see how to Current time display in the application. So let see how it<br />
will worked.</p>
<p>Step 1: Open the Xcode, Create a new project using View Base application. Give the application<br />
“TimeApplication”.</p>
<p>Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.</p>
<p>Step 3: Expand classes and notice Interface Builder created the ViewController class for you. Expand Resources and notice the template generated a separate nib, TimeApplicationViewController.xib for the TimeApplication.</p>
<p>Step 4: Open the TimeApplicationViewController.h file and make the following changes:</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="co1">#import &lt;UIKit/UIKit.h&gt;</span><br />
<span class="kw1">@interface</span> TimeApplicationViewController <span class="sy0">:</span> UIViewController <span class="br0">&#123;</span><br />
IBOutlet UIButton <span class="sy0">*</span>submit;<br />
IBOutlet UILabel <span class="sy0">*</span><a href="http://www.opengroup.org/onlinepubs/009695399/functions/time.html"><span class="kw3">time</span></a>;<br />
<a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a> <span class="sy0">*</span>data;<br />
<span class="br0">&#125;</span><br />
<span class="kw1">@property</span> <span class="br0">&#40;</span>nonatomic, retain<span class="br0">&#41;</span> IBOutlet UIButton <span class="sy0">*</span>submit;<br />
<span class="kw1">@property</span> <span class="br0">&#40;</span>nonatomic, retain<span class="br0">&#41;</span> <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a> <span class="sy0">*</span>data;<br />
<span class="kw1">@property</span> <span class="br0">&#40;</span>nonatomic, retain<span class="br0">&#41;</span> IBOutlet UILabel <span class="sy0">*</span><a href="http://www.opengroup.org/onlinepubs/009695399/functions/time.html"><span class="kw3">time</span></a>;<br />
<span class="sy0">-</span><span class="br0">&#40;</span>IBAction<span class="br0">&#41;</span>ShowTime<span class="sy0">:</span><span class="br0">&#40;</span><span class="kw4">id</span><span class="br0">&#41;</span>sender;<br />
<span class="kw1">@end</span></div>
</div>
<p>Step 5: Double click the TimeApplicationViewController.xib file and open it to the Interface Builder. First drag the Round Rect Button and label from the library and place it to the view window. Now select the Round Rect button and bring up Connection Inspector and connect Touch Up inside to the File&#8217;s Owner icon and select ShowTime: method. Select File&#8217;s Owner icon to the label and select time. Now save the .sib file close it and go back to the Xcode.</p>
<p>Step 6: Open the TimeApplicationViewController.m file and make the following changes:</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="co1">#import &quot;TimeApplicationViewController.h&quot;</span><br />
<span class="kw1">@implementation</span> TimeApplicationViewController<br />
<span class="kw1">@synthesize</span> submit, <a href="http://www.opengroup.org/onlinepubs/009695399/functions/time.html"><span class="kw3">time</span></a>, data;<br />
<span class="sy0">-</span><span class="br0">&#40;</span>IBAction<span class="br0">&#41;</span>ShowTime<span class="sy0">:</span><span class="br0">&#40;</span><span class="kw4">id</span><span class="br0">&#41;</span>sender<br />
<span class="br0">&#123;</span><br />
<a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/"><span class="kw5">NSDate</span></a> <span class="sy0">*</span>date <span class="sy0">=</span> <span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/"><span class="kw5">NSDate</span></a> date<span class="br0">&#93;</span>;<br />
<a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/"><span class="kw5">NSDateFormatter</span></a> <span class="sy0">*</span>formatter <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span><span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/"><span class="kw5">NSDateFormatter</span></a> alloc<span class="br0">&#93;</span>init<span class="br0">&#93;</span>autorelease<span class="br0">&#93;</span>;<br />
<span class="br0">&#91;</span>formatter setDateFormat<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;HH:MM:SS&quot;</span><span class="br0">&#93;</span>;<br />
<a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a><span class="sy0">*</span> str <span class="sy0">=</span><span class="br0">&#91;</span>formatter stringFromDate<span class="sy0">:</span>date<span class="br0">&#93;</span>;<br />
NSLog<span class="br0">&#40;</span>str<span class="br0">&#41;</span>;<br />
<span class="br0">&#91;</span><a href="http://www.opengroup.org/onlinepubs/009695399/functions/time.html"><span class="kw3">time</span></a> setText<span class="sy0">:</span>str<span class="br0">&#93;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>dealloc<br />
<span class="br0">&#123;</span><br />
<span class="br0">&#91;</span>super dealloc<span class="br0">&#93;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>didReceiveMemoryWarning<br />
<span class="br0">&#123;</span><br />
<span class="br0">&#91;</span>super didReceiveMemoryWarning<span class="br0">&#93;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="co1">#pragma mark &#8211; View lifecycle</span><br />
<span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>viewDidUnload<br />
<span class="br0">&#123;</span><br />
<span class="br0">&#91;</span>super viewDidUnload<span class="br0">&#93;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">BOOL</span><span class="br0">&#41;</span>shouldAutorotateToInterfaceOrientation<span class="sy0">:</span><span class="br0">&#40;</span>UIInterfaceOrientation<span class="br0">&#41;</span>interfaceOrientation<br />
<span class="br0">&#123;</span><br />
<span class="kw1">return</span> <span class="br0">&#40;</span>interfaceOrientation <span class="sy0">==</span> UIInterfaceOrientationPortrait<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw1">@end</span></div>
</div>
<p>Step 7: Now Compile and run the application on the Simulator<br />
<a href="http://www.edumobile.org/iphone/wp-content/uploads/2011/09/18.jpg"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2011/09/18-166x300.jpg" alt="" title="1" width="166" height="300" class="alignnone size-medium wp-image-3319" /></a></p>
<p>You can Download SourceCode from <a href='http://www.edumobile.org/iphone/wp-content/uploads/2011/09/TimeApplication.zip'>here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.edumobile.org/iphone/iphone-interface-builder/time-application-in-iphone/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Alert View Application in iPhone</title>
		<link>http://www.edumobile.org/iphone/iphone-programming-tutorials/alert-view-application-in-iphone/</link>
		<comments>http://www.edumobile.org/iphone/iphone-programming-tutorials/alert-view-application-in-iphone/#comments</comments>
		<pubDate>Tue, 09 Apr 2013 04:37:04 +0000</pubDate>
		<dc:creator>Sushant</dc:creator>
				<category><![CDATA[iPhone Programming Tutorials]]></category>
		<category><![CDATA[Alert View Application]]></category>
		<category><![CDATA[Alert View Application iphone]]></category>
		<category><![CDATA[Alert View iPhone tutorials]]></category>
		<category><![CDATA[AlertView Application example]]></category>
		<category><![CDATA[iphone Alert View Application]]></category>

		<guid isPermaLink="false">http://www.edumobile.org/iphone/?p=3338</guid>
		<description><![CDATA[In this application we will see how to AlertView display after button pressed in iPhone. So let see how it will worked. Step 1: Open the Xcode, Create a new project using View Base application. Give the application “AlertView”. Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can [...]]]></description>
			<content:encoded><![CDATA[<p>In this application we will see how to AlertView display after button pressed in iPhone. So let see how it will<br />
worked. </p>
<p>Step 1: Open the Xcode, Create a new project using View Base application. Give the application<br />
“AlertView”. </p>
<p>Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.</p>
<p>Step 3: Expand classes and notice Interface Builder created the ViewController class for you. Expand Resources and notice the template generated a separate nib, AlertViewViewController.xib for the AlertView application.</p>
<p>Step 4: Open the AlertViewViewController.h file and make the following changes:</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="co1">#import &lt;UIKit/UIKit.h&gt;</span><br />
<span class="kw1">@interface</span> AlertViewViewController <span class="sy0">:</span> UIViewController <span class="br0">&#123;</span><br />
IBOutlet UITextField <span class="sy0">*</span>text;<br />
<span class="br0">&#125;</span><br />
<span class="sy0">-</span> <span class="br0">&#40;</span>IBAction<span class="br0">&#41;</span>AlertView<span class="sy0">:</span><span class="br0">&#40;</span><span class="kw4">id</span><span class="br0">&#41;</span>sender;<br />
<span class="kw1">@end</span></div>
</div>
<p>Step 5: Double click the AlertViewViewController.xib file and open it to the interface Builder. Drag the Textfield, Label and round rect button from the library and place it to the View window. Now connect File&#8217;s Owner icon to the TextField and select text, select the button and bring up Connection Inspector and connect Touch Up Inside to the File&#8217;s Owner icon and select AlertView: method. Now Save the .xib file, close it and go back to the Xcode.</p>
<p>Step 6: Open the AlertViewViewController.m file and make the following changes:</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="co1">#import &quot;AlertViewViewController.h&quot;</span><br />
<span class="kw1">@implementation</span> AlertViewViewController<br />
<span class="sy0">-</span> <span class="br0">&#40;</span>IBAction<span class="br0">&#41;</span>AlertView<span class="sy0">:</span><span class="br0">&#40;</span><span class="kw4">id</span><span class="br0">&#41;</span>sender<br />
<span class="br0">&#123;</span><br />
<span class="kw1">if</span><span class="br0">&#40;</span><span class="br0">&#91;</span>text.text length<span class="br0">&#93;</span><span class="sy0">==</span><span class="nu0">0</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
UIAlertView <span class="sy0">*</span> alert <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>UIAlertView alloc<span class="br0">&#93;</span> initWithTitle<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;&quot;</span><br />
message<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;Enter Your Name&quot;</span><br />
delegate<span class="sy0">:</span>self<br />
cancelButtonTitle<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;OK&quot;</span><br />
otherButtonTitles<span class="sy0">:</span> <span class="kw2">nil</span><span class="br0">&#93;</span>;<br />
<span class="br0">&#91;</span>alert show<span class="br0">&#93;</span>;<br />
<span class="br0">&#125;</span><span class="br0">&#91;</span><br />
text resignFirstResponder<span class="br0">&#93;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>dealloc<br />
<span class="br0">&#123;</span><br />
<span class="br0">&#91;</span>super dealloc<span class="br0">&#93;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>didReceiveMemoryWarning<br />
<span class="br0">&#123;</span><br />
<span class="co2">// Releases the view if it doesn&#8217;t have a superview.</span><br />
<span class="br0">&#91;</span>super didReceiveMemoryWarning<span class="br0">&#93;</span>;<br />
<span class="co2">// Release any cached data, images, etc that aren&#8217;t in use.</span><br />
<span class="br0">&#125;</span><br />
<span class="co1">#pragma mark &#8211; View lifecycle</span><br />
<span class="coMULTI">/*<br />
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.<br />
- (void)viewDidLoad<br />
{<br />
[super viewDidLoad];<br />
}<br />
*/</span><br />
<span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>viewDidUnload<br />
<span class="br0">&#123;</span><br />
<span class="br0">&#91;</span>super viewDidUnload<span class="br0">&#93;</span>;<br />
<span class="co2">// Release any retained subviews of the main view.</span><br />
<span class="co2">// e.g. self.myOutlet = nil;</span><br />
<span class="br0">&#125;</span><br />
<span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">BOOL</span><span class="br0">&#41;</span>shouldAutorotateToInterfaceOrientation<span class="sy0">:</span><span class="br0">&#40;</span>UIInterfaceOrientation<span class="br0">&#41;</span>interfaceOrientation<br />
<span class="br0">&#123;</span><br />
<span class="co2">// Return YES for supported orientations</span><br />
<span class="kw1">return</span> <span class="br0">&#40;</span>interfaceOrientation <span class="sy0">==</span> UIInterfaceOrientationPortrait<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw1">@end</span></div>
</div>
<p>Step 7: Now Compile and run the application on the Simulator.</p>
<p><a href="http://www.edumobile.org/iphone/wp-content/uploads/2011/09/110.jpg"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2011/09/110-150x150.jpg" alt="" title="1" width="150" height="150" class="alignnone size-thumbnail wp-image-3339" /></a></p>
<p>You can Download SourceCode from <a href='http://www.edumobile.org/iphone/wp-content/uploads/2011/09/AlertView.zip'>AlertView</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.edumobile.org/iphone/iphone-programming-tutorials/alert-view-application-in-iphone/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using the Empty Template in iPad</title>
		<link>http://www.edumobile.org/iphone/ipad-development/using-the-empty-template-in-ipad/</link>
		<comments>http://www.edumobile.org/iphone/ipad-development/using-the-empty-template-in-ipad/#comments</comments>
		<pubDate>Tue, 02 Apr 2013 07:39:58 +0000</pubDate>
		<dc:creator>Sushant</dc:creator>
				<category><![CDATA[iPad Development]]></category>
		<category><![CDATA[empty template]]></category>
		<category><![CDATA[iPad application]]></category>
		<category><![CDATA[ipad programming]]></category>
		<category><![CDATA[iPad tutorials]]></category>

		<guid isPermaLink="false">http://www.edumobile.org/iphone/?p=3489</guid>
		<description><![CDATA[Xcode offers many templates from which to choose when creating a new application. Sometimes, though, we want a bit more control over the details of an app than these templates can provide; for this reason, Xcode also offers an Empty application template. In this blog, we&#8217;ll see how to use the Empty template to build [...]]]></description>
			<content:encoded><![CDATA[<p>Xcode offers many templates from which to choose when creating a new application. Sometimes,<br />
though, we want a bit more control over the details of an app than these templates can provide; for this<br />
reason, Xcode also offers an Empty application template. In this blog, we&#8217;ll see how to use the Empty<br />
template to build a simple application. So let&#8217;s get started!</p>
<p>Start up Xcode and select “Create a new Xcode project.” In the next screen, choose Empty Application,<br />
then click next:</p>
<p><a href="http://www.edumobile.org/iphone/wp-content/uploads/2012/05/ChooseTemplate.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2012/05/ChooseTemplate.png" alt="" title="ChooseTemplate" width="131" height="97" class="aligncenter size-full wp-image-3490" /></a></p>
<p>In the following screen, name the project EmptyApp, select the options shown. Note that even though<br />
we are developing here for iPad, the principles involved are the same for iPhone development.</p>
<p><a href="http://www.edumobile.org/iphone/wp-content/uploads/2012/05/ProjectOptions1.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2012/05/ProjectOptions1-300x202.png" alt="" title="ProjectOptions" width="400" height="252" class="aligncenter size-medium wp-image-3495" /></a></p>
<p>Click Next, choose a location to save the project, and click Create.</p>
<p>The first thing to notice about the Empty application<a href="http://www.edumobile.org/iphone/wp-content/uploads/2012/05/NavigatorOne.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2012/05/NavigatorOne.png" alt="" title="NavigatorOne" width="262" height="157" class="alignright size-full wp-image-3493" /></a><br />
template is that it provides the AppDelegate class, but<br />
nothing else. We will need to provide our own view<br />
controller and view for the application.</p>
<p>In the menu, select File | New ► then File&#8230; In the next<br />
window, choose Objective – C Class, then click Next. In<br />
the window that results, name your new class<br />
MainViewController, make it an instance of<br />
UIViewController, select the checkbox next to “Targeted<br />
for iPad,” and make sure the “With XIB for user interface” check box is unchecked. (We&#8217;re going to<br />
create the xib file separately in this case to show how a view is connected to a view controller.) Click<br />
Next. Choose the default location to save the files, and click Create.</p>
<p>Now we&#8217;ll create the actual view. Another way to add a new file to the application is to right-click in<br />
the Project Navigator and select New File&#8230; from the popup menu. Do so now. In the resulting window,<br />
choose “User Interface” from the list to the left under iOS, then select the Empty document as shown<br />
here:</p>
<p><a href="http://www.edumobile.org/iphone/wp-content/uploads/2012/05/MakingXIBFile.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2012/05/MakingXIBFile-300x202.png" alt="" title="MakingXIBFile" width="400" height="252" class="aligncenter size-medium wp-image-3492" /></a></p>
<p>Click Next, and make sure the Device Family is set to “iPad.” Click Next once again, and in the Save<br />
As text box, enter “MainView.” Click Create to save the file in the default location.</p>
<p>The Project Navigator panel should now appear as shown<a href="http://www.edumobile.org/iphone/wp-content/uploads/2012/05/NavigatorTwo.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2012/05/NavigatorTwo.png" alt="" title="NavigatorTwo" width="260" height="207" class="alignright size-full wp-image-3494" /></a><br />
here. Note that you may have to drag some files around in the Navigator to properly organize them. Sometimes, Xcode will crash when dragging files – this is a known issue; simply reopen Xcode and continue when this happens.</p>
<p>Select the xib file to open it in Interface Builder. You will see an entirely empty Window. First, select the File&#8217;s Owner object. In the Identity Inspector, choose MainViewController for the File&#8217;s Owner&#8217;s class, as shown below. This will make the MainViewController class the view controller for the view that we will be adding to this xib file.</p>
<p><a href="http://www.edumobile.org/iphone/wp-content/uploads/2012/05/FilesOwnersClass.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2012/05/FilesOwnersClass.png" alt="" title="FilesOwnersClass" width="261" height="121" class="alignleft size-full wp-image-3491" /></a>Next, drag a UIView object from the Library to the<br />
Interface Builder surface. Notice that the UIView<br />
object is properly sized for the iPad device, because<br />
we made the iPad selection in the Device Family<br />
drop down when we created the xib file. Give the<br />
view a nice background color.</p>
<p>Now right-click the File&#8217;s Owner object, and drag<br />
from the circle to the right of the “View” item to the view itself. This makes the new UIView object the<br />
MainViewController&#8217;s view. At this point, we&#8217;re done with Interface Builder (for now).</p>
<p>We need to make this view a subview of the application&#8217;s window object, so it can be displayed when<br />
the app runs. Select the AppDelegate.h file, and make the changes shown in<strong> bold:</strong></p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="co1">#import &lt;UIKit/UIKit.h&gt;</span><br />
<span class="co1">#import &quot;MainViewController.h&quot;</span><br />
<span class="kw1">@interface</span> AppDelegate <span class="sy0">:</span> UIResponder &lt;UIApplicationDelegate&gt;<br />
<span class="kw1">@property</span> <span class="br0">&#40;</span>strong, nonatomic<span class="br0">&#41;</span> UIWindow <span class="sy0">*</span>window;<br />
<span class="kw1">@property</span> <span class="br0">&#40;</span>strong, nonatomic<span class="br0">&#41;</span> MainViewController <span class="sy0">*</span>mainViewController;<br />
<span class="kw1">@end</span></div>
</div>
<p>We&#8217;ve imported the MainViewController.h file (to obtain access to that object), and also set up a<br />
property of type MainViewController called mainViewController. We will instantiate this object in<br />
the .m file, then add it&#8217;s view as a subview of the window object.</p>
<p>Open up the AppDelegate.m file, and once again make the bolded changes. (The listing is on the next<br />
page:)</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="co1">#import &quot;AppDelegate.h&quot;</span><br />
<span class="kw1">@implementation</span> AppDelegate<br />
<span class="kw1">@synthesize</span> window <span class="sy0">=</span> _window;<br />
<span class="kw1">@synthesize</span> mainViewController <span class="sy0">=</span> _mainViewController;<br />
<span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">BOOL</span><span class="br0">&#41;</span>application<span class="sy0">:</span><span class="br0">&#40;</span>UIApplication <span class="sy0">*</span><span class="br0">&#41;</span>application didFinishLaunchingWithOptions<span class="sy0">:</span><span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/"><span class="kw5">NSDictionary</span></a><br />
<span class="sy0">*</span><span class="br0">&#41;</span>launchOptions<br />
<span class="br0">&#123;</span><br />
self.window <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>UIWindow alloc<span class="br0">&#93;</span> initWithFrame<span class="sy0">:</span><span class="br0">&#91;</span><span class="br0">&#91;</span>UIScreen mainScreen<span class="br0">&#93;</span> bounds<span class="br0">&#93;</span><span class="br0">&#93;</span>;<br />
<span class="co2">// Override point for customization after application launch.</span><br />
self.mainViewController <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>MainViewController alloc<span class="br0">&#93;</span> initWithNibName<span class="sy0">:</span><span class="kw2">nil</span><br />
bundle<span class="sy0">:</span><span class="kw2">nil</span><span class="br0">&#93;</span>;<br />
self.window.backgroundColor <span class="sy0">=</span> <span class="br0">&#91;</span>UIColor whiteColor<span class="br0">&#93;</span>;<br />
<span class="br0">&#91;</span>self.window addSubview<span class="sy0">:</span>self.mainViewController.view<span class="br0">&#93;</span>;<br />
<span class="br0">&#91;</span>self.window makeKeyAndVisible<span class="br0">&#93;</span>;<br />
<span class="kw1">return</span> <span class="kw2">YES</span>;<br />
<span class="br0">&#125;</span>.<br />
..</div>
</div>
<p>(We&#8217;re not showing the entire file, only the portion that requires changes to be made.)</p>
<p>First, we synthesize the property mainViewController. Following Apple&#8217;s recommendation, we<br />
synthesize this property with a name different from that given to it earlier, this makes it impossible to<br />
accidentally use it as an ivar: we must access it using its getter and setter methods.</p>
<p>Next we instantiate mainViewController by first allocating space on the heap for an object of class<br />
MainViewController, then calling initWithNibName: bundle: to initialize the object. In this case, we&#8217;ve<br />
already wired up the view controller, view, and File&#8217;s Owner in Interface Builder: a value of nil for the<br />
nib name will take the values already provided. A value of nil for the bundle parameter signals the<br />
system to take the current application bundle.</p>
<p>Finally, we add the mainViewController&#8217;s view as a subview of the main application window. When<br />
this window is made visible, the subview will be displayed on top of it.</p>
<p>Run the application to verify that the view is shown. At this point, the application is ready for<br />
development to proceed along traditional lines.</p>
<p><a href="http://www.edumobile.org/iphone/wp-content/uploads/2012/05/RunningApp.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2012/05/RunningApp.png" alt="" title="RunningApp" width="577" height="775" class="aligncenter size-full wp-image-3496" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.edumobile.org/iphone/ipad-development/using-the-empty-template-in-ipad/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Delegation between View Controllers using Storyboards (for iPhone)</title>
		<link>http://www.edumobile.org/iphone/iphone-programming-tutorials/delegation-between-view-controllers-using-storyboards-for-iphone/</link>
		<comments>http://www.edumobile.org/iphone/iphone-programming-tutorials/delegation-between-view-controllers-using-storyboards-for-iphone/#comments</comments>
		<pubDate>Sat, 30 Mar 2013 09:40:09 +0000</pubDate>
		<dc:creator>Sushant</dc:creator>
				<category><![CDATA[iPhone Programming Tutorials]]></category>
		<category><![CDATA[Delegation between UIViewControllers]]></category>
		<category><![CDATA[Delegation between UIViewControllers iphone]]></category>
		<category><![CDATA[Delegation between UIViewControllers tutorials]]></category>
		<category><![CDATA[modal delegation]]></category>
		<category><![CDATA[segue delegation]]></category>

		<guid isPermaLink="false">http://www.edumobile.org/iphone/?p=4758</guid>
		<description><![CDATA[Delegation between UIViewControllers is a common pattern. We might have a class graph in which there is a master view controller (that communicates with the model and all the other view controllers). Each main view in the application will have its own view controller. It is more efficient to have these view controllers delegate up [...]]]></description>
			<content:encoded><![CDATA[<p>Delegation between UIViewControllers is a common pattern. We might have a class graph in which there is a master view controller (that communicates with the model and all the other view controllers). Each main view in the application will have its own view controller. It is more efficient to have these view controllers delegate up to the master controller than to have each one communicate directly with the model.</p>
<p>When we use Nib files to represent views, the delegate class can name itself the delegate in the initWithNibName: bundle: method, or in viewDidLoad. Unfortunately, this practice does not work with storyboards. Since there is no Nib file, initWithNibName: bundle: is never called. Setting a class to be the delegate of another class in viewDidLoad relies on being able to “lazily instantiate” the other class in the delegate class, then setting its delegate property to self. This will not work because in a storyboard setting, the new class is actually instantiated during the segue itself (with modal segues). If we do instantiate the new class in viewDidLoad, it is not the same object that will be used.</p>
<p>So what do we do? In this blog, we&#8217;ll look at how to perform delegation with a modal view controller. Start Xcode and create a new project. Use the Single View Application template, name the project Delegation, and make sure to check the “Use Storyboards” checkbox as below:</p>
<p><a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/03/Image0011.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/03/Image0011.png" alt="" title="Image001" width="537" height="357" class="alignnone size-full wp-image-4759" /></a></p>
<p>Click Next, choose a location to save the project, and click Create. When the project has loaded, create a new UIViewController class named SecondViewController. Here is SecondViewController.h:</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="co1">#import &lt;UIKit/UIKit.h&gt;</span></p>
<p><span class="kw1">@protocol</span> SecondControllerDelegate &lt;NSObject&gt;</p>
<p><span class="sy0">-</span><span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span> userDidMakeChoice<span class="sy0">:</span><span class="br0">&#40;</span>NSUInteger<span class="br0">&#41;</span>choice;</p>
<p><span class="kw1">@end</span></div>
</div>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="kw1">@interface</span> SecondViewController <span class="sy0">:</span> UIViewController</p>
<p><span class="kw1">@property</span> <span class="br0">&#40;</span>nonatomic, assign<span class="br0">&#41;</span> <span class="kw4">id</span> &lt;SecondControllerDelegate&gt; delegate;</p>
<p><span class="sy0">-</span><span class="br0">&#40;</span>IBAction<span class="br0">&#41;</span>choiceMade<span class="sy0">:</span><span class="br0">&#40;</span>UISegmentedControl <span class="sy0">*</span><span class="br0">&#41;</span>sender;</p>
<p><span class="kw1">@end</span></div>
</div>
<p>We&#8217;ve set up a protocol named SecondControllerDelegate to hold a single delegate method, userDidMakeChoice:. When the user selects an option in a segmented control (in the view), the SecondViewController&#8217;s choiceMade: action method will fire, calling the delegate method before dismissing the view controller (which will reveal the main view controller once again).</p>
<p>Let&#8217;s look at the implementation of SecondViewController.m:</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="co1">#import &quot;SecondViewController.h&quot;</span></p>
<p><span class="kw1">@interface</span> SecondViewController <span class="br0">&#40;</span><span class="br0">&#41;</span></p>
<p><span class="kw1">@end</span></p>
<p><span class="kw1">@implementation</span> SecondViewController</p>
<p><span class="kw1">@synthesize</span> delegate;</p>
<p><span class="sy0">-</span> <span class="br0">&#40;</span>IBAction<span class="br0">&#41;</span>choiceMade<span class="sy0">:</span><span class="br0">&#40;</span>UISegmentedControl <span class="sy0">*</span><span class="br0">&#41;</span>sender<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="br0">&#91;</span>self.delegate userDidMakeChoice<span class="sy0">:</span>sender.selectedSegmentIndex<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#91;</span>self dismissViewControllerAnimated<span class="sy0">:</span><span class="kw2">YES</span> completion<span class="sy0">:</span><span class="kw2">nil</span><span class="br0">&#93;</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw1">@end</span></div>
</div>
<p>After synthesizing the delegate property, we implement choiceMade:. First, we call the delegate method, then dismiss this view controller by calling dismissViewControllerAnimated: completion: (with a nil value for completion, since we don&#8217;t need any code to run after the controller is dismissed).</p>
<p>Open MainStoryboard.storyboard in Interface Builder by clicking on it in the navigator. Drag a new View Controller to the main view, next to the first view controller. Place a single button labeled Choose&#8230; and a UILabel above the button with the text left blank on the first view controller, then CTRL – click and drag from this button to the second view controller to create a segue. Choose Modal when prompted for a segue type.</p>
<p>Now place a UISegmentedControl and a label on the second view controller. When finished, the Interface Builder should look like this (the label is on the first view controller, it is not selected and therefore invisible):</p>
<p><a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/03/Image0021.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/03/Image0021.png" alt="" title="Image002" width="995" height="658" class="alignnone size-full wp-image-4760" /></a></p>
<p>Select the second view controller, and change its class to SecondViewController as shown here:</p>
<p><a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/03/Image0031.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/03/Image0031.png" alt="" title="Image003" width="258" height="163" class="alignnone size-full wp-image-4761" /></a></p>
<p>Now select the segue itself, and give it an identifier a shown:</p>
<p><a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/03/Image0041.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/03/Image0041.png" alt="" title="Image004" width="259" height="144" class="alignnone size-full wp-image-4762" /></a></p>
<p>Now we&#8217;re ready to write the code for ViewController. Here is ViewController.h:</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="co1">#import &lt;UIKit/UIKit.h&gt;</span><br />
<span class="co1">#import &quot;SecondViewController.h&quot;</span></p>
<p><span class="kw1">@interface</span> ViewController <span class="sy0">:</span> UIViewController<br />
&lt;SecondControllerDelegate&gt;</p>
<p><span class="kw1">@property</span> <span class="br0">&#40;</span>nonatomic, weak<span class="br0">&#41;</span> IBOutlet UILabel <span class="sy0">*</span>outcome;</p>
<p><span class="kw1">@end</span></div>
</div>
<p>We&#8217;ve imported SecondViewController for two reasons, to adopt its protocol, and to get its type, which will become important in a moment. The IBOutlet UILabel property “outcome” will be wired to the “textless” label on this view controller&#8217;s view.</p>
<p>Now, ViewController.m:</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="co1">#import &quot;ViewController.h&quot;</span></p>
<p><span class="kw1">@interface</span> ViewController <span class="br0">&#40;</span><span class="br0">&#41;</span></p>
<p><span class="kw1">@end</span></p>
<p><span class="kw1">@implementation</span> ViewController</p>
<p><span class="kw1">@synthesize</span> outcome;</p>
<p><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>prepareForSegue<span class="sy0">:</span><span class="br0">&#40;</span>UIStoryboardSegue <span class="sy0">*</span><span class="br0">&#41;</span>segue sender<span class="sy0">:</span><span class="br0">&#40;</span><span class="kw4">id</span><span class="br0">&#41;</span>sender<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="br0">&#91;</span>segue.identifier isEqualToString<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;toSecondVC&quot;</span><span class="br0">&#93;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SecondViewController <span class="sy0">*</span>secondVC <span class="sy0">=</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span>SecondViewController <span class="sy0">*</span><span class="br0">&#41;</span> segue.destinationViewController;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>secondVC setDelegate<span class="sy0">:</span>self<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
<p><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>userDidMakeChoice<span class="sy0">:</span><span class="br0">&#40;</span>NSUInteger<span class="br0">&#41;</span>choice<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">switch</span> <span class="br0">&#40;</span>choice<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span> <span class="nu0">0</span><span class="sy0">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.outcome.text <span class="sy0">=</span> <span class="co3">@</span><span class="st0">&quot;One chosen&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span> <span class="nu0">1</span><span class="sy0">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.outcome.text <span class="sy0">=</span> <span class="co3">@</span><span class="st0">&quot;Two chosen&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span> <span class="nu0">2</span><span class="sy0">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.outcome.text <span class="sy0">=</span> <span class="co3">@</span><span class="st0">&quot;Three chosen&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">default</span><span class="sy0">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">break</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
<p><span class="kw1">@end</span></div>
</div>
<p>The important thing here is the prepareForSegue: sender: method, which will be called right before a segue fires. If there is only one segue in an app, there is no need to check the identifier (or even to set an identifier on the segue at all). We do so here to illustrate the more common case, in which there would be multiple segues from a single view controller, and a need to distinguish them.</p>
<p>The key point is the line:</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;">SecondViewController <span class="sy0">*</span>secondVC <span class="sy0">=</span> <span class="br0">&#40;</span>SecondViewController <span class="sy0">*</span><span class="br0">&#41;</span> segue.destinationViewController;</div>
</div>
<p>segue.destinationViewController is the view controller that will be shown by the segue. We need to get that view controller so we can set its delegate property to self. But segue.destinationViewController is typed as a UIViewController, not a SecondViewController. It must be cast to the proper type so that we can get to its properties!</p>
<p>Now we can assign the delegate:</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="br0">&#91;</span>secondVC setDelegate<span class="sy0">:</span>self<span class="br0">&#93;</span>;</div>
</div>
<p>This allows us to respond to the protocol&#8217;s method, which simply converts the integer parameter to an English word.</p>
<p>To finish, we must wire up the ViewController&#8217;s label outlet to the label (the one with no text), and the SecondViewController&#8217;s choiceMade: method to the UISegmentedControl&#8217;s ValueChanged event. Note the button on the first view controller does not need an IBAction method: the segue process itself knows what to do with this button press. It calls prepareForSegue: sender: performing the code we specify, then performs the segue by displaying the SecondViewController.</p>
<p><a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/03/Image0051.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/03/Image0051.png" alt="" title="Image005" width="328" height="465" class="alignnone size-full wp-image-4763" /></a></p>
<p><a href="http://www.edumobile.org/iphone/wp-content/uploads/2013/03/Image0061.png"><img src="http://www.edumobile.org/iphone/wp-content/uploads/2013/03/Image0061.png" alt="" title="Image006" width="326" height="467" class="alignnone size-full wp-image-4764" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.edumobile.org/iphone/iphone-programming-tutorials/delegation-between-view-controllers-using-storyboards-for-iphone/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
