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

<channel>
	<title>iBlogging NRG &#187; Development</title>
	<atom:link href="http://nrg.com.bd/blog/archives/category/development/feed" rel="self" type="application/rss+xml" />
	<link>http://nrg.com.bd/blog</link>
	<description>CoCoA Cooking Kitchen!</description>
	<lastBuildDate>Wed, 29 Apr 2009 09:09:25 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Calendar View Control for iPhone</title>
		<link>http://nrg.com.bd/blog/archives/36</link>
		<comments>http://nrg.com.bd/blog/archives/36#comments</comments>
		<pubDate>Tue, 21 Apr 2009 07:48:04 +0000</pubDate>
		<dc:creator>nrg</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Cocoa Touch]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Calendar]]></category>
		<category><![CDATA[Calendar Controller]]></category>
		<category><![CDATA[UIKit]]></category>

		<guid isPermaLink="false">http://nrg.com.bd/blog/?p=36</guid>
		<description><![CDATA[Some days ago when I was developing an application for iPhone, I required a calendar view to show date specific data on it. Unfortunately I did not find any built-in control in Cocoa Touch UIKit. I was googling to find out any third-party solution but luck did not favored me on that as well. So [...]]]></description>
			<content:encoded><![CDATA[<div>Some days ago when I was developing an application for iPhone, I required a calendar view to show date specific data on it. Unfortunately I did not find any built-in control in Cocoa Touch UIKit. I was googling to find out any third-party solution but luck did not favored me on that as well. So I decided to create my own calendar control. This article shows you how to use it in your applications.
<div>The calendar control that I developed is actually nothing but a view component and if you want&#160; to show any date specific data on it (like event information) then you need separate delegate. The calendar is inherited from UIView and so you can add it as sub-view to other top level view that you want. Follow the steps below to add this and manipulate over this.</div>
<div>1. Create a new view based project in XCode and name it CalTest.     <br />2. In the Classes folder add my <strong>NRGUICalendarView.h</strong> and <strong>NRGUICalendarView.m</strong> files.</div>
<div>&#160;</div>
<div><a href="http://nrg.com.bd/nrg/NRGUICalendarView/paypal.php" target="_blank"><img style="display: inline; margin-left: 0px; margin-right: 0px" border="0" alt="Preview Image" src="https://www.paypal.com/en_GB/i/btn/btn_buynowCC_LG.gif" /></a>      </div>
<div>3. You need to add two labels for current month and year and four buttons for navigating around months and years. To contain the calendar view you need to add another sub-view. So you need two outlets for two labels and one outlet for the view and four actions. Add the following code to the <strong>CalTestViewController.h</strong> file.</div>
<blockquote><blockquote>
<blockquote>
<p><span>#import </span><span>&lt;UIKit/UIKit.h&gt;</span></p>
<p><span>@class</span> <span>NRGUICalendarView</span><span>;</span></p>
<p><span>@interface</span><span> CalTestViewController : UIViewController {</span>            <br /><span>NRGUICalendarView</span><span> *cal;</span>            <br /><span>IBOutlet</span> <span>UILabel</span><span> *monthLabel;</span>            <br /><span>IBOutlet</span> <span>UILabel</span><span> *yearLabel;</span>            <br /><span>IBOutlet</span> <span>UIView</span><span> *calCont;</span>            <br /><span>}</span></p>
<p><span>@property</span><span> (</span><span>nonatomic</span><span>, </span><span>retain</span><span>) </span><span>IBOutlet</span> <span>UILabel</span><span> *monthLabel;</span>            <br /><span>@property</span><span> (</span><span>nonatomic</span><span>, </span><span>retain</span><span>) </span><span>IBOutlet</span> <span>UILabel</span><span> *yearLabel;</span>            <br /><span>@property</span><span> (</span><span>nonatomic</span><span>, </span><span>retain</span><span>) </span><span>IBOutlet</span> <span>UIView</span><span> *calCont;</span></p>
<p><span>- (</span><span>IBAction</span><span>)prevMonth:(</span><span>id</span><span>)sender;</span>            <br /><span>- (</span><span>IBAction</span><span>)nextMonth:(</span><span>id</span><span>)sender;</span>            <br /><span>- (</span><span>IBAction</span><span>)prevYear:(</span><span>id</span><span>)sender;</span>            <br /><span>- (</span><span>IBAction</span><span>)nextYear:(</span><span>id</span><span>)sender;</span></p>
<p><span>@end</span></p>
</blockquote>
</blockquote>
</blockquote>
<div>
<p>4. Now add synthesis and dealloc code to the <strong>CalTestViewController.h</strong> file.</p>
</p></div>
<blockquote><blockquote>
<blockquote>
<p><span>#import </span><span>&quot;CalTestViewController.h&quot;</span>            <br /><span>#import</span> <span>&quot;NRGUICalendarView.h&quot;</span></p>
<p><span>@implementation</span><span> CalTestViewController</span></p>
<p><span>@synthesize</span><span> yearLabel, monthLabel, calCont;</span>            <br /><span>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</span>            <br /><span>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</span></p>
<p><span>- (</span><span>void</span><span>)dealloc {</span>            <br /><span>[</span><span>yearLabel</span> <span>release</span><span>];</span>            <br /><span>[</span><span>monthLabel</span> <span>release</span><span>];</span>            <br /><span>[</span><span>calCont</span> <span>release</span><span>];</span>            <br /><span>[</span><span>super</span> <span>dealloc</span><span>];</span>            <br /><span>}</span></p>
</blockquote>
</blockquote>
</blockquote>
<div>
<p>5. Now open <strong>CalTestViewController.xib</strong> in interface builder and add four buttons, two labels and a view to the root view as shown below and the connect all outlets and assign all actions accordingly.</p>
</p></div>
<div>
<a href="http://nrg.com.bd/blog/wp-content/loads/2009/04/12.jpg"><img class="size-full wp-image-38" title="Connecting Outlets and Actions" alt="Connecting Outlets and Actions" src="http://nrg.com.bd/blog/wp-content/loads/2009/04/12.jpg" width="320" height="502" /></a>
<p>6. Now add the four navigation actions for months and years in the <strong>CalTestViewController.m </strong>file.</p>
</p></div>
<blockquote><blockquote>
<blockquote>
<p><span>IBAction</span><span>)prevMonth:(</span><span>id</span><span>)sender {</span>            <br /><span>[cal </span><span>previousMonth</span><span>];</span>            <br /><span>monthLabel.</span><span>text</span><span> = [cal </span><span>currentMonthName</span><span>];</span>            <br /><span>}</span></p>
<p><span>- (</span><span>IBAction</span><span>)nextMonth:(</span><span>id</span><span>)sender {</span>            <br /><span>[cal </span><span>nextMonth</span><span>];</span>            <br /><span>monthLabel.</span><span>text</span><span> = [cal </span><span>currentMonthName</span><span>];</span>            <br /><span>}</span></p>
<p><span>- (</span><span>IBAction</span><span>)prevYear:(</span><span>id</span><span>)sender {</span>            <br /><span>[cal </span><span>previousYear</span><span>];</span>            <br /><span>yearLabel.</span><span>text</span><span> = [[</span><span>NSString</span> <span>alloc</span><span>] </span><span>initWithFormat</span><span>:</span><span>@&quot;%i&quot;</span><span>, [cal </span><span>currentYear</span><span>]];</span>            <br /><span>}</span></p>
<p><span>- (</span><span>IBAction</span><span>)nextYear:(</span><span>id</span><span>)sender {</span>            <br /><span>[cal </span><span>nextYear</span><span>];</span>            <br /><span>yearLabel.</span><span>text</span><span> = [[</span><span>NSString</span> <span>alloc</span><span>] </span><span>initWithFormat</span><span>:</span><span>@&quot;%i&quot;</span><span>, [cal </span><span>currentYear</span><span>]];</span>            <br /><span>}</span></p>
</blockquote>
</blockquote>
</blockquote>
<div>7. Add the following initialization code inside the <strong>viewDidLoad</strong> method of <strong>CalTestViewController.m</strong> file.</div>
<blockquote><blockquote>
<blockquote>
<p><span>cal</span><span> = [[</span><span>NRGUICalendarView</span> <span>alloc</span><span>] </span><span>initWithHandler</span><span>:</span><span>self</span><span>];</span>            <br /><span>[</span><span>calCont</span> <span>addSubview</span><span>:</span><span>cal</span><span>];</span>            <br /><span>monthLabel</span><span>.</span><span>text</span><span> = [</span><span>cal</span> <span>currentMonthName</span><span>];</span>            <br /><span>yearLabel</span><span>.</span><span>text</span><span> = [[</span><span>NSString</span> <span>alloc</span><span>] </span><span>initWithFormat</span><span>:</span><span>@&quot;%i&quot;</span><span>, [</span><span>cal</span> <span>currentYear</span><span>]];</span></p>
</blockquote>
</blockquote>
</blockquote>
<div>
<p>8. Now we need to tell the calendar control that we are going to use the <strong>CalTestViewController</strong> as its data source and delegate. That&#8217;s why we passed <strong>self</strong> to the <strong>initWithHandler</strong> message. Now add the following two methods to <strong>CalTestViewController</strong>.</p>
</p></div>
<blockquote><blockquote>
<blockquote>
<p><span>- (void)daySelected:(id)sender {</span>            <br /><span>}</span></p>
<p><span>- (BOOL)isMarked:(NSInteger)year month:(NSInteger)month day:(NSInteger)day {</span></p>
<blockquote><p><span>return NO;</span></p>
</blockquote>
<p><span>}</span></p>
</blockquote>
</blockquote>
</blockquote>
<div></div>
<div>Here when the user clicks on any cell in the calendar, the <strong>daySelected</strong> event is triggered, each cell is implemented by <strong>UIButton</strong> and the tag of each button is assigned by the month day of the corresponding cell. So if we are interested in the day clicked, we just need to use <strong>sender.tag</strong> property.</div>
<div>If you want to show marked (colored differently) day (for events etc) you need to return YES from the <strong>isMarked:month:day</strong> method. The calendar asks the data source delegate if a day is marked while it is being rendered.
<p>9. Now build and run the simulator and you should see something like the following.</p>
</p></div>
<div>
<div id="g8.x">
<a href="http://nrg.com.bd/blog/wp-content/loads/2009/04/21.jpg"><img class="size-full wp-image-39" title="Demo" alt="Demo" src="http://nrg.com.bd/blog/wp-content/loads/2009/04/21.jpg" width="386" height="742" /></a>
</p></div>
<p>Other methods in NRGUICalendarView class.</p>
</p></div>
<div>
<p><span><strong>- (</strong></span><span><strong>void</strong></span><span><strong>)renderYear:(</strong></span><span><strong>NSInteger</strong></span><span><strong>)year month:(</strong></span><span><strong>NSInteger</strong></span><span><strong>)month;</strong></span></p>
</p></div>
<blockquote><p>renders the calendar view to show the specified month.</p>
</blockquote>
<div>
<p><span><strong>- (</strong></span><span><strong>void</strong></span><span><strong>)gotoYear:(</strong></span><span><strong>NSInteger</strong></span><span><strong>)year month:(</strong></span><span><strong>NSInteger</strong></span><span><strong>)month day:(</strong></span><span><strong>NSInteger</strong></span><span><strong>)day;</strong></span></p>
</p></div>
<blockquote><p>selects the specified cell by day, month, year.</p>
</blockquote>
<div>
<p><span><strong>- (</strong></span><span><strong>void</strong></span><span><strong>)goToday;</strong></span></p>
</p></div>
<blockquote><p>goes and selects the current day.</p>
</blockquote>
<div>
<p><span><strong>- (</strong></span><span><strong>void</strong></span><span><strong>)previousMonth;</strong></span></p>
</p></div>
<blockquote><p>navigates to previous month.</p>
</blockquote>
<div>
<p><span><strong>- (</strong></span><span><strong>void</strong></span><span><strong>)nextMonth;</strong></span></p>
</p></div>
<blockquote><p>navigates to next month.</p>
</blockquote>
<div>
<p><span><strong>- (</strong></span><span><strong>void</strong></span><span><strong>)previousYear;</strong></span></p>
</p></div>
<blockquote><p>navigates to previous year.</p>
</blockquote>
<div>
<p><span><strong>- (</strong></span><span><strong>void</strong></span><span><strong>)nextYear;</strong></span></p>
</p></div>
<blockquote><p>navigates to next year.</p>
</blockquote>
<div><span><strong>- (</strong></span><span><strong>NSString</strong></span><span><strong>*)currentMonthName;</strong></span></div>
<blockquote><p>returns current month name as string.</p>
</blockquote>
<div>
<p><span><strong>- (</strong></span><span><strong>NSInteger</strong></span><span><strong>)currentMonth;</strong></span></p>
</p></div>
<blockquote><p>returns current month as integer (January = 1, February = 2, and so on).</p>
</blockquote>
<div>
<p><span><strong>- (</strong></span><span><strong>NSInteger</strong></span><span><strong>)currentYear;</strong></span></p>
</p></div>
<blockquote><p>returns current year as integer.</p>
</blockquote></div>
<div>You can buy this class from <a href="http://nrg.com.bd/nrg/NRGUICalendarView/paypal.php">here</a>.</div>
<div id="st200811092854" class="st-taf"><script src="http://cdn.socialtwist.com/200811092854/script.js"></script><img style="border:0;margin:0;padding:0;" src="http://images.socialtwist.com/200811092854/button.png" onmouseout="hideHoverMap(this)" onmouseover="showHoverMap(this, '200811092854',  'http%3A%2F%2Fnrg.com.bd%2Fblog%2Farchives%2F36', 'Calendar+View+Control+for+iPhone')" onclick="cw(this, {id:'200811092854',link: 'http%3A%2F%2Fnrg.com.bd%2Fblog%2Farchives%2F36', title: '+Calendar+View+Control+for+iPhone+' })"/></div>]]></content:encoded>
			<wfw:commentRss>http://nrg.com.bd/blog/archives/36/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
