<?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>Matteo Caldari &#187; cocoa</title>
	<atom:link href="http://matteocaldari.it/tag/cocoa/feed" rel="self" type="application/rss+xml" />
	<link>http://matteocaldari.it</link>
	<description>freelance developer</description>
	<lastBuildDate>Sat, 17 Sep 2011 14:01:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>A UISegmentedControl with custom color</title>
		<link>http://matteocaldari.it/2010/05/a-uisegmentedcontrol-with-custom-color</link>
		<comments>http://matteocaldari.it/2010/05/a-uisegmentedcontrol-with-custom-color#comments</comments>
		<pubDate>Mon, 24 May 2010 09:24:58 +0000</pubDate>
		<dc:creator>matteo</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[iphone sdk]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[colors]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[subclassing]]></category>
		<category><![CDATA[uikit]]></category>

		<guid isPermaLink="false">http://matteocaldari.it/?p=364</guid>
		<description><![CDATA[Update: code for the class is now on GitHub. Also updated with BSD license and a fix for the next iOS version coming soon. If you have issues please use GitHub. The color of a UISegmentedControl in the UIKit is customizable only in the bar style mode, so if one uses the plain or bordered [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update: </strong> code for the class is now on <a href="https://github.com/oettam/MCSegmentedControl">GitHub</a>. Also updated with BSD license and a fix for the next iOS version coming soon. If you have issues please use GitHub.</p>
<p>The color of a UISegmentedControl in the UIKit is customizable only in the bar style mode, so if one uses the plain or bordered mode, the only possible color is blue. This could be a problem if blue does not fit well in your app or in a view.</p>
<p>The only way to support other colors is to subclass the UISegmentedColor and draw the view yourself. That&#8217;s what I did in the MCSegmentedControl class, here&#8217;s how it looks with a green tint color.</p>
<p><img class="aligncenter size-full wp-image-367" title="Screen shot 2010-05-24 at 09.53.17" src="http://matteocaldari.it/wp-content/uploads/Screen-shot-2010-05-24-at-09.53.17.png" alt="" width="315" height="68" /><br />
<span id="more-364"></span><br />
Here  compared with the standard UISegmentedControl, bordered style (mine is above):</p>
<p><img class="aligncenter size-full wp-image-368" title="Screen shot 2010-05-24 at 09.51.49" src="http://matteocaldari.it/wp-content/uploads/Screen-shot-2010-05-24-at-09.51.49.png" alt="" width="301" height="137" /></p>
<p>and plain style:</p>
<p><img class="aligncenter size-full wp-image-369" title="Screen shot 2010-05-24 at 09.52.28" src="http://matteocaldari.it/wp-content/uploads/Screen-shot-2010-05-24-at-09.52.28.png" alt="" width="313" height="141" /></p>
<p>and now with some ugly colors and font:</p>
<p><img class="aligncenter size-full wp-image-370" title="Screen shot 2010-05-24 at 09.53.49" src="http://matteocaldari.it/wp-content/uploads/Screen-shot-2010-05-24-at-09.53.49.png" alt="" width="308" height="66" /></p>
<p>My implementation is not pixel identical to the Apple one, but it is very similar.</p>
<p>I added support for customizing the tint (i.e. the background of the selected segment), the color of the not selected items and of the selected item, plus, if the segment has an image, it is drawn as a mask, so it will always be in the same color as the text.</p>
<h3>Example</h3>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #11740a; font-style: italic;">// Instantiate as usual</span><br />
<a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/"><span style="color: #400080;">NSArray</span></a> <span style="color: #002200;">*</span>items <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/"><span style="color: #400080;">NSArray</span></a> arrayWithObjects<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;first&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;second&quot;</span>, <span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;image.png&quot;</span><span style="color: #002200;">&#93;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;<br />
MCSegmentedControl <span style="color: #002200;">*</span>segmentedControl <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MCSegmentedControl alloc<span style="color: #002200;">&#93;</span> initWithItems<span style="color: #002200;">:</span>items<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <br />
<span style="color: #11740a; font-style: italic;">// set frame, add to view, set target and action for value change as usual</span><br />
segmentedControl.frame <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span>10.0f, 10.0f, 300.0f, 44.0f<span style="color: #002200;">&#41;</span>;<br />
<span style="color: #002200;">&#91;</span>self.view addSubview<span style="color: #002200;">:</span>segmentedControl<span style="color: #002200;">&#93;</span>;<br />
<span style="color: #002200;">&#91;</span>segmentedControl addTarget<span style="color: #002200;">:</span>self action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>segmentedControlDidChange<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> forControlEvents<span style="color: #002200;">:</span>UIControlEventValueChanged<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <br />
<span style="color: #11740a; font-style: italic;">// Set a tint color</span><br />
segmentedControl.tintColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor orangeColor<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <br />
<span style="color: #11740a; font-style: italic;">// Customize font and items color</span><br />
segmentedControl.selectedItemColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor yellowColor<span style="color: #002200;">&#93;</span>;<br />
segmentedControl.unselectedItemColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor darkGrayColor<span style="color: #002200;">&#93;</span>;</div></div>
<p>If you use Interface Builder, add a normal UISegmentedControl, set its class as MCSegmentedControl in the Identity Inspector, set the Tint in the Attributes Inspector.</p>
<p>At the moment, animations and the following UISegmentedControl methods are not supported:</p>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setWidth<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGFloat<span style="color: #002200;">&#41;</span>width forSegmentAtIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSUInteger<span style="color: #002200;">&#41;</span>segment;<br />
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setContentOffset<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGSize<span style="color: #002200;">&#41;</span>offset forSegmentAtIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSUInteger<span style="color: #002200;">&#41;</span>segment</div></div>
<p>Here are the files: <a href="http://matteocaldari.it/download/MCSegmentedControl.zip">MCSegmentedControl.zip</a><br />
<div id="tweetbutton364" class="tw_button" style=""><a href="http://twitter.com/share?url=http%3A%2F%2Fbit.ly%2FcAV7Z0&amp;via=matteo&amp;text=A%20UISegmentedControl%20with%20custom%20color&amp;related=matteo&amp;lang=en&amp;count=none&amp;counturl=http%3A%2F%2Fmatteocaldari.it%2F2010%2F05%2Fa-uisegmentedcontrol-with-custom-color" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('https://matteocaldari.it/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div></p>
]]></content:encoded>
			<wfw:commentRss>http://matteocaldari.it/2010/05/a-uisegmentedcontrol-with-custom-color/feed</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
		<item>
		<title>Multiple contexts, controllers, delegates and CoreData bug</title>
		<link>http://matteocaldari.it/2009/11/multiple-contexts-controllers-delegates-and-coredata-bug</link>
		<comments>http://matteocaldari.it/2009/11/multiple-contexts-controllers-delegates-and-coredata-bug#comments</comments>
		<pubDate>Mon, 09 Nov 2009 11:13:13 +0000</pubDate>
		<dc:creator>matteo</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[coredata]]></category>
		<category><![CDATA[iphone sdk]]></category>
		<category><![CDATA[workaround]]></category>

		<guid isPermaLink="false">http://matteocaldari.it/?p=203</guid>
		<description><![CDATA[In a iPhone app I&#8217;m currently developing, I have a tipical Core Data object model with two entities (called Collection and Item) in a one-to-many relationship, and two table views for the entities, the second appearing when selecting a Collection in the first. The two table views have their own controller (which implements NSFetchedResultsControllerDelegate), managed [...]]]></description>
			<content:encoded><![CDATA[<p>In a iPhone app I&#8217;m currently developing, I have a tipical Core Data object model with two entities (called <em>Collection</em> and <em>Item</em>) in a one-to-many relationship, and two table views for the entities, the second appearing when selecting a <em>Collection</em> in the first. The two table views have their own controller (which implements <em>NSFetchedResultsControllerDelegate</em>), managed object context and <em>NSFetchedResultsController</em>. The second table has has a reference to the <em>Collection</em> object, used to build a predicate in this way:</p>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSPredicate_Class/"><span style="color: #400080;">NSPredicate</span></a> predicateWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;collection == %@&quot;</span>, <span style="color: #002200;">&#91;</span>self.collection objectID<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span></div></div>
<p><span id="more-203"></span><br />
In the first table view (the <em>collections</em>) everything works.<br />
In the second (the <em>items</em> of a <em>collection</em>) when a new object is added, none of the <strong>NSFetchedResultsControllerDelegate</strong> methods gets called.<br />
Moreover, when the name of an item is modified, the method <strong>controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:</strong> is called, but with type <strong>NSFetchedResultsChangeDelete</strong> instead of <strong>NSFetchedResultsChangeUpdate</strong>.</p>
<p>The problem is that, because of a bug in NSFetchedResultsController (see <a href="https://devforums.apple.com/message/139580#139580">this post</a> on the Apple Developer Forum) , the NSPredicate above does a pointer comparison between two objects that are in two different contexts, so a possibile workaround until it get fixed would be something like this:</p>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSPredicate_Class/"><span style="color: #400080;">NSPredicate</span></a> predicateWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;collection == %@&quot;</span>, <span style="color: #002200;">&#91;</span>self.managedObjectContext objectWithID<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self.collection objectID<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;</div></div>
]]></content:encoded>
			<wfw:commentRss>http://matteocaldari.it/2009/11/multiple-contexts-controllers-delegates-and-coredata-bug/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

