tag:blogger.com,1999:blog-6866191193233335492024-03-05T12:56:41.356-08:00iPhone Development ExperiencesUnknownnoreply@blogger.comBlogger88125tag:blogger.com,1999:blog-686619119323333549.post-65363526047113385282014-07-20T16:17:00.001-07:002014-07-20T16:17:09.378-07:00Refactoring the table providerAs you recall, the last version of the table provider had a hardcoded cell provider:<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> tableView(tableView: <span style="color: #703daa;">UITableView</span>!, cellForRowAtIndexPath indexPath: <span style="color: #703daa;">NSIndexPath</span>!) -> <span style="color: #703daa;">UITableViewCell</span>!</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> cell = tableView.<span style="color: #3d1d81;">dequeueReusableCellWithIdentifier</span>(<span style="color: #d12f1b;">"dieRoll"</span>, forIndexPath: indexPath) <span style="color: #bb2ca2;">as</span> <span style="color: #4f8187;">DieResultCell</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> data=<span style="color: #4f8187;">myArray</span>[indexPath.<span style="color: #703daa;">row</span>] ;</div>
<div style="font-family: Menlo; font-size: 11px;">
cell.<span style="color: #4f8187;">dieLabel</span>.<span style="color: #703daa;">text</span>=<span style="color: #d12f1b;">"</span>\(data)<span style="color: #d12f1b;">"</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">return</span> cell</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
This is fine for a simple table, but I would like to allow for different types of table cells within the view and provide a general routine.<br />
<br />
The refactoring is to allow the datatype to implement a protocol that defines the nib identifier. In addition a closure will provide a callback that initializes the table cell with the data it is to display.<br />
<br />
The protocol is rather simple:<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">@objc</span> <span style="color: #bb2ca2;">protocol</span> ProvidesCellIdentifier</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> cellIdentifier:<span style="color: #703daa;">String</span> { <span style="color: #bb2ca2;">get</span>}</div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
<br /></div>
I encapsulate my roll result into a cass, and an associated data type. This will allow for more flexibility, and uses the Swift extension mechanism.<br />
<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">@objc</span> <span style="color: #bb2ca2;">class</span> RollResult</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> rollResult:<span style="color: #703daa;">String</span>=<span style="color: #d12f1b;">""</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">init</span>(value:<span style="color: #703daa;">String</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #4f8187;">rollResult</span>=value;</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
}</div>
then add an extension to my RollResult type that supplies that cell identifier:<br />
<br />
<div style="color: #4f8187; font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">extension</span><span style="color: black;"> </span>RollResult<span style="color: black;">: </span>ProvidesCellIdentifier</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> cellIdentifier: <span style="color: #703daa;">String</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">return</span> <span style="color: #4f8187;">DieResultCell</span>.cellIdentifier;</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
}</div>
The next step is to modify my table so that it tracks information about the various cells that it can display:<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">class</span> CellConfigurationInfo</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> cellIdentifier:<span style="color: #703daa;">String</span>=<span style="color: #d12f1b;">""</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> dataClass:<span style="color: #703daa;">AnyClass</span>=<span style="color: #703daa;">AnyObject</span>.<span style="color: #bb2ca2;">self</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> configuration:((<span style="color: #703daa;">UITableViewCell</span>,<span style="color: #703daa;">AnyObject</span>)->())={(cell:<span style="color: #703daa;">UITableViewCell</span>,value:<span style="color: #703daa;">AnyObject</span>) <span style="color: #bb2ca2;">in</span> };</div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="font-family: Times; font-size: small;">This is used so that when I display a cell with the given cell identifier, I will call the configuration routine with that data. Note that the default callback doesn't actually do anything:</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="font-family: Times; font-size: small;"><br /></span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="font-family: Times; font-size: small;">Now to initialize my table, the following call is used:</span></div>
<div style="color: #4f8187; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>tableControl<span style="color: black;">=</span>TableViewDataProvider<span style="color: black;">(</span><span style="color: #bb2ca2;">self</span><span style="color: black;">.</span>resultTable<span style="color: black;">)</span></div>
<div style="font-family: Menlo; font-size: 11px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
.<span style="color: #31595d;">addMapping</span>(<span style="color: #4f8187;">DieResultCell</span>.cellIdentifier, dataClass: <span style="color: #4f8187;">Die</span>.<span style="color: #bb2ca2;">self</span>, configurationRoutine: <span style="color: #4f8187;">DieResultCell</span>.dieCellClosure )</div>
<div style="font-family: Menlo; font-size: 11px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
Where DieResult Cell is:</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">class</span> DieResultCell : <span style="color: #703daa;">UITableViewCell</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">@IBOutlet</span> <span style="color: #bb2ca2;">var</span> dieLabel : <span style="color: #703daa;">UILabel</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">class</span> <span style="color: #bb2ca2;">var</span> cellIdentifier:<span style="color: #703daa;">String</span> { <span style="color: #bb2ca2;">return</span> <span style="color: #d12f1b;">"dieRoll"</span>};</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">class</span> <span style="color: #bb2ca2;">var</span> dieCellClosure:(<span style="color: #703daa;">UITableViewCell</span>,<span style="color: #703daa;">AnyObject</span>)->() {</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">return</span> {(cell:<span style="color: #703daa;">UITableViewCell</span>, value:<span style="color: #703daa;">AnyObject</span>)->() <span style="color: #bb2ca2;">in</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> die=value <span style="color: #bb2ca2;">as</span> <span style="color: #4f8187;">RollResult</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> dieCell=cell <span style="color: #bb2ca2;">as</span> <span style="color: #4f8187;">DieResultCell</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
dieCell.<span style="color: #4f8187;">dieLabel</span>.<span style="color: #703daa;">text</span>=<span style="color: #d12f1b;">"</span>\(die.<span style="color: #4f8187;">rollResult</span>)<span style="color: #d12f1b;">"</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
}}</div>
<div style="font-family: Menlo; font-size: 11px;">
</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div>
<br /></div>
The end result is a flexible table data source that can display different data types: The full source for the TableViewProvider is below:<br />
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
//</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
// TableViewDataProvider.swift</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
// SimpleDieRoller</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
//</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
// Created by Jon Lundy on 6/28/14.</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
// Copyright (c) 2014 Jon Lundy. All rights reserved.</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
//</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">import</span> Foundation</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">import</span> UIKit</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">class</span> CellConfigurationInfo</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> cellIdentifier:<span style="color: #703daa;">String</span>=<span style="color: #d12f1b;">""</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> dataClass:<span style="color: #703daa;">AnyClass</span>=<span style="color: #703daa;">AnyObject</span>.<span style="color: #bb2ca2;">self</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> configuration:((<span style="color: #703daa;">UITableViewCell</span>,<span style="color: #703daa;">AnyObject</span>)->())={(cell:<span style="color: #703daa;">UITableViewCell</span>,value:<span style="color: #703daa;">AnyObject</span>) <span style="color: #bb2ca2;">in</span> };</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="color: #703daa; font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">class</span><span style="color: black;"> TableViewDataProvider : </span>NSObject<span style="color: black;">, </span>UITableViewDataSource<span style="color: black;">,</span>UITableViewDelegate</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> configurations = <span style="color: #703daa;">Dictionary</span><<span style="color: #703daa;">String</span>,<span style="color: #4f8187;">CellConfigurationInfo</span>>();</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> table:<span style="color: #703daa;">UITableView</span>;</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">init</span>(<span style="color: #bb2ca2;">_</span> mytable:<span style="color: #703daa;">UITableView</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">self</span>.<span style="color: #4f8187;">table</span>=mytable;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #4f8187;">myArray</span>=<span style="color: #703daa;">Array</span><<span style="color: #703daa;">AnyObject</span>>();</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">super</span>.<span style="color: #bb2ca2;">init</span>();</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">self</span>.<span style="color: #4f8187;">table</span>.<span style="color: #703daa;">dataSource</span>=<span style="color: #bb2ca2;">self</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">self</span>.<span style="color: #4f8187;">table</span>.<span style="color: #703daa;">delegate</span>=<span style="color: #bb2ca2;">self</span>;</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> myArray:<span style="color: #703daa;">Array</span><<span style="color: #703daa;">AnyObject</span>>;</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> cellSelected:((<span style="color: #703daa;">AnyObject</span>)->())={(value:<span style="color: #703daa;">AnyObject</span>) <span style="color: #bb2ca2;">in</span> <span style="color: #3d1d81;">println</span>(<span style="color: #d12f1b;">"Cell Selected </span>\(value)<span style="color: #d12f1b;">"</span>)};</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> addMapping(cellIdentifier:<span style="color: #703daa;">String</span>, dataClass:<span style="color: #703daa;">AnyClass</span>,</div>
<div style="font-family: Menlo; font-size: 11px;">
configurationRoutine:(<span style="color: #703daa;">UITableViewCell</span>,<span style="color: #703daa;">AnyObject</span>)->()) -><span style="color: #4f8187;">TableViewDataProvider</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="color: #4f8187; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span><span style="color: #bb2ca2;">let</span><span style="color: black;"> info=</span>CellConfigurationInfo<span style="color: black;">();</span></div>
<div style="font-family: Menlo; font-size: 11px;">
info.<span style="color: #4f8187;">cellIdentifier</span>=cellIdentifier;</div>
<div style="font-family: Menlo; font-size: 11px;">
info.<span style="color: #4f8187;">dataClass</span>=dataClass;</div>
<div style="font-family: Menlo; font-size: 11px;">
info.<span style="color: #4f8187;">configuration</span>=configurationRoutine;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #4f8187;">configurations</span>.<span style="color: #3d1d81;">updateValue</span>(info, forKey: cellIdentifier)</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">return</span> <span style="color: #bb2ca2;">self</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> onSelection(selectCall:(<span style="color: #703daa;">AnyObject</span>)->())-><span style="color: #4f8187;">TableViewDataProvider</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #4f8187;">cellSelected</span>=selectCall;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">return</span> <span style="color: #bb2ca2;">self</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> addArray(arrayToWatch:<span style="color: #703daa;">Array</span><<span style="color: #703daa;">AnyObject</span>>)</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #4f8187;">myArray</span>=arrayToWatch;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">self</span>.<span style="color: #4f8187;">table</span>.<span style="color: #3d1d81;">reloadData</span>();</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> clear()</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">self</span>.<span style="color: #4f8187;">myArray</span>.<span style="color: #3d1d81;">removeAll</span>(keepCapacity: <span style="color: #703daa;">true</span>);</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">self</span>.<span style="color: #4f8187;">table</span>.<span style="color: #3d1d81;">reloadData</span>();</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> addItem(itemToAdd:<span style="color: #703daa;">AnyObject</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">self</span>.<span style="color: #4f8187;">myArray</span>.<span style="color: #3d1d81;">insert</span>(itemToAdd, atIndex: <span style="color: #272ad8;">0</span>);</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">self</span>.<span style="color: #4f8187;">table</span>.<span style="color: #3d1d81;">reloadData</span>();</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> updateData()</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">self</span>.<span style="color: #4f8187;">table</span>.<span style="color: #3d1d81;">reloadData</span>();</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> tableView(tableView: <span style="color: #703daa;">UITableView</span>!, numberOfRowsInSection section: <span style="color: #703daa;">Int</span>) -> <span style="color: #703daa;">Int</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">return</span> <span style="color: #4f8187;">myArray</span>.<span style="color: #703daa;">count</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> tableView(tableView: <span style="color: #703daa;">UITableView</span>!, didSelectRowAtIndexPath indexPath: <span style="color: #703daa;">NSIndexPath</span>!)</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> selectedObject=<span style="color: #31595d;">getObjectAtPath</span>(indexPath);</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">self</span>.<span style="color: #4f8187;">cellSelected</span>(selectedObject);</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> getObjectAtPath(indexPath:<span style="color: #703daa;">NSIndexPath</span>)-><span style="color: #703daa;">AnyObject</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">return</span> <span style="color: #3d1d81;">self</span>.<span style="color: #4f8187;">myArray</span>[indexPath.<span style="color: #703daa;">row</span>];</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> tableView(tableView: <span style="color: #703daa;">UITableView</span>!, cellForRowAtIndexPath indexPath: <span style="color: #703daa;">NSIndexPath</span>!) -> <span style="color: #703daa;">UITableViewCell</span>!</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> data=<span style="color: #4f8187;">myArray</span>[indexPath.<span style="color: #703daa;">row</span>] ;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> myclass=data.classForCoder</div>
<div style="color: #d12f1b; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span><span style="color: #3d1d81;">println</span><span style="color: black;">(</span>"Getting object for class <span style="color: black;">\(myclass)</span>"<span style="color: black;">)</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> dataProtocol = data <span style="color: #bb2ca2;">as</span> <span style="color: #4f8187;">ProvidesCellIdentifier</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> config=<span style="color: #4f8187;">configurations</span>[dataProtocol.<span style="color: #4f8187;">cellIdentifier</span>]!</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> cell = tableView.<span style="color: #3d1d81;">dequeueReusableCellWithIdentifier</span>(config.<span style="color: #4f8187;">cellIdentifier</span>, forIndexPath: indexPath) <span style="color: #bb2ca2;">as</span> <span style="color: #703daa;">UITableViewCell</span>;</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
config.<span style="color: #4f8187;">configuration</span>(cell,data);</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">return</span> cell;</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="font-family: Times; font-size: small;"><br /></span></div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-686619119323333549.post-16715786871876767512014-07-12T08:15:00.001-07:002014-07-12T08:15:23.207-07:00A simple table providerFor my next task I wanted to do a first cut at a simple table view. I personally have found that the having the TableViewDataSource and TableViewDelegate be my view controller to mean duplicated code. My first version is just a simple array of 'any object' that can be managed by a table view.<br />
<br />
In this case I'm trying a slightly different UI than most. I have a picker view and a table view on the same window. <br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgCVGgvz3Ah2xi3KXwHo9DU4JpFDcYjhRO8i_bEqSCnk6MsermION2-ijzEX2BYvkf-mHZmN3_ex7rTWcJ7TbK17mBgqQ_NPdewk-aIXBFytYd1XLu9OV4A2k4kYAwrmfu3VtxaEhaugw/s1600/Screen+Shot+2014-07-12+at+10.06.53+AM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgCVGgvz3Ah2xi3KXwHo9DU4JpFDcYjhRO8i_bEqSCnk6MsermION2-ijzEX2BYvkf-mHZmN3_ex7rTWcJ7TbK17mBgqQ_NPdewk-aIXBFytYd1XLu9OV4A2k4kYAwrmfu3VtxaEhaugw/s1600/Screen+Shot+2014-07-12+at+10.06.53+AM.png" height="320" width="295" /></a></div>
<br />
In this case the top is the picker view, and the bottom is the table view. Right now I just made a very primitive custom view that had a single 'label'. In Xcode I gave it an identifier of 'dieRoll'.<br />
<br />
The code to hook up my view to the table is:<br />
<br />
<div class="p1">
<span class="s1"><br /></span></div>
<div class="p1">
<span class="s1"> </span>tableControl<span class="s1">=</span>TableViewDataProvider<span class="s1">(</span><span class="s2">self</span><span class="s1">.</span>resultTable<span class="s1">)</span></div>
<div class="p1">
<span class="s1"><br /></span></div>
<div class="p1">
<span class="s1">To add a new row to the table I do:</span></div>
<div class="p1">
<span class="s1"><br /></span></div>
<div class="p1">
<br /></div>
<div class="p2">
<span class="s1">let</span> result=<span class="s2">currentRoller</span>!.<span class="s3">roll</span>();</div>
<div class="p1">
<span class="s1">
</span></div>
<div class="p2">
<span class="s1">self</span>.<span class="s2">tableControl</span>!.<span class="s3">addItem</span>(result)</div>
<div class="p2">
<br /></div>
<div class="p2">
In this case result is an instance of my 'Die' class:</div>
<div class="p2">
<br /></div>
<div class="p2">
Finally the class for the table controller is:<br />
<br />
Note that I have code in here where I can get selection events, but I currently just use a segue in interface builder for going to a subview.</div>
<div class="p2">
<br /></div>
<div class="p2">
<br /></div>
<div class="p1">
<div style="color: #703daa; font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">class</span><span style="color: black;"> TableViewDataProvider : </span>NSObject<span style="color: black;">, </span>UITableViewDataSource<span style="color: black;">,</span>UITableViewDelegate</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> table:<span style="color: #703daa;">UITableView</span>;</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">init</span>(<span style="color: #bb2ca2;">_</span> mytable:<span style="color: #703daa;">UITableView</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">self</span>.<span style="color: #4f8187;">table</span>=mytable;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #4f8187;">myArray</span>=<span style="color: #703daa;">Array</span><<span style="color: #703daa;">AnyObject</span>>();</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">super</span>.<span style="color: #bb2ca2;">init</span>();</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">self</span>.<span style="color: #4f8187;">table</span>.<span style="color: #703daa;">dataSource</span>=<span style="color: #bb2ca2;">self</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">self</span>.<span style="color: #4f8187;">table</span>.<span style="color: #703daa;">delegate</span>=<span style="color: #bb2ca2;">self</span>;</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> myArray:<span style="color: #703daa;">Array</span><<span style="color: #703daa;">AnyObject</span>>;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> cellSelected:((<span style="color: #703daa;">AnyObject</span>)->())={(value:<span style="color: #703daa;">AnyObject</span>) <span style="color: #bb2ca2;">in</span> <span style="color: #3d1d81;">println</span>(<span style="color: #d12f1b;">"Cell Selected </span>\(value)<span style="color: #d12f1b;">"</span>)};</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> onSelection(selectCall:(<span style="color: #703daa;">AnyObject</span>)->())-><span style="color: #4f8187;">TableViewDataProvider</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #4f8187;">cellSelected</span>=selectCall;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">return</span> <span style="color: #bb2ca2;">self</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> addArray(arrayToWatch:<span style="color: #703daa;">Array</span><<span style="color: #703daa;">AnyObject</span>>)</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #4f8187;">myArray</span>=arrayToWatch;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">self</span>.<span style="color: #4f8187;">table</span>.<span style="color: #3d1d81;">reloadData</span>();</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> clear()</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">self</span>.<span style="color: #4f8187;">myArray</span>.<span style="color: #3d1d81;">removeAll</span>(keepCapacity: <span style="color: #703daa;">true</span>);</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">self</span>.<span style="color: #4f8187;">table</span>.<span style="color: #3d1d81;">reloadData</span>();</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> addItem(itemToAdd:<span style="color: #703daa;">AnyObject</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">self</span>.<span style="color: #4f8187;">myArray</span>.<span style="color: #3d1d81;">insert</span>(itemToAdd, atIndex: <span style="color: #272ad8;">0</span>);</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">self</span>.<span style="color: #4f8187;">table</span>.<span style="color: #3d1d81;">reloadData</span>();</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> updateData()</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">self</span>.<span style="color: #4f8187;">table</span>.<span style="color: #3d1d81;">reloadData</span>();</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> tableView(tableView: <span style="color: #703daa;">UITableView</span>!, numberOfRowsInSection section: <span style="color: #703daa;">Int</span>) -> <span style="color: #703daa;">Int</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">return</span> <span style="color: #4f8187;">myArray</span>.<span style="color: #703daa;">count</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> tableView(tableView: <span style="color: #703daa;">UITableView</span>!, didSelectRowAtIndexPath indexPath: <span style="color: #703daa;">NSIndexPath</span>!)</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> selectedObject=<span style="color: #31595d;">getObjectAtPath</span>(indexPath);</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">self</span>.<span style="color: #4f8187;">cellSelected</span>(selectedObject);</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> getObjectAtPath(indexPath:<span style="color: #703daa;">NSIndexPath</span>)-><span style="color: #703daa;">AnyObject</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">return</span> <span style="color: #3d1d81;">self</span>.<span style="color: #4f8187;">myArray</span>[indexPath.<span style="color: #703daa;">row</span>];</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> tableView(tableView: <span style="color: #703daa;">UITableView</span>!, cellForRowAtIndexPath indexPath: <span style="color: #703daa;">NSIndexPath</span>!) -> <span style="color: #703daa;">UITableViewCell</span>!</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> cell = tableView.<span style="color: #3d1d81;">dequeueReusableCellWithIdentifier</span>(<span style="color: #d12f1b;">"dieRoll"</span>, forIndexPath: indexPath) <span style="color: #bb2ca2;">as</span> <span style="color: #4f8187;">DieResultCell</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> data=<span style="color: #4f8187;">myArray</span>[indexPath.<span style="color: #703daa;">row</span>] ;</div>
<div style="font-family: Menlo; font-size: 11px;">
cell.<span style="color: #4f8187;">dieLabel</span>.<span style="color: #703daa;">text</span>=<span style="color: #d12f1b;">"</span>\(data)<span style="color: #d12f1b;">"</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">return</span> cell</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<br />
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
</div>
<div class="p1">
<span class="s1">This is rather primitive, but a starting point. I've abstracted from my view the ability to display an array. However this currently has a hardcoded 'cell manager', which gets the proper cell, and initializes it. In addition it only works for arrays, I would also like it to work for a NSFetchedResultsController.</span><br />
<span class="s1"><br /></span>
<span class="s1"><br /></span>
<span class="s1">Refactoring goals:</span><br />
<br />
<ul>
<li>Pass in the configuration for determining which cell to use, and how to initialize it.</li>
<li>Abstract the management of data to a subclass to allow other sources (such as CoreData, web, etc).</li>
</ul>
<div>
<br /></div>
</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-686619119323333549.post-83689099436236031872014-06-28T10:30:00.001-07:002014-06-28T10:30:51.339-07:00Version 2 of swift UI picker view<h2>
This version's Goals</h2>
The previous iteration of the picker view was great, but only supported pure text. The next version supports using an object as well as supplying more utility routines to provide data.<br />
<br />
For this version I want to clean up the API and make it more general and add the following capabilities:<br />
<br />
<br />
<ul>
<li>Be able to use an object to supply the data, and make that object available when it is picked</li>
<li>Have an easier interface to provide data to the picker view</li>
<li>Be able to have varying widths when implementing displaying the text.</li>
</ul>
<br />
These goals will implement the vast majority of pickers required, the main item missing will be the ability to provide custom views for the picker images,<br />
<br />
The previous version supported an object, but that object had to implement a protocol and it was not a very general solution. This version will keep the implementation of the object pure, and let the picker view work with pretty much any object.<br />
<h2>
Calling Implementation</h2>
<h3>
Die data type</h3>
<div>
My datatype is a very simple die type. It contains the ability to define the die in the constructor (initializer) of the data type, as well as a method that generates a random number:</div>
<div>
<div style="color: #bb2ca2; font-family: Menlo; font-size: 11px;">
@objc<span style="color: black;"> </span>class<span style="color: black;"> Die</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> dieType:<span style="color: #703daa;">Int</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">init</span>(<span style="color: #bb2ca2;">_</span> die:<span style="color: #703daa;">Int</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #4f8187;">dieType</span>=die;</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> roll () -> <span style="color: #703daa;">Int</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> result=<span style="color: #703daa;">Int</span>(<span style="color: #3d1d81;">arc4random_uniform</span>(<span style="color: #703daa;">UInt32</span>(<span style="color: #4f8187;">dieType</span>)));</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">return</span> result + <span style="color: #272ad8;">1</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
}</div>
</div>
<div>
Note that my previous solution had the Die implementing a protocol and the picker provider.</div>
<h3>
Creating the dice</h3>
<div>
When creating the dice array, we can use the map function to create the array just like before:</div>
<div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> dieTypes=[<span style="color: #272ad8;">4</span>,<span style="color: #272ad8;">6</span>,<span style="color: #272ad8;">8</span>,<span style="color: #272ad8;">10</span>,<span style="color: #272ad8;">12</span>,<span style="color: #272ad8;">20</span>].<span style="color: #3d1d81;">map</span>{ (<span style="color: #bb2ca2;">var</span> number) -> Die <span style="color: #bb2ca2;">in</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">return</span> <span style="color: #4f8187;">Die</span>(number);</div>
<div style="font-family: Menlo; font-size: 11px;">
};</div>
</div>
<div>
In this case since the die type is no longer implementing a protocol, I supply a simple closure that takes an AnyObject, casts it to a Die and supplies the text I want:</div>
<div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> dieToText={ (value:<span style="color: #703daa;">AnyObject</span>)->String <span style="color: #bb2ca2;">in</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> die = value <span style="color: #bb2ca2;">as</span> <span style="color: #4f8187;">Die</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">return</span> <span style="color: #d12f1b;">"d</span>\(die.<span style="color: #4f8187;">dieType</span>)<span style="color: #d12f1b;">"</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
};</div>
This is a more general solution, since I can now decorate any object with a simple provider, and it should scale to supplying views as well as title strings.</div>
<h3>
Creating the picker view</h3>
The big change was that I modified the picker view to have a fluent interface. Objective C and Swift supply some of these capabilities, but in this case I wanted to support many different objects without writing a ton of constructors. In this case I also supply a 'weight'. The sum of all weights is added up (it defaults to 1), and then that is used as the basic to calculate the width of each component. <br />
I also added methods that create numeric ranges.<br />
<h2>
<div style="color: #4f8187; font-family: Menlo; font-size: 11px; font-weight: normal;">
delegate<span style="color: black;">=</span>PickerProvider<span style="color: black;">().</span><span style="color: #31595d;">changeCallback</span><span style="color: black;">(returnValue)</span></div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
.<span style="color: #31595d;">addComponentRange</span>(<span style="color: #272ad8;">1</span>, maxValue: <span style="color: #272ad8;">20</span>).<span style="color: #31595d;">setWeight</span>(<span style="color: #272ad8;">1.5</span>)</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
.<span style="color: #31595d;">addComponent</span>(dieTypes)</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
.<span style="color: #31595d;">setTextProvider</span>(dieToText).<span style="color: #31595d;">setWeight</span>(<span style="color: #272ad8;">3</span>)</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
.<span style="color: #31595d;">addComponent</span>([<span style="color: #d12f1b;">"+"</span>,<span style="color: #d12f1b;">"-"</span>]).<span style="color: #31595d;">setWeight</span>(<span style="color: #272ad8;">0.5</span>)</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
.<span style="color: #31595d;">addComponentRange</span>(<span style="color: #272ad8;">0</span>,maxValue:<span style="color: #272ad8;">30</span>).<span style="color: #31595d;">setWeight</span>(<span style="color: #272ad8;">1.2</span>);</div>
</h2>
<h3>
The callback</h3>
<div>
The callback for when an item is selected will create a roller object with the selected values. Right now I have to do a lot of casting, I'm not sure of a good solution for that, but since I supply the components at the same location as I supply it, then it should be relatively type safe.</div>
<div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> getDieText(value:<span style="color: #703daa;">AnyObject</span>[])-><span style="color: #703daa;">String</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> numDice:<span style="color: #703daa;">Int</span>=value[<span style="color: #272ad8;">0</span>] <span style="color: #bb2ca2;">as</span> <span style="color: #703daa;">Int</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> dieType:<span style="color: #4f8187;">Die</span>=value[<span style="color: #272ad8;">1</span>] <span style="color: #bb2ca2;">as</span> <span style="color: #4f8187;">Die</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> modifier:<span style="color: #703daa;">String</span>=value[<span style="color: #272ad8;">2</span>] <span style="color: #bb2ca2;">as</span> <span style="color: #703daa;">String</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> num:<span style="color: #703daa;">Int</span>=value[<span style="color: #272ad8;">3</span>] <span style="color: #bb2ca2;">as</span> <span style="color: #703daa;">Int</span>;</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #4f8187;">currentRoller</span> = <span style="color: #4f8187;">Roller</span>(numDice: numDice, dieType: dieType, modSign: modifier, modifier: num)</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">return</span> <span style="color: #d12f1b;">"</span>\(numDice)<span style="color: #d12f1b;">d</span>\(dieType.<span style="color: #4f8187;">dieType</span>)\(modifier)\(num)<span style="color: #d12f1b;">"</span>;</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
</div>
<div>
<br /></div>
<div>
The Roller is simple:</div>
<div>
<div style="color: #bb2ca2; font-family: Menlo; font-size: 11px;">
@objc<span style="color: black;"> </span>class<span style="color: black;"> Roller</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> myDice=<span style="color: #272ad8;">0</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> myDie:<span style="color: #4f8187;">Die</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> myMod:<span style="color: #703daa;">String</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> myModifier:<span style="color: #703daa;">Int</span>;</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">init</span>(numDice:<span style="color: #703daa;">Int</span>, dieType:<span style="color: #4f8187;">Die</span>, modSign:<span style="color: #703daa;">String</span>, modifier:<span style="color: #703daa;">Int</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #4f8187;">myDice</span>=numDice;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #4f8187;">myDie</span>=dieType;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #4f8187;">myMod</span>=modSign;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #4f8187;">myModifier</span>=modifier;</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> roll()-><span style="color: #703daa;">String</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> result=<span style="color: #272ad8;">0</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> stringResult=<span style="color: #d12f1b;">"</span>\(<span style="color: #4f8187;">myDie</span>.<span style="color: #4f8187;">dieType</span>)<span style="color: #d12f1b;">:"</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">for</span> i <span style="color: #bb2ca2;">in</span> <span style="color: #272ad8;">1</span>...<span style="color: #4f8187;">myDice</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">if</span> i != <span style="color: #272ad8;">1</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
stringResult+=<span style="color: #d12f1b;">","</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> roll = <span style="color: #4f8187;">myDie</span>.<span style="color: #31595d;">roll</span>();</div>
<div style="font-family: Menlo; font-size: 11px;">
result += roll;</div>
<div style="font-family: Menlo; font-size: 11px;">
stringResult += <span style="color: #d12f1b;">"</span>\(roll)<span style="color: #d12f1b;">"</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">if</span> <span style="color: #4f8187;">myModifier</span> > <span style="color: #272ad8;">0</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">if</span> (<span style="color: #4f8187;">myMod</span> == <span style="color: #d12f1b;">"+"</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
result += <span style="color: #4f8187;">myModifier</span>;</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
stringResult += <span style="color: #d12f1b;">"+</span>\(<span style="color: #4f8187;">myModifier</span>)<span style="color: #d12f1b;">"</span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">else</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
stringResult += <span style="color: #d12f1b;">"</span>\(<span style="color: #4f8187;">myModifier</span>)<span style="color: #d12f1b;">"</span></div>
<div style="font-family: Menlo; font-size: 11px;">
result -= <span style="color: #4f8187;">myModifier</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
stringResult += <span style="color: #d12f1b;">"=</span>\(result)<span style="color: #d12f1b;">"</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">return</span> stringResult;</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
</div>
<h2>
Picker Provider implementation</h2>
<br />
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
//</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
// DieRollDataSource.swift</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
// SimpleDieRoller</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
//</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
// Created by Jon Lundy on 6/4/14.</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
// Copyright (c) 2014 Jon Lundy. All rights reserved.</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
//</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">import</span> Foundation</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">import</span> UIKit</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
// Component Info keeps information about each component. </div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
// It uses an array to track the data, as well suppling</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
// a text provider that converts an item in the array to</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
// a string. Note that I have a placeholder for a view</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
// provider, but that is not implemented yet.</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">class</span> ComponentInfo</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// The items contained within this component.</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> componentArray:<span style="color: #703daa;">Array</span><<span style="color: #703daa;">AnyObject</span>>;</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// The default text provider just casts the object to value.</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// Convert an item to a string. The default is</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// the built in Swift conversion.</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> textProvider:((<span style="color: #703daa;">AnyObject</span>)-><span style="color: #703daa;">String</span>)={(value:<span style="color: #703daa;">AnyObject</span>) <span style="color: #bb2ca2;">in</span> <span style="color: #bb2ca2;">return</span> <span style="color: #d12f1b;">"</span>\(value)<span style="color: #d12f1b;">"</span>};</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// Not implemented yet.</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> viewProvider:((<span style="color: #703daa;">AnyObject</span>)-><span style="color: #703daa;">UIView</span>)?=<span style="color: #703daa;">nil</span>;</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// By default just use a weight of 1.</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> weight:<span style="color: #703daa;">Double</span>=<span style="color: #272ad8;">1</span>;</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// A simple constructor.</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">init</span>(<span style="color: #bb2ca2;">_</span> array:<span style="color: #703daa;">Array</span><<span style="color: #703daa;">AnyObject</span>>)</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #4f8187;">componentArray</span>=array;</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
//the core class for the picker.</div>
<div style="color: #703daa; font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">class</span><span style="color: black;"> PickerProvider :</span>NSObject<span style="color: black;">, </span>UIPickerViewDataSource<span style="color: black;">,</span>UIPickerViewDelegate</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> componentArrays:<span style="color: #703daa;">Array</span><<span style="color: #4f8187;">ComponentInfo</span>>=<span style="color: #703daa;">Array</span><<span style="color: #4f8187;">ComponentInfo</span>>();</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> callback:(<span style="color: #703daa;">AnyObject</span>[])->() ;</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// Change the default callback, the default is to do</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// nothing.</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> changeCallback(selectedCall:(<span style="color: #703daa;">AnyObject</span>[])->()) -><span style="color: #4f8187;">PickerProvider</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="color: #d12f1b; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span><span style="color: #3d1d81;">println</span><span style="color: black;">(</span>"Entering setCallback"<span style="color: black;">);</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #4f8187;">callback</span>=selectedCall;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">return</span> <span style="color: #bb2ca2;">self</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// Set the text provider for the last component added.</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> setTextProvider(provider:(<span style="color: #703daa;">AnyObject</span>)-><span style="color: #703daa;">String</span>)-><span style="color: #4f8187;">PickerProvider</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="color: #4f8187; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>componentArrays<span style="color: black;">[</span>componentArrays<span style="color: black;">.</span><span style="color: #703daa;">count</span><span style="color: black;">-</span><span style="color: #272ad8;">1</span><span style="color: black;">].</span>textProvider<span style="color: black;">=provider;</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">return</span> <span style="color: #bb2ca2;">self</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// Set the weight for the last callback.</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> setWeight(weight:<span style="color: #703daa;">Double</span>) -><span style="color: #4f8187;">PickerProvider</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="color: #4f8187; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>componentArrays<span style="color: black;">[</span>componentArrays<span style="color: black;">.</span><span style="color: #703daa;">count</span><span style="color: black;">-</span><span style="color: #272ad8;">1</span><span style="color: black;">].</span>weight<span style="color: black;">=weight;</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">return</span> <span style="color: #bb2ca2;">self</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// Add a component</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> addComponent(newArray:<span style="color: #703daa;">Array</span><<span style="color: #703daa;">AnyObject</span>> )-><span style="color: #4f8187;">PickerProvider</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> comp = <span style="color: #4f8187;">ComponentInfo</span>(newArray);</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #4f8187;">componentArrays</span>.<span style="color: #3d1d81;">append</span>(comp);</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">return</span> <span style="color: #bb2ca2;">self</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// Add a range of numbers.</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> addComponentRange(minValue:<span style="color: #703daa;">Int</span>, maxValue:<span style="color: #703daa;">Int</span>)-><span style="color: #4f8187;">PickerProvider</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> newarray=<span style="color: #31595d;">getArray</span>(minValue, maxNum:maxValue)</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #31595d;">addComponent</span>(newarray);</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">return</span> <span style="color: #bb2ca2;">self</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// Create an array of numbers.</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> getArray(minNum:<span style="color: #703daa;">Int</span>,maxNum:<span style="color: #703daa;">Int</span>) -> <span style="color: #703daa;">Int</span>[]</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> returnValue=<span style="color: #703daa;">Int</span>[]();</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">for</span> i <span style="color: #bb2ca2;">in</span> minNum...maxNum</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
returnValue.<span style="color: #3d1d81;">append</span>(i);</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">return</span> returnValue;</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// The initial method will take a callback for when a selection si made as well</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// as the array of objects to display.</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">init</span>()</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #4f8187;">callback</span> = { (selectedRows: <span style="color: #703daa;">AnyObject</span>[])->() <span style="color: #bb2ca2;">in</span></div>
<div style="font-family: Menlo; font-size: 11px;">
};</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// Standard override return the number of rows for the given component.</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> pickerView(pickerview: <span style="color: #703daa;">UIPickerView</span>!,numberOfRowsInComponent component: <span style="color: #703daa;">Int</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
-> <span style="color: #703daa;">Int</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> array=<span style="color: #31595d;">getArray</span>(component);</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">return</span> array.<span style="color: #703daa;">count</span>;</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// returns the number of 'columns' to display.</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> numberOfComponentsInPickerView(pickerView: <span style="color: #703daa;">UIPickerView</span>!) -> <span style="color: #703daa;">Int</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="color: #4f8187; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span><span style="color: #bb2ca2;">return</span><span style="color: black;"> </span>componentArrays<span style="color: black;">.</span><span style="color: #703daa;">count</span><span style="color: black;">;</span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// Internal function to retrieve a specific array.</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> getArray( compNum:<span style="color: #703daa;">Int</span>) -> <span style="color: #703daa;">Array</span><<span style="color: #703daa;">AnyObject</span>></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="color: #4f8187; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span><span style="color: #bb2ca2;">return</span><span style="color: black;"> </span>componentArrays<span style="color: black;">[compNum].</span>componentArray<span style="color: black;">;</span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// Get the value for a given component. Just use the standard swift to make it into a string.</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> pickerView(pickerView: <span style="color: #703daa;">UIPickerView</span>!, titleForRow row: <span style="color: #703daa;">Int</span>, forComponent component: <span style="color: #703daa;">Int</span>) -> <span style="color: #703daa;">String</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> comp=<span style="color: #4f8187;">componentArrays</span>[component];</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> value : <span style="color: #703daa;">AnyObject</span>=comp.<span style="color: #4f8187;">componentArray</span>[row];</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">return</span> comp.<span style="color: #4f8187;">textProvider</span>(value);</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// Right now weight all arrays equally for the size. For a real approach we would need to be</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> pickerView(pickerView: <span style="color: #703daa;">UIPickerView</span>!, widthForComponent component: <span style="color: #703daa;">Int</span>) -> <span style="color: #703daa;">CGFloat</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> totalWeight:<span style="color: #703daa;">Double</span>=<span style="color: #272ad8;">0.0</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> myComponent=<span style="color: #3d1d81;">self</span>.<span style="color: #4f8187;">componentArrays</span>[component].<span style="color: #4f8187;">weight</span>;</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">for</span> component <span style="color: #bb2ca2;">in</span> <span style="color: #bb2ca2;">self</span>.<span style="color: #4f8187;">componentArrays</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
totalWeight += component.<span style="color: #4f8187;">weight</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> width:<span style="color: #703daa;">Double</span>=pickerView.<span style="color: #703daa;">frame</span>.<span style="color: #703daa;">width</span>-<span style="color: #272ad8;">10</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> value=width / totalWeight * myComponent;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">return</span> value;</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> pickerView(pickerView: <span style="color: #703daa;">UIPickerView</span>!, didSelectRow row: <span style="color: #703daa;">Int</span>, inComponent component: <span style="color: #703daa;">Int</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> returnValue=<span style="color: #703daa;">AnyObject</span>[]();</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">for</span> i <span style="color: #bb2ca2;">in</span> <span style="color: #272ad8;">0</span>..<span style="color: #4f8187;">componentArrays</span>.<span style="color: #703daa;">count</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> selected=pickerView.<span style="color: #3d1d81;">selectedRowInComponent</span>(i)</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> array=<span style="color: #31595d;">getArray</span>(i);</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> value=array[selected]</div>
<div style="font-family: Menlo; font-size: 11px;">
returnValue.<span style="color: #3d1d81;">append</span>(value);</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">self</span>.<span style="color: #4f8187;">callback</span>(returnValue);</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div>
<br /></div>
<h2>
What's next:</h2>
The next version should support:<br />
The ability to have a custom view in addition to pure text in the picker.<br />
The ability to have the contents of some of the components change based upon selections in the previous components (for example the date picker won't allow you to pick Day 31 for February.Unknownnoreply@blogger.com1tag:blogger.com,1999:blog-686619119323333549.post-5125356920018276962014-06-18T21:22:00.001-07:002014-06-18T21:22:24.305-07:00Cannot downcast from 'AnyObject' to non-@objc protocol typeThe next item I wished to do was to pass in an object to my picker and let it be a choice. I defined a simple object type of Die, which represents a type of die.<br />
<br />
I could not find a way to have interpolation work with the object, for "\(value)", so I decided to have a protocol that can be checked for.<br />
<br />
I defined the following protocol:<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">protocol</span> ProvidesTextValue</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> textValue:<span style="color: #703daa;">String</span> { <span style="color: #bb2ca2;">get</span>}</div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="font-family: Times; font-size: small;">With the following usage:</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> pickerView(pickerView: <span style="color: #703daa;">UIPickerView</span>!, titleForRow row: <span style="color: #703daa;">Int</span>, forComponent component: <span style="color: #703daa;">Int</span>) -> <span style="color: #703daa;">String</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> array=<span style="color: #31595d;">getArray</span>(component);</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> value : <span style="color: #703daa;">AnyObject</span>=array[row];</div>
<div style="color: #4f8187; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span><span style="color: #bb2ca2;">if</span><span style="color: black;"> value </span><span style="color: #bb2ca2;">is</span><span style="color: black;"> </span>ProvidesTextValue</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> textProvider=value <span style="color: #bb2ca2;">as</span> <span style="color: #703daa;">ProvidesTextValue</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">return</span> <span style="color: #d12f1b;">"</span>\(textProvider.textValue)<span style="color: #d12f1b;">"</span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">return</span> <span style="color: #d12f1b;">"</span>\(value)<span style="color: #d12f1b;">"</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div>
<br /></div>
<div>
Which resulted in the error:</div>
<div>
<div style="font-family: Menlo; font-size: 11px; margin-left: 12px; text-indent: -12px;">
DieRollDataSource.swift:61:18: Cannot downcast from 'AnyObject' to non-@objc protocol type 'ProvidesTextValue'</div>
<div style="font-family: Menlo; font-size: 11px; margin-left: 12px; text-indent: -12px;">
<br /></div>
<div style="margin-left: 12px; text-indent: 0px;">
The <a href="https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/swift_programming_language/Protocols.html">Swift Programming guide</a> provides the following guidance:</div>
<div style="margin-left: 12px; text-indent: 0px;">
<br /></div>
<div class="aside-title" style="background-color: #f9f9f9; border: 0px; color: grey; font-family: Helvetica, Arial, sans-serif; font-size: 0.6em; letter-spacing: 2px; margin-bottom: 8px; outline: 0px; padding: 0px; text-transform: uppercase; vertical-align: baseline;">
NOTE</div>
<div class="para" style="background-color: #f9f9f9; border: 0px; color: #414141; font-family: Helvetica, Arial, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 8px; outline: 0px; padding: 0px; vertical-align: baseline;">
You can check for protocol conformance only if your protocol is marked with the <code class="code-voice" style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; color: grey; font-family: Menlo, monospace; font-size: 0.9em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-wrap: break-word;">@objc</code> attribute, as seen for the <code class="code-voice" style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; color: grey; font-family: Menlo, monospace; font-size: 0.9em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-wrap: break-word;">HasArea</code> protocol above. This attribute indicates that the protocol should be exposed to Objective-C code and is described in <em class="u-book" style="background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"><a href="https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/BuildingCocoaApps/index.html#//apple_ref/doc/uid/TP40014216" style="background-color: transparent; border: 0px; color: #0088cc; margin: 0px; outline: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;" target="_self">Using Swift with Cocoa and Objective-C</a></em>. Even if you are not interoperating with Objective-C, you need to mark your protocols with the <code class="code-voice" style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; color: grey; font-family: Menlo, monospace; font-size: 0.9em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-wrap: break-word;">@objc</code> attribute if you want to be able to check for protocol conformance.</div>
<div class="para" style="background-color: #f9f9f9; border: 0px; color: #414141; font-family: Helvetica, Arial, sans-serif; font-size: 12px; line-height: 17px; margin-bottom: 8px; outline: 0px; padding: 0px; vertical-align: baseline;">
Note also that <code class="code-voice" style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; color: grey; font-family: Menlo, monospace; font-size: 0.9em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-wrap: break-word;">@objc</code> protocols can be adopted only by classes, and not by structures or enumerations. If you mark your protocol as <code class="code-voice" style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; color: grey; font-family: Menlo, monospace; font-size: 0.9em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-wrap: break-word;">@objc</code> in order to check for conformance, you will be able to apply that protocol only to class types.</div>
<div style="font-family: Menlo; font-size: 11px; margin-left: 12px; min-height: 13px; text-indent: -12px;">
<br /></div>
<div style="margin-left: 12px; min-height: 13px; text-indent: 0px;">
So I had to modify the protocol definition to be:</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">@objc</span> <span style="color: #bb2ca2;">protocol</span> ProvidesTextValue</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> textValue:<span style="color: #703daa;">String</span> { <span style="color: #bb2ca2;">get</span>}</div>
<div style="margin-left: 12px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<br /></div>
This resulted in a runtime error with the very 'useful' error message:<br />
<b style="font-family: Menlo; font-size: 11px;">** NSForwarding: warning: object 0x110fa0000 of class '_TtC15SimpleDieRoller3Die' does not implement methodSignatureForSelector: -- trouble ahead</b><br />
<br />
I then modified the class definition with @objc as well, which solved the problem:<br />
<div style="color: #4f8187; font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">@objc</span><span style="color: black;"> </span><span style="color: #bb2ca2;">class</span><span style="color: black;"> Die : </span>ProvidesTextValue</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> dieType:<span style="color: #703daa;">Int</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">init</span>(die:<span style="color: #703daa;">Int</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #4f8187;">dieType</span>=die;</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> textValue:<span style="color: #703daa;">String</span> { <span style="color: #bb2ca2;">return</span> <span style="color: #d12f1b;">"</span>\(<span style="color: #4f8187;">dieType</span>)<span style="color: #d12f1b;">"</span> }</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
}</div>
</div>
Unknownnoreply@blogger.com2tag:blogger.com,1999:blog-686619119323333549.post-51591003762820897602014-06-16T20:52:00.002-07:002014-06-16T20:52:54.336-07:00A first cut at a swift library to display a UIPickerViewBelow is my first cut at creating a class that manages the data for a picker view. The standard examples have your view controller managing the rows and data for the picker. I think that this is a bad connection of concerns, so I wanted a generic class that will manage the data for me.<br />
<br />
The example is a simple polyhedral dice roller, which lets you pick a number of dice, of a certain type, and add a modifier.<br />
<br />
Below is a screenshot of the picker.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhtTHXdCCO3nxwxKg9NKX0SlqVcLlAqPIAf5NwkLAh-FocCZ-06IHefJnelufBe8RJtigS7kZtNJ6JzWehJcIq8z8ov22ZtBNXgWfv3vwQ_RiaetiTiOfG7C5vOVxW7LF94_cMsF0vbnw/s1600/iOS+Simulator+Screen+Shot+Jun+16,+2014,+10.34.29+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhtTHXdCCO3nxwxKg9NKX0SlqVcLlAqPIAf5NwkLAh-FocCZ-06IHefJnelufBe8RJtigS7kZtNJ6JzWehJcIq8z8ov22ZtBNXgWfv3vwQ_RiaetiTiOfG7C5vOVxW7LF94_cMsF0vbnw/s1600/iOS+Simulator+Screen+Shot+Jun+16,+2014,+10.34.29+PM.png" height="320" width="180" /></a></div>
As you can see I just have a simple picker at the top of the screen. Right now all it does is print out the results to the console:<br />
<br />
<div style="font-family: Menlo; font-size: 11px;">
<b>selected row [1, d8, +, 0]</b></div>
<div style="font-family: Menlo; font-size: 11px;">
<b>selected row [3, d8, +, 0]</b></div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
<b>selected row [3, d8, +, 2]</b></div>
<div>
<br />
<br />
The view code is just concerned with establishing the data that we wish to display:<br />
<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> delegate:<span style="color: #4f8187;">PickerProvider</span>? = <span style="color: #703daa;">nil</span>;</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">init</span>(nibName nibNameOrNil: <span style="color: #703daa;">String</span>!, bundle nibBundleOrNil: <span style="color: #703daa;">NSBundle</span>!)</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">super</span>.<span style="color: #bb2ca2;">init</span>(nibName: nibNameOrNil, bundle: nibBundleOrNil)</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> getArray(minNum:<span style="color: #703daa;">Int</span>,maxNum:<span style="color: #703daa;">Int</span>) -> <span style="color: #703daa;">Int</span>[]</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> returnValue=<span style="color: #703daa;">Int</span>[]();</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">for</span> i <span style="color: #bb2ca2;">in</span> minNum...maxNum</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
returnValue.<span style="color: #3d1d81;">append</span>(i);</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">return</span> returnValue;</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">init</span>(coder aDecoder: <span style="color: #703daa;">NSCoder</span>!)</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #4f8187;">delegate</span>=<span style="color: #703daa;">nil</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">super</span>.<span style="color: #bb2ca2;">init</span>(coder:aDecoder)</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">override</span> <span style="color: #bb2ca2;">func</span> viewDidLoad() {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">super</span>.<span style="color: #3d1d81;">viewDidLoad</span>()</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// Create an array of numbers for dice. Use the map function to prepend a 'd' in</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// front of them.</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> dieTypes=[<span style="color: #272ad8;">4</span>,<span style="color: #272ad8;">6</span>,<span style="color: #272ad8;">8</span>,<span style="color: #272ad8;">10</span>,<span style="color: #272ad8;">12</span>,<span style="color: #272ad8;">20</span>].<span style="color: #3d1d81;">map</span>{ (<span style="color: #bb2ca2;">var</span> number) -> String <span style="color: #bb2ca2;">in</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">return</span> <span style="color: #d12f1b;">"d</span>\(number)<span style="color: #d12f1b;">"</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
};</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// Note that this is actually a (selectedRows:AnyObject[])->() even though it is</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// declared as (selectedRows:AnyObject[]). If you declare the paraemter of the method</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// to be (value:AnyObject[]) you will get a compile error.</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> returnValue = { (selectedRows: <span style="color: #703daa;">AnyObject</span>[])->() <span style="color: #bb2ca2;">in</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #3d1d81;">println</span>(<span style="color: #d12f1b;">"selected row </span>\(selectedRows)<span style="color: #d12f1b;">"</span>);</div>
<div style="font-family: Menlo; font-size: 11px;">
};</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> numDice=<span style="color: #31595d;">getArray</span>(<span style="color: #272ad8;">1</span>,maxNum:<span style="color: #272ad8;">20</span>);</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> modifiers=<span style="color: #31595d;">getArray</span>(<span style="color: #272ad8;">0</span>,maxNum:<span style="color: #272ad8;">30</span>);</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> modRolls=[<span style="color: #d12f1b;">"+"</span>,<span style="color: #d12f1b;">"-"</span>];</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// Pass the callback to the new delegate, as well as the list of arrays.</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #4f8187;">delegate</span>=<span style="color: #4f8187;">PickerProvider</span>(selectedCall:returnValue</div>
<div style="font-family: Menlo; font-size: 11px;">
,arrays:numDice,dieTypes,modRolls,modifiers);</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// Set the picker view to use the subclass as both a delgate and dataSource.</div>
<div style="color: #4f8187; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>pickerView<span style="color: black;">.</span><span style="color: #703daa;">dataSource</span><span style="color: black;">=</span>delegate<span style="color: black;">;</span></div>
<div style="color: #4f8187; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>pickerView<span style="color: black;">.</span><span style="color: #703daa;">delegate</span><span style="color: black;">=</span>delegate<span style="color: black;">;</span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">@IBOutlet</span> <span style="color: #bb2ca2;">var</span> pickerView : <span style="color: #703daa;">UIPickerView</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">override</span> <span style="color: #bb2ca2;">func</span> didReceiveMemoryWarning() {</div>
<div style="color: #3d1d81; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span><span style="color: #bb2ca2;">super</span><span style="color: black;">.</span>didReceiveMemoryWarning<span style="color: black;">()</span></div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// Dispose of any resources that can be recreated.</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<br />
I started from a simple 'single view' template, added a picker view, and set the delegate to be 'pickerView'.<br />
<br />
The core of the logic is in the PickerProvider class, this class is designed to be a general supplier of picker information. Right now it only displays text titles, and will subdivide the picker into equal segments.<br />
<br />
<div style="color: #703daa; font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">class</span><span style="color: black;"> PickerProvider :</span>NSObject<span style="color: black;">, </span>UIPickerViewDataSource<span style="color: black;">,</span>UIPickerViewDelegate</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> myarrays:<span style="color: #703daa;">Array</span><<span style="color: #703daa;">Array</span><<span style="color: #703daa;">AnyObject</span>>>;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> callback:(value:<span style="color: #703daa;">AnyObject</span>[])->();</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// The initial method will take a callback for when a selection si made as well</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// as the array of objects to display.</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">init</span>(selectedCall:(value:<span style="color: #703daa;">AnyObject</span>[])->(), arrays: <span style="color: #703daa;">Array</span><<span style="color: #703daa;">AnyObject</span>> ... )</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #4f8187;">myarrays</span>=arrays;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #4f8187;">callback</span>=selectedCall</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// Standard override return the number of rows for the given component.</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> pickerView(pickerview: <span style="color: #703daa;">UIPickerView</span>!,numberOfRowsInComponent component: <span style="color: #703daa;">Int</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
-> <span style="color: #703daa;">Int</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> array=<span style="color: #31595d;">getArray</span>(component);</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">return</span> array.<span style="color: #703daa;">count</span>;</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// returns the number of 'columns' to display.</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> numberOfComponentsInPickerView(pickerView: <span style="color: #703daa;">UIPickerView</span>!) -> <span style="color: #703daa;">Int</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">return</span> <span style="color: #4f8187;">myarrays</span>.<span style="color: #703daa;">count</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// Internal function to retrieve a specific array.</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> getArray( compNum:<span style="color: #703daa;">Int</span>) -> <span style="color: #703daa;">Array</span><<span style="color: #703daa;">AnyObject</span>></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">return</span> <span style="color: #4f8187;">myarrays[</span>compNum];</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// Get the value for a given component. Just use the standard swift to make it into a string.</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> pickerView(pickerView: <span style="color: #703daa;">UIPickerView</span>!, titleForRow row: <span style="color: #703daa;">Int</span>, forComponent component: <span style="color: #703daa;">Int</span>) -> <span style="color: #703daa;">String</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> array=<span style="color: #31595d;">getArray</span>(component);</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> value : <span style="color: #703daa;">AnyObject</span>=array[row];</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">return</span> <span style="color: #d12f1b;">"</span>\(value)<span style="color: #d12f1b;">"</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// Right now weight all arrays equally for the size. For a real approach we would need to be</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> pickerView(pickerView: <span style="color: #703daa;">UIPickerView</span>!, widthForComponent component: <span style="color: #703daa;">Int</span>) -> <span style="color: #703daa;">CGFloat</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> numComp:<span style="color: #703daa;">NSNumber</span>=<span style="color: #bb2ca2;">self</span>.<span style="color: #31595d;">numberOfComponentsInPickerView</span>(pickerView);</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> width:<span style="color: #703daa;">Double</span>=pickerView.<span style="color: #703daa;">frame</span>.<span style="color: #703daa;">width</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> value=width / numComp.<span style="color: #703daa;">doubleValue</span>;</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">return</span> value;</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">func</span> pickerView(pickerView: <span style="color: #703daa;">UIPickerView</span>!, didSelectRow row: <span style="color: #703daa;">Int</span>, inComponent component: <span style="color: #703daa;">Int</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> returnValue=<span style="color: #703daa;">AnyObject</span>[]();</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">for</span> i <span style="color: #bb2ca2;">in</span> <span style="color: #272ad8;">0</span>..<span style="color: #4f8187;">myarrays</span>.<span style="color: #703daa;">count</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> selected=pickerView.<span style="color: #3d1d81;">selectedRowInComponent</span>(i)</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> array=<span style="color: #31595d;">getArray</span>(i);</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> value=array[selected]</div>
<div style="font-family: Menlo; font-size: 11px;">
returnValue.<span style="color: #3d1d81;">append</span>(value);</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">self</span>.<span style="color: #4f8187;">callback</span>(value:returnValue);</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
}</div>
</div>
<div>
<br /></div>
<div>
The core of the logic is in the PickerProvider class, this class is designed to be a general supplier of picker information. Right now it only displays text titles, and will subdivide the picker into equal segments.</div>
<div>
<br /></div>
<div>
Overall I would say that Swift makes for better written code. I've done similiar constructions in Objective-C, and this is far cleaner. The code is very readable, and barring a few quirks of Swift fairly easy to write.</div>
<div>
<br /></div>
<div>
This examples shows several usages of Swift constructs, the Closures are easier to write than blocks and are first class citizens. The map function is very neat and shows the ability to transform lists.</div>
<div>
<br /></div>
<div>
List creation and manipulation is far less verbose than in Objective-C, as is displaying primitives as a string.</div>
Unknownnoreply@blogger.com4tag:blogger.com,1999:blog-686619119323333549.post-42379985412838866082014-06-15T19:30:00.002-07:002014-06-15T19:30:46.450-07:00Holding closures in a member variableThe next problem that was not obvious was that I wanted to have a callback to my main routine from the picker data source. After some experimentation I declared the following:<br />
<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> callback:(value:<span style="color: #703daa;">AnyObject</span>[]);</div>
<div style="font-family: Menlo; font-size: 11px;">
<br /></div>
And I called the initializer with:<br />
<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">init</span>(selectedCall:(value:<span style="color: #703daa;">AnyObject</span>[]), arrays: <span style="color: #703daa;">Array</span><<span style="color: #703daa;">AnyObject</span>> ... )</div>
<div style="font-family: Menlo; font-size: 11px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<br /></div>
However when I declared the following:<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">let</span> returnValue = { (selectedRows: <span style="color: #703daa;">AnyObject</span>[]) <span style="color: #bb2ca2;">in</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #3d1d81;">println</span>(<span style="color: #d12f1b;">"selected row </span>\(selectedRows)<span style="color: #d12f1b;">"</span>);</div>
<div style="font-family: Menlo; font-size: 11px;">
};</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #008400;">// Pass the callback to the new delegate, as well as the list of arrays.</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> delegate=<span style="color: #4f8187;">PickerProvider</span>(selectedCall:returnValue</div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
,arrays:numDice,dieTypes,modRolls,modifiers);</div>
<div>
<br /></div>
<div>
This resulted in the compile time error of:<br />
<div style="font-family: Menlo; font-size: 11px; margin-left: 12px; text-indent: -12px;">
'(value: @lvalue AnyObject[]) -> $T3' is not identical to '(value: AnyObject[])'</div>
<div style="font-family: Menlo; font-size: 11px; margin-left: 12px; text-indent: -12px;">
'(AnyObject[]) -> ()' is not convertible to 'AnyObject[]'</div>
<div style="margin-left: 12px; text-indent: 0px;">
<br /></div>
What really happened is explained in Apple's swift manual in a note under <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html#//apple_ref/doc/uid/TP40014097-CH10-XID_204">functions</a>:<br />
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="background-color: #f9f9f9; color: #414141; font-family: Helvetica, Arial, sans-serif; font-size: 12px; line-height: 17px;">Strictly speaking, the </span><code class="code-voice" style="background-color: #f9f9f9; border: 0px; color: grey; font-family: Menlo, monospace; font-size: 0.9em; line-height: 17px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-wrap: break-word;">sayGoodbye</code><span style="background-color: #f9f9f9; color: #414141; font-family: Helvetica, Arial, sans-serif; font-size: 12px; line-height: 17px;"> function </span><em style="background-color: #f9f9f9; border: 0px; color: #414141; font-family: Helvetica, Arial, sans-serif; font-size: 12px; line-height: 17px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">does</em><span style="background-color: #f9f9f9; color: #414141; font-family: Helvetica, Arial, sans-serif; font-size: 12px; line-height: 17px;"> still return a value, even though no return value is defined. Functions without a defined return type return a special value of type </span><code class="code-voice" style="background-color: #f9f9f9; border: 0px; color: grey; font-family: Menlo, monospace; font-size: 0.9em; line-height: 17px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-wrap: break-word;">Void</code><span style="background-color: #f9f9f9; color: #414141; font-family: Helvetica, Arial, sans-serif; font-size: 12px; line-height: 17px;">. This is simply an empty tuple, in effect a tuple with zero elements, which can be written as </span><code class="code-voice" style="background-color: #f9f9f9; border: 0px; color: grey; font-family: Menlo, monospace; font-size: 0.9em; line-height: 17px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-wrap: break-word;">()</code><span style="background-color: #f9f9f9; color: #414141; font-family: Helvetica, Arial, sans-serif; font-size: 12px; line-height: 17px;">.</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="background-color: #f9f9f9; color: #414141; font-family: Helvetica, Arial, sans-serif; font-size: 12px; line-height: 17px;"><br /></span></div>
The Apple manual has a lot of explanations for how to use closures for existing libraries, but not a lot for how to write your own functions to use closures. The problem is that my return Value is actually<br />
<br />
<span style="font-family: Menlo; font-size: 11px;">(selectedRows: </span><span style="color: #703daa; font-family: Menlo; font-size: 11px;">AnyObject</span><span style="font-family: Menlo; font-size: 11px;">[])->()</span><span style="font-family: Menlo; font-size: 11px;"> </span><br />
<br />
While the compiler will currently let you declare variables and member functions without the ->(), it will not consider those the same as the real function.<br />
<br />
The solution to this one is to declare my code as:<br />
<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span> callback:(value:<span style="color: #703daa;">AnyObject</span>[])->();</div>
With an initializer of:<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">init</span>(selectedCall:(value:<span style="color: #703daa;">AnyObject</span>[])->(), arrays: <span style="color: #703daa;">Array</span><<span style="color: #703daa;">AnyObject</span>> ... )</div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="background-color: #f9f9f9; color: #414141; font-family: Helvetica, Arial, sans-serif; font-size: 12px; line-height: 17px;"><br /></span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="background-color: #f9f9f9; color: #414141; font-family: Helvetica, Arial, sans-serif; font-size: 12px; line-height: 17px;"><br /></span></div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-686619119323333549.post-80004824800845528372014-06-15T19:20:00.001-07:002014-06-15T19:20:04.982-07:00Swift is Reference counted, NOT garbage collectedThe first error I encountered was two fold, the difference between reference counting and garbage collection. I was attempting to create a class to manage the data for a multi-level picker, and I dislike the standard practice Apple uses to have the view controller manage the pickers (I think it belongs in the subclass).<br />
<br />
In my first attempt I tried the following code: (which would work great in Java)<br />
<pre> // Pass the callback to the new delegate, as well as the list of arrays.
var delegate=PickerProvider(selectedCall:returnValue
,arrays:numDice,dieTypes,modRolls,modifiers);
// Set the picker view to use the subclass as both a delgate and dataSource.
pickerView.dataSource=delegate;
pickerView.delegate=delegate;
</pre>
<br />
When I did this, I would end up with a very obscure error:<br />
<br />
<pre>014-06-15 21:16:11.737 SimpleDieRoller[11717:13412066] -[CALayerArray numberOfComponentsInPickerView:]: unrecognized selector sent to instance 0x10bf05330
2014-06-15 21:16:11.742 SimpleDieRoller[11717:13412066] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CALayerArray numberOfComponentsInPickerView:]: unrecognized selector sent to instance 0x10bf05330'
*** First throw call stack:
(
0 CoreFoundation 0x000000010046ee35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000101f1a9a0 objc_exception_throw + 45
2 CoreFoundation 0x00000001004751cd -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x00000001003cf89c ___forwarding___ + 988
4 CoreFoundation 0x00000001003cf438 _CF_forwarding_prep_0 + 120
5 UIKit 0x0000000100cd8398 -[UIPickerView _updateSelectedRows] + 69
6 UIKit 0x0000000100cd84ce -[UIPickerView didMoveToWindow] + 78
7 UIKit 0x0000000100d57052 -[UIView(Internal) _didMoveFromWindow:toWindow:] + 1496
8 UIKit 0x0000000100d56d41 -[UIView(Internal) _didMoveFromWindow:toWindow:] + 711
9 UIKit 0x0000000100d4f8e5 __45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 128
10 UIKit 0x0000000100d4f856 -[UIView(Hierarchy) _postMovedFromSuperview:] + 437
11 UIKit 0x0000000100d5968e -[UIView(Internal) _addSubview:positioned:relativeTo:] + 1610
12 UIKit 0x0000000100d29c3f -[UIWindow addRootViewControllerViewIfPossible] + 452
13 UIKit 0x0000000100d29e22 -[UIWindow _setHidden:forced:] + 276
14 UIKit 0x0000000100d369ad -[UIWindow makeKeyAndVisible] + 42
15 UIKit 0x0000000100ce4db9 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2753
16 UIKit 0x0000000100ce7529 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1222
17 UIKit 0x0000000100ce6510 -[UIApplication workspaceDidEndTransaction:] + 19
18 CoreFoundation 0x00000001003a5bbc __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
19 CoreFoundation 0x000000010039b095 __CFRunLoopDoBlocks + 341
20 CoreFoundation 0x000000010039a84c __CFRunLoopRun + 844
21 CoreFoundation 0x000000010039a296 CFRunLoopRunSpecific + 470
22 UIKit 0x0000000100ce5f26 -[UIApplication _run] + 413
23 UIKit 0x0000000100ce9308 UIApplicationMain + 2994
24 SimpleDieRoller 0x0000000100006c3d top_level_code + 77
25 SimpleDieRoller 0x0000000100006c7a main + 42
26 libdyld.dylib 0x0000000102478145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
</pre>
<br />
It was not obvious from this what was happening. What was really happening is that the 'var delegate' local variable was being allocated and given a count of 1. It was then assigned to<br />
'delegate' and datasource' of the picker.<br />
<br />
However those properties are declared:<br />
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: #bb2ca2;">var</span><span style="color: black;"> dataSource: </span><span style="color: #703daa;">UIPickerViewDataSource</span><span style="color: black;">! </span>// default is nil. weak reference</div>
<div style="color: #008400; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span><span style="color: #bb2ca2;">var</span><span style="color: black;"> delegate: </span><span style="color: #703daa;">UIPickerViewDelegate</span><span style="color: black;">! </span>// default is nil. weak reference</div>
<div>
<br /></div>
<div>
The fact that they were weak references, means that when the initializer left scope, the count was decremented, the variable was no longer valid, so the picker called an invalid reference. </div>
<div>
<br /></div>
<div>
This was an easy fix, I just made a member variable that held my picker source, however the error was so obscure it took a while to find it out.</div>
<div>
<br /></div>
<div>
<br /></div>
<br />Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-686619119323333549.post-11475151467861459922014-06-15T19:13:00.000-07:002014-06-15T19:13:07.237-07:00Introduction to swiftLike most developers I found the introduction of swift to be very interesting. One of the great barriers to iPhone development was the learning curve for Objective-C. Swift seems like a very interesting development. Instead of supporting an existing language they have added a new language that has attempted to diverge from C in many fashions.<br />
<br />
Swift has an interesting grab bag of features that it looks like the language designers decided to toss in.<br />
<br />
<br />
<ul>
<li>Explicit methods for having 'no value' instead of using null. This means that you can bypass the need that Java has for Double vs double, etc..</li>
<li>Enhanced for loops that try to support more expressiveness in terms of what to iterate over</li>
<li>Support for more advance switch statements, with no default fall through (a cause of bugs for decades), but which supports </li>
</ul>
<div>
<br /></div>
<div>
For a greater introduction to swift the best place is of course <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/">Apple's Swift introduction</a>. It covers the basics of the language, but so far in my brief experiences with Swift there are some gotchas that I have found which were not immediately obvious from the book.</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-686619119323333549.post-37630408553654248642013-03-24T08:35:00.001-07:002013-03-24T08:35:57.059-07:00Displaying an alert view with blocksThe default method for displaying an alert view is somewhat cumbersome if you have a dialog with many possible choices. I had a table cell that
had two buttons, one that would add to a number, and one that would subtract. I wanted to then add the ability to directly set the number.
As I started adding more choices, I ended up with a cumbersome dialog. The default alert view protocol expects only one callback, so you have to track what dialog you are showing, and have an case statement or if/else block for that.
Anytime you see that construct, you know it is time to refactor.
<br />
<br />
<h2>
Before</h2>
<div>
This code starts to get ugly quickly, multiple repeated lines, and the need to put an if/else in my alert callback. The logic was also hard to follow.</div>
<div>
<br /></div>
<pre>- (IBAction)subtract:(id)sender {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:self.tracker.subtractString message:@"How much:" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:self.tracker.subtractString,nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * alertTextField = [alert textFieldAtIndex:0];
alertTextField.keyboardType = UIKeyboardTypeNumberPad;
alertTextField.placeholder = [NSString stringWithFormat:@"How much to %@",self.tracker.subtractString];
self.add=false;
[alert show];
}
</pre>
<pre>- (IBAction)add:(id)sender {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:self.tracker.addString message:@"How much:" delegate:self cancelButtonTitle:@"Continue" otherButtonTitles:self.tracker.addString,nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * alertTextField = [alert textFieldAtIndex:0];
alertTextField.keyboardType = UIKeyboardTypeNumberPad;
alertTextField.placeholder = [NSString stringWithFormat:@"How much to %@",self.tracker.addString];
@"Enter how much to heal";
self.add=true;
[alert show];
}
</pre>
<pre>- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (alertView.cancelButtonIndex!=buttonIndex)
{
int num= [[[alertView textFieldAtIndex:0] text] intValue];
if (self.add)
{
self.tracker.currentValue+=num;
}
else
{
self.tracker.currentValue-=num;
}
NSError *error;
[self.tracker.managedObjectContext save:&error];
//self.currentLabel.text=[NSString stringWithFormat:@"%d",self.tracker.currentValue];
}
}
</pre>
The ability to create a block, an inline method, allows this code to be extracted into a common class, and then made FAR simpler for the receiver.<br />
<div>
<br />
The first step was creating the alert class that can display an alert view. This class assumes that there is only one prompt active at a time.</div>
<div>
<pre>@interface AlertButtonHelper : NSObject
@property(nonatomic, copy) void (^action)(NSString *);
- (void)promptForInput:(NSString *)prompt title:(NSString *)title withAcceptButton:(NSString *)acceptButton
endingAction:(void (^)(NSString *))action keyboardType:(enum UIKeyboardType)keyboardType;
@end
@implementation AlertButtonHelper {
}
// The main command to prompt.
- (void)promptForInput:(NSString *)prompt title:(NSString *)title withAcceptButton:(NSString *)acceptButton endingAction:(void (^)(NSString *))action keyboardType:(enum UIKeyboardType)keyboardType {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:title message:prompt delegate:self cancelButtonTitle:@"Cancel"
otherButtonTitles:acceptButton,nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * alertTextField = [alert textFieldAtIndex:0];
alertTextField.keyboardType = keyboardType;
alertTextField.placeholder = [NSString stringWithFormat:prompt];
[alert show];
self.action=action;
}
// The standard alert callbakc.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSString *value = [alertView textFieldAtIndex:0].text;
if (self.action!=nil)
{
self.action(value);
}
}
@end
</pre>
<br />
<div>
I ended up with a bunch of blocks that looked like this. Where the only difference was the one line where the number was used.</div>
<pre> void (^actionToTake)(NSString *) = ^(NSString *string) {
int num= [string intValue];
<b> self.tracker.currentValue=num;</b>
NSError *error;
[self.tracker.managedObjectContext save:&error];
};
</pre>
I'm using AppCode, which has a nifty <i>extract as block</i> feature. I used it to transform it into<br />
<pre>- (void (^)(NSString *))getModifyFunction:(void (^)(int))modifyValue {
void (^actionToTake)(NSString *) = ^(NSString *string) {
int num= [string intValue];
modifyValue(num);
NSError *error;
[self.tracker.managedObjectContext save:&error];
};
return actionToTake;
}
</pre>
<pre> void (^actionToTake)(NSString *)= [self getModifyFunction:^(int num) {
self.tracker.maxValue = num;
}];
</pre>
<br />
The final client code is:<br />
<br />
<pre>- (IBAction)subtract:(id)sender {
NSString *str=[NSString stringWithFormat:@"How much to %@",self.tracker.subtractString] ;
void (^actionToTake)(NSString *)= [self getModifyFunction:^(int num) {
self.tracker.currentValue -= num;
}];
[self.helper promptForInput:str title:self.tracker.subtractString withAcceptButton:self.tracker.subtractString
endingAction:actionToTake keyboardType:UIKeyboardTypeNumberPad];
}
- (IBAction)maxSelected:(id)sender {
NSString *str=[NSString stringWithFormat:@"Enter max Value"] ;
void (^actionToTake)(NSString *)= [self getModifyFunction:^(int num) {
self.tracker.maxValue = num;
}];
[self.helper promptForInput:str title:@"Max Value" withAcceptButton:@"Set Max"
endingAction:actionToTake keyboardType:UIKeyboardTypeNumberPad];
}
- (void (^)(NSString *))getModifyFunction:(void (^)(int))modifyValue {
void (^actionToTake)(NSString *) = ^(NSString *string) {
int num= [string intValue];
modifyValue(num);
NSError *error;
[self.tracker.managedObjectContext save:&error];
};
return actionToTake;
}
- (IBAction)add:(id)sender {
NSString *str=[NSString stringWithFormat:@"How much to %@",self.tracker.addString] ;
void (^actionToTake)(NSString *)= [self getModifyFunction:^(int num) {
self.tracker.maxValue += num;
}];
[self.helper promptForInput:str title:self.tracker.addString withAcceptButton:self.tracker.addString
endingAction:actionToTake keyboardType:UIKeyboardTypeNumberPad];
}
- (IBAction)currentSelected:(id)sender {
NSString *str=[NSString stringWithFormat:@"Enter current Value"] ;
void (^actionToTake)(NSString *)= [self getModifyFunction:^(int num) {
self.tracker.currentValue = num;
}];
[self.helper promptForInput:str title:@"Set Value" withAcceptButton:@"Set Value"
endingAction:actionToTake keyboardType:UIKeyboardTypeNumberPad];
}</pre>
<pre></pre>
<pre></pre>
I initialize helper in the init method.<br />
<br />
<pre> if (self.helper==nil)
{
self.helper=[[AlertButtonHelper alloc] init];
}
</pre>
The result has strange syntax, but eliminates a lot of redundant code. The use of blocks is a great way to isolate the 'different' items.
The next stage would be to extract a protocol for the alert interface, so that the method of displaying alerts could change if desired.
</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-686619119323333549.post-16724624868895626332011-10-04T19:56:00.000-07:002011-10-04T19:56:31.077-07:00App Code BetaOne of the difficulties that I've had with XCode is that the SDE is not as slick as I'm used to. When combined with some of the peculiarities of the language, it is more painful to code in this than in Eclipse for Java or Visual Studio for C# (especially with the Resharper plugin).
I've found that JetBrains, the creators of IntellJ and Resharper have created a <a href="http://www.jetbrains.com/objc/index.jsp">SDE called AppCode </a>for Objective C. I've tried it and it is far easier to use for most things than XCode (either 3 or 4). Right now the Beta is free, and you can sign up for a 50% discount on the final product.
<br />
<br />
I highly recommend it. The smart code completion is far better, it can automatically make properties for you, it has far better refactoring, and navigation.<br />
<br />
The only downsides are that it doesn't support interface builder, or modifying a SQL-Lite database. Also if you want to modify your project settings you still need to boot up XCode.<br />
<br />Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-686619119323333549.post-60555551291874854172011-10-04T19:52:00.000-07:002011-10-04T19:52:39.807-07:00XCode 4 static libraries in a project.I'm actually trying to get back into IOS development. I was attempting to get my static library to work properly in XCode 4. Apple does not make it easy to do this.
I found a really good blog article that helps with this. <a href="http://blog.carbonfive.com/2011/04/04/using-open-source-static-libraries-in-xcode-4/">Using Open Source Static Libraries in XCode 4</a>. While written for open source libraries, it works great with my personal static library as well.Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-686619119323333549.post-15687616592133417302011-03-18T09:40:00.000-07:002011-03-18T09:40:27.875-07:00Best eBook Lending ServiceWith the recent establishment of the ability to lend ebooks with the Kindle and the Nook, several 'swap clubs' have sprung up around the net where you can make your ebooks available for lending and borrow ebooks from others. I've tried three of the services and have found that they all work, although the last one I've tried has been FAR more successful. <br />
<br />
<a href="http://booklending.com/">Booklending.com</a> (formly Kindlelending.com until Amazon complained about their trademark). This is a pretty good service. It operations on a 'push' principle. When you log in you register what books you are interested in, and then wait for someone to send them to you. When you make your books available it will show if anyone is waiting for those books and offer up to 3 people that you can send to.<br />
<br />
It has a limited ability to see which books are available for lending, but no real strong search capabilities. I've received several books from here and it works OK. <br />
<br />
I've also lent books from this site. When you lend a book it opens Amazon in a frame and you can then use the Amazon lending capabilities to send your book out. When you list the books you have available you have to manually enter them.<br />
<br />
<a href="http://www.lendink.com/">http://www.lendink.com/</a> Lendink is another site, it has an advantage in that you can see what books are available and search by genre. It operates on a 'pull' principle. When you see a book you are interested in you can click it and the site will send an email to the owner of the book that you are interested in lending it.<br />
I've also lent books through this service. It operates very similarly to the booklending.com site. When you list the books you have available you have to manually enter them.<br />
<br />
This site supports both Nook and kindle books.<br />
<br />
Finally the last and best site is:<br />
<br />
<a href="http://www.lendle.me/">lendle.me</a><br />
This is the latest site I've tried. Unlike the other sites you have to enter your books before you can borrow from it. It provides a bookmark that runs a script on your Amazon page to parse out which books you have available. It then saves all of those and figures out which ones are lendable. <br />
<br />
It gives you two credits (loans) to start, and then as you register books it gives you more. I had 10 credits after I parsed out my books.<br />
<br />
For the previous sites you want to request many books, you don't have a good hit ratio. However for lendle.me I had a 7 out of 10 books lent to me in less than 48 hours, I had requested several because based on the other sites I expected to only get 2-3 hits with 10 requests. Lendle.me seems to have a wider selection of books (probably because it parses them from Amazon). I can generally read a book in 2 days, so I'm going to have to stop requesting just to keep up.<br />
<br />
If you are borrowing Kindle books, I recommend <a href="http://lendle.me/">lendle.me</a>, if you are borrowing Nook books then <a href="http://lendink.com/">lendink</a> is your choice. However it doesn't hurt to be on all 3 sites to maximize your borrowing and lending chances. <br />
<br />
I've been using the lending services to borrow books that I would check out of a library. I've added some new authors to my 'buy list' after doing so. It stretches my dollars and makes me more likely to take a chance, you can borrow the first book in a series and see if you want to be spending your money on the rest.Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-686619119323333549.post-18568091236474458402011-03-14T12:49:00.000-07:002011-03-14T12:49:18.126-07:00Application not picking up changes in a static libraryI recently wasted a lot of time, I was attempting to modify a class in a static library, and no matter what I did, it would NOT work. Eventually I added some log statements and they didn't show up, although old ones did.<br />
<br />
I found that when I made modifications to my static library the main application was not relinked. <br />
<br />
For my WallPaperEdit application, I found that the main executable was WallPaperEdit in the directory<br />
<br />
/Users/myusername/Library/Developer/Xcode/DerivedData/WallPaperEDit_randomLetters/Build/Products/Debug-iphonesimulator/WallPaperEdit.app<br />
<br />
When I made a change in my static library, it was not being picked up for this, and it was not being relinked. Even doing a clean did not work.<br />
<br />
If I modify a file in my main library, then it recompiles and relinks this, and my new change is picked up. I'm going to use that as a workaround for now, and then try and find the build setting that makes this behavior as it properly does.<br />
<br />
While XCode4 says it supports automated dependencies between multiple projects, I've found that it can sometimes be painful.Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-686619119323333549.post-70008754510801618622011-02-15T19:27:00.000-08:002011-02-15T19:27:17.093-08:00Apple's new subscription model.I'm seeing a lot of reports on Apple's new subscription model. Providers that provide content that is purchased outside the app store now has to provide a 'in app purchase' to get that material, costing 30%. This is a killer, move on Apple's part, and if they enforce it you can say goodbye to Kindle, Nook, Netflix, etc... The companies can't afford to take this cut.<br />
<br />
With the agency model for ebooks, 5 out of 6 of the big publishers in the US now set their prices for their ebooks. When Amazon sells one of these ebooks, it gets 30% of the list price, and the publisher gets 70% of the list price. By the contract Amazon can NOT change this price.<br />
<br />
If they were to make this available as an in-app purchase, then Apple would get 30%, the publisher would get 70%, and Amazon would get nothing. Obviously Amazon will not go for this.<br />
<br />
I wonder if Apple is attempting to push all other ebook providers off the App Store, or if it is just rattling cages. If they do push all other ebook providers off, then I might have to reconsider using the Apple Ecosystem. I have NO desire to use iBooks, and will never use it. It ties me to one device, and Apple has just shown how unwise that is. <br />
<br />
This seems like a foolish move now that serious iPhone contenders are on the horizon, between Android, and now Windows 7 (with the Nokia deal), IOS could have some troubles. I would rather be using the Windows 7 toolset instead of IOS, however the market is not currently there.Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-686619119323333549.post-20458382615718330802010-12-17T12:29:00.000-08:002010-12-17T12:29:52.342-08:00Side Track: Unreal Development KitOK, I've been working (slowly) over the last month on refactoring my flow, but I'm not ready to post stuff. However I decided that I was going to download the <a href="http://www.udk.com/">Unreal Development Kit</a> and see if I could get 'Epic Citadel' to compile and install my own version. Step 1 is to boot up a windows box and download the development kit. It's a 1.1 GB download. <br />
<br />
The UDK is Windows only, but interestingly enough it can send data directly to your iphone for testing and running. For submission to the developer program you need to use your Mac. I'm not sure if that means you can't use Objective-C stuff directly in it, probably not.<br />
<br />
It was trivially easy to get stuff running on the iPhone. I installed the application, started the Mobile Editor, and used their sample map. I then ran play install on iOS device, and it prompted me for my provisioning certificate, my developer certificate and for some plist information. I just moved those over from my mac, and boom I had a level running on my iPhone 4.<br />
<br />
I don't have much knowledge of the unreal editor, I played with the very first version when Unreal first came out. It looks like a non-trivial learning curve, however for those with skills creating models, textures and scripting, I think getting a really nice game has just become a lot easier. I look forward to a good game in the Epic Citadel map. (If I read their licensing terms correctly, you can use those models in your game, your milage may vary, I'm not a lawyer.) (Infinity blade is OK, but not my style of game).<br />
<br />
Epic did a great job of getting a third party editor working with iOS. I'm sure there are pitfalls in moving forward with a real game, but the first impressions are VERY impressive.<br />
(Epic requires the following text when writing documentation about using the Unreal Development Kit, since they are providing this for FREE, I think it's quite reasonable to comply with their legal requirements.)<br />
<br />
<br />
<br />
<br />
Note this blog post is not endorsed by Epic games. <br />
Unreal Development Kit (UDK) © 2009, Epic Games, Inc. Epic, Epic Games, Gears of War, Gears of War 2, Unreal, AnimSet Viewer, AnimTree Editor, Unreal Cascade, Unreal Content Browser, Unreal Development Kit, Unreal Editor, Unreal Engine, Unreal Kismet, Unreal Lightmass, Unreal Matinee, Unreal PhAT, UnrealScript and Unreal Tournament are trademarks or registered trademarks of Epic Games, Inc. in the United States of America and elsewhere. All other trademarks are the prUnknownnoreply@blogger.com0tag:blogger.com,1999:blog-686619119323333549.post-67952109482058271262010-10-25T21:34:00.000-07:002010-10-25T21:34:57.958-07:00Cloud Storage Dropbox vs SugarsyncRecenlty I started thinking about backups and decided to use cloud storage to help keep things up to date. Right now I'm testing two services out to see how they work. I'm using the free version, source is pretty small, but might upgrade to a paid service if its good enough.<br />
<br />
The two services I'm testing are Dropbox and sugarsync.<br />
<br />
Dropbox<br />
Dropbox is one of the most common ones out there. You establish a single directory on your system and then the dropbox application will monitor that directory and sync any files found to the cloud and other computers.<br />
<br />
It's easy to set up, and comes with a 2GB initial size.<br />
<br />
If I refer someone to dropbox both that person and myself get an extra 250 MB of space (up to an 8GB max). I should have found a referral link before signing up.<br />
<br />
If you want a referral link, just follow:<br />
<a href="http://www.dropbox.com/referrals/NTEzODMxNTQ5OQ">Dropbox referal link</a><br />
<br />
The iphone client is pretty nice and will even let you view your source files.<br />
<br />
Sugarsync<br />
This got good reviews and has one major advantage over dropbox. It can share ANY directory on your system. This is pretty good. In addition if some one is referral to the service by you, you and that person get 500 mb of extra space (not bad) really. 2 GB for free out of the box, another 500 mb for playing with it a little bit, and then one extra person signing up for a free account gives you 500 mb, with no upper limit. <br />
<br />
If you are want to try it out follow this link:<br />
<a href="https://www.sugarsync.com/referral?rf=eaa4gqn26jxfx">Sugarsync referral</a><br />
<br />
I wish I had found a referral link prior to signing up, it would have been extra space :)<br />
<br />
Right now I'm trying out both DropBox and Sugarsync on the same files in the same directory. This is a nice dual redundant backup, and I can see how they work for me.<br />
<br />
The iphone client lets you stream music, and view photos but doesn't let you view other files like DropBox does. I like the space and flexibility however.<br />
<br />
The nice things about these is the service runs in the background and updates your files as you change them. They can even track history for you. Painless source control. Of course I would still run a real repository, but this is a good 'backup'.<br />
<br />
I plan to run both services in parallel for a while to see which I like better.Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-686619119323333549.post-68751026692216274322010-09-16T20:29:00.000-07:002010-09-16T20:29:52.078-07:00Capturing part of an image from a UIImageViewI have a set of views over a picture and I wanted to pick the part of the picture under the views. I found <a href="http://stackoverflow.com/questions/2635371/how-to-crop-the-uiimage/3353844#3353844">some code on stack overflow</a> that helped me extract the part of the photo I wanted, however when I used it, it seemed to be scaled improperly and showed up strangely.<br />
<br />
I finally figured out that the reason was because the UIImageView scales the image, and I was using the view coordinates of the part I was interested to try and get the subimage from the picture. This means that when I'm choosing a number from 0-320 in the view and grabbing those coordinates from a 1200x1600 image. This meant that I was always in the top left part of the picture.<br />
<br />
The following code seems pretty close (it might still be a little off) but at least it grabs stuff in the right vicinity. I wanted to solve this problem even though I'm currently planning to actually abandon the 'grab' screen and put on separate dialog approach. Instead I'm going to create an overlay and place it on top of the current view, allowing precise dynamic changes to occur.<br />
<br />
<br />
<div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">- (<span style="color: #662ea7;">UIImage</span>*) getView:(<span style="color: #662ea7;">CGRect</span> )area</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">{</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #a513a0;">if</span> (<span style="color: #a513a0;">self</span>.<span style="color: #662ea7;">image</span>==<span style="color: #a513a0;">nil</span>)</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #a513a0;">return</span> <span style="color: #a513a0;">nil</span>;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #a513a0;">if</span> (<span style="color: #a513a0;">self</span>.<span style="color: #662ea7;">image</span>.<span style="color: #662ea7;">image</span>==<span style="color: #a513a0;">nil</span>)</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #a513a0;">return</span> <span style="color: #a513a0;">nil</span>;</div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> </span>UIGraphicsBeginImageContext<span style="color: black;">(area.</span><span style="color: #662ea7;">size</span><span style="color: black;">);</span></div><div style="color: #418715; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> </span>// we are using aspect fit, so we will be using which ever is the larger of these two scale factors.</div><div style="color: #662ea7; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> </span><span style="color: #a513a0;">float</span><span style="color: black;"> widthScaleFactor=</span><span style="color: #a513a0;">self</span><span style="color: black;">.</span>image<span style="color: black;">.</span>bounds<span style="color: black;">.</span>size<span style="color: black;">.</span>width<span style="color: black;">/</span><span style="color: #a513a0;">self</span><span style="color: black;">.</span>image<span style="color: black;">.</span>image<span style="color: black;">.</span>size<span style="color: black;">.</span>width<span style="color: black;">;</span></div><div style="color: #662ea7; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> </span><span style="color: #a513a0;">float</span><span style="color: black;"> heightScaleFactor=</span><span style="color: #a513a0;">self</span><span style="color: black;">.</span>image<span style="color: black;">.</span>bounds<span style="color: black;">.</span>size<span style="color: black;">.</span>height<span style="color: black;">/</span><span style="color: #a513a0;">self</span><span style="color: black;">.</span>image<span style="color: black;">.</span>image<span style="color: black;">.</span>size<span style="color: black;">.</span>height<span style="color: black;">;</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"> </div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"> </div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #a513a0;">float</span> scaleFactor=widthScaleFactor;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #37047f;">NSLog</span>(<span style="color: #ba2e1d;">@"scale factors are height=%f width=%f"</span>,widthScaleFactor,heightScaleFactor);</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #a513a0;">if</span> (widthScaleFactor<heightscalefactor)></heightscalefactor)></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> scaleFactor=heightScaleFactor;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"> </div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #662ea7;">CGRect</span> drawRect=<span style="color: #37047f;">CGRectMake</span>(-area.<span style="color: #662ea7;">origin</span>.<span style="color: #662ea7;">x</span>/scaleFactor, -area.<span style="color: #662ea7;">origin</span>.<span style="color: #662ea7;">y</span>/scaleFactor,</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #a513a0;">self</span>.<span style="color: #662ea7;">image</span>.<span style="color: #662ea7;">image</span>.<span style="color: #662ea7;">size</span>.<span style="color: #662ea7;">width</span>,<span style="color: #a513a0;">self</span>.<span style="color: #662ea7;">image</span>.<span style="color: #662ea7;">image</span>.<span style="color: #662ea7;">size</span>.<span style="color: #662ea7;">height</span>);</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="color: #662ea7; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> </span>UIImageOrientation<span style="color: black;"> currentOrient=</span><span style="color: #a513a0;">self</span><span style="color: black;">.</span>image<span style="color: black;">.</span>image<span style="color: black;">.</span>imageOrientation<span style="color: black;">;</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"> </div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #662ea7;">CGRect</span> destRect=<span style="color: #37047f;">CGRectMake</span>(<span style="color: #2600d5;">0</span>, <span style="color: #2600d5;">0</span>, area.<span style="color: #662ea7;">size</span>.<span style="color: #662ea7;">width</span>, area.<span style="color: #662ea7;">size</span>.<span style="color: #662ea7;">height</span>);</div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> </span><span style="color: #662ea7;">CGContextRef</span><span style="color: black;"> context=</span>UIGraphicsGetCurrentContext<span style="color: black;">();</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #37047f;">CGContextClipToRect</span>(context, destRect);</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> [<span style="color: #a513a0;">self</span>.<span style="color: #662ea7;">image</span>.<span style="color: #662ea7;">image</span> <span style="color: #37047f;">drawInRect</span>:drawRect];</div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> </span><span style="color: #662ea7;">UIImage</span><span style="color: black;">* im=</span>UIGraphicsGetImageFromCurrentImageContext<span style="color: black;">();</span></div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> </span>UIGraphicsEndImageContext<span style="color: black;">();</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #a513a0;">return</span> im;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">}</div><div><br />
</div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-686619119323333549.post-10422596206941943732010-09-16T19:37:00.000-07:002010-09-16T19:37:25.042-07:00Colored RGB sliders, without a XIBThe next step of my refactoring was to modify the Colored View sliders so that they are independent of a .xib file so that I can place them into any view. This gives me a lot of flexibility and reduces some of the dependance of a xib in a dependent library. <br />
In addition I found that when I programmatically created the sliders I could change the height and make it look somewhat nicer.<br />
<br />
<br />
I revised the creation code so that it creates 4 sliders programmatically spaced within a passed in view. It sets up callbacks in the current routine.<br />
<br />
When a color changes it calls a delegate with the changes. <br />
<br />
<br />
<div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">- (<span style="color: #a513a0;">void</span>) loadView : (<span style="color: #662ea7;">UIView</span> *)viewToUse</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">{</div><div style="color: #ba2e1d; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> </span><span style="color: #37047f;">NSLog</span><span style="color: black;">(</span>@"In loadView for ColorEditor"<span style="color: black;">);</span></div><div style="color: #418715; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> </span>// Don't use a nib.</div><div style="color: #662ea7; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> </span><span style="color: #a513a0;">self</span><span style="color: black;">.</span>leftMargin<span style="color: black;">=</span><span style="color: #2600d5;">10</span><span style="color: black;">;</span></div><div style="color: #662ea7; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> </span><span style="color: #a513a0;">self</span><span style="color: black;">.</span>rightMargin<span style="color: black;">=</span><span style="color: #2600d5;">10</span><span style="color: black;">;</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #a513a0;">self</span>.<span style="color: #662ea7;">spacing</span>=<span style="color: #2600d5;">5</span>;</div><div style="color: #662ea7; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> </span><span style="color: #a513a0;">self</span><span style="color: black;">.</span>fontSize<span style="color: black;">=</span><span style="color: #2600d5;">10</span><span style="color: black;">;</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #a513a0;">int</span> labelWidth=<span style="color: #2600d5;">74</span>;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #a513a0;">int</span> labelHeight=<span style="color: #2600d5;">21</span>;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"> </div><div style="color: #662ea7; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> viewToUse.</span>backgroundColor<span style="color: black;">=[</span>UIColor<span style="color: black;"> </span><span style="color: #37047f;">cyanColor</span><span style="color: black;">];</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"> </div><div style="color: #5e8087; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> </span><span style="color: #a513a0;">if</span><span style="color: black;"> (</span>currentColorSpace<span style="color: black;">==</span><span style="color: #a513a0;">nil</span><span style="color: black;">)</span></div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> </span><span style="color: #5e8087;">currentColorSpace</span><span style="color: black;"> = </span>CGColorSpaceCreateDeviceRGB<span style="color: black;">();</span></div><div style="color: #ba2e1d; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> </span><span style="color: #662ea7;">NSArray</span><span style="color: black;"> *sliderLabels= [</span><span style="color: #662ea7;">NSArray</span><span style="color: black;"> arrayWithObjects:</span><span style="color: #6f4a2c;">NSLocalizedString</span><span style="color: black;">(</span>@"Red"<span style="color: black;">, </span>@"Red"<span style="color: black;">), </span><span style="color: #6f4a2c;">NSLocalizedString</span><span style="color: black;">(</span>@"Green"<span style="color: black;">, </span>@"Green"<span style="color: black;">), </span><span style="color: #6f4a2c;">NSLocalizedString</span><span style="color: black;">(</span>@"Blue"<span style="color: black;">, </span>@"Blue"<span style="color: black;">),</span><span style="color: #6f4a2c;">NSLocalizedString</span><span style="color: black;">(</span>@"Transparency"<span style="color: black;">,</span>"Transparency"<span style="color: black;">), </span><span style="color: #a513a0;">nil</span><span style="color: black;">];</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"> </div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"> </div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #37047f;">NSLog</span>(<span style="color: #ba2e1d;">@"view height %f, width %f x %f, y %f"</span>,viewToUse.<span style="color: #662ea7;">frame</span>.<span style="color: #662ea7;">size</span>.<span style="color: #662ea7;">height</span>,viewToUse.<span style="color: #662ea7;">frame</span>.<span style="color: #662ea7;">size</span>.<span style="color: #662ea7;">width</span>,viewToUse.<span style="color: #662ea7;">frame</span>.<span style="color: #662ea7;">origin</span>.<span style="color: #662ea7;">x</span>,viewToUse.<span style="color: #662ea7;">frame</span>.<span style="color: #662ea7;">origin</span>.<span style="color: #662ea7;">y</span>);</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"> </div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #a513a0;">int</span> sliderHeight=(viewToUse.<span style="color: #662ea7;">frame</span>.<span style="color: #662ea7;">size</span>.<span style="color: #662ea7;">height</span>)/<span style="color: #2600d5;">4</span>-<span style="color: #5e8087;">spacing</span>;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"> </div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #37047f;">NSLog</span>(<span style="color: #ba2e1d;">@"SliderHeight %d"</span>,sliderHeight);</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"> </div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #a513a0;">for</span> (<span style="color: #a513a0;">int</span> i=<span style="color: #2600d5;">0</span>;i<<span style="color: #2600d5;">4</span>;i++)</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> {</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #662ea7;">CGRect</span> labelFrame=<span style="color: #37047f;">CGRectMake</span>(<span style="color: #2600d5;">0</span>, (sliderHeight+<span style="color: #5e8087;">spacing</span>)*i+sliderHeight/<span style="color: #2600d5;">2</span>-labelHeight/<span style="color: #2600d5;">2</span>, labelWidth, labelHeight);</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #662ea7;">UILabel</span> *label=[[<span style="color: #662ea7;">UILabel</span> <span style="color: #37047f;">alloc</span>] <span style="color: #37047f;">initWithFrame</span>:labelFrame];</div><div style="color: #662ea7; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> label.</span>adjustsFontSizeToFitWidth<span style="color: black;">=</span><span style="color: #6f4a2c;">true</span><span style="color: black;">;</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> label.<span style="color: #662ea7;">text</span>=[sliderLabels <span style="color: #37047f;">objectAtIndex</span>:i];</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> [viewToUse <span style="color: #37047f;">addSubview</span>:label];</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #662ea7;">CGRect</span> areaFrame=<span style="color: #37047f;">CGRectMake</span>(<span style="color: #5e8087;">leftMargin</span>+labelWidth, (sliderHeight+<span style="color: #5e8087;">spacing</span>)*i,viewToUse.<span style="color: #662ea7;">bounds</span>.<span style="color: #662ea7;">size</span>.<span style="color: #662ea7;">width</span>-<span style="color: #5e8087;">leftMargin</span>-<span style="color: #5e8087;">rightMargin</span>-labelWidth, sliderHeight);</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"> </div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #5e8087;">sliders</span>[i]=[[<span style="color: #662ea7;">UISlider</span> <span style="color: #37047f;">alloc</span>]<span style="color: #37047f;">init</span>];</div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> [</span><span style="color: #5e8087;">sliders</span><span style="color: black;">[i] </span>addTarget<span style="color: black;">:</span><span style="color: #a513a0;">self</span><span style="color: black;"> </span>action<span style="color: black;">:</span><span style="color: #a513a0;">@selector</span><span style="color: black;">(sliderChanged:) </span>forControlEvents<span style="color: black;">:</span>UIControlEventValueChanged<span style="color: black;">];</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #5e8087;">sliders</span>[i].<span style="color: #662ea7;">tag</span>=i; </div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> sliders[i].accessibilityLabel=[sliderLabels objectAtIndex:i];</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #37047f;">NSLog</span>(<span style="color: #ba2e1d;">@"Y is %d"</span>,(sliderHeight+<span style="color: #5e8087;">spacing</span>)*i);</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #5e8087;">sliders</span>[i].<span style="color: #662ea7;">frame</span>=areaFrame;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> [viewToUse <span style="color: #37047f;">addSubview</span>:<span style="color: #5e8087;">sliders</span>[i]];</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"> </div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> } </div><div style="color: #3e585d; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> [</span><span style="color: #a513a0;">self</span><span style="color: black;"> </span>calcColors<span style="color: black;">];</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">}</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">and the calc colors is:</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">- (<span style="color: #a513a0;">void</span>) calcColors</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">{</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #a513a0;">for</span> (<span style="color: #a513a0;">int</span> index=<span style="color: #2600d5;">0</span>;index<<span style="color: #2600d5;">4</span>;index++)</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> {</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> [<span style="color: #a513a0;">self</span> <span style="color: #3e585d;">setSlider</span>:<span style="color: #5e8087;">sliders</span>[index] <span style="color: #3e585d;">colorIndex</span>:index];</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> }</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"> </div><div style="color: #5e8087; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> </span>currentColor<span style="color: black;">=[</span><span style="color: #662ea7;">UIColor</span><span style="color: black;"> </span><span style="color: #37047f;">colorWithRed</span><span style="color: black;">:</span>colors<span style="color: black;">[</span><span style="color: #2600d5;">0</span><span style="color: black;">] </span><span style="color: #37047f;">green</span><span style="color: black;">:</span>colors<span style="color: black;">[</span><span style="color: #2600d5;">1</span><span style="color: black;">] </span><span style="color: #37047f;">blue</span><span style="color: black;">:</span>colors<span style="color: black;">[</span><span style="color: #2600d5;">2</span><span style="color: black;">] </span><span style="color: #37047f;">alpha</span><span style="color: black;">:</span>colors<span style="color: black;">[</span><span style="color: #2600d5;">3</span><span style="color: black;">]];<span class="Apple-tab-span" style="white-space: pre;"> </span></span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"> </div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #a513a0;">for</span> (<span style="color: #a513a0;">int</span> index=<span style="color: #2600d5;">0</span>;index<<span style="color: #2600d5;">4</span>;index++)</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> {</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #418715;">// numeric fields are not implemented. </span></div><div style="color: #418715; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> </span>// textFields[index].text=[NSString stringWithFormat:@"%d",(int) (colors[index]*255)];</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> }<span class="Apple-tab-span" style="white-space: pre;"> </span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #a513a0;">if</span> (<span style="color: #5e8087;">delegate</span>!=<span style="color: #a513a0;">nil</span>)</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> {</div><div style="color: #5e8087; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> [</span>delegate<span style="color: black;"> </span><span style="color: #3e585d;">colorChanged</span><span style="color: black;">:</span>currentColor<span style="color: black;">];</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> }</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"> </div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">}</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">This looks like:</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><br />
</div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEik3tL3GuYT-DtyEelvs2IoUwI3tA8tV9oTjkSCeKjZN9pbedCBRvFHgWJLpJ6p7qfY3rT0ygiArUJBemQU1di15Co0UmcYftKlF1vDjCRfLm5r7pZ4FpH6w0XgNuvAritsZkqh4Mq2sA/" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEik3tL3GuYT-DtyEelvs2IoUwI3tA8tV9oTjkSCeKjZN9pbedCBRvFHgWJLpJ6p7qfY3rT0ygiArUJBemQU1di15Co0UmcYftKlF1vDjCRfLm5r7pZ4FpH6w0XgNuvAritsZkqh4Mq2sA/" width="213" /></a></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><br />
</div><div><br />
</div><div>Note that the sliders (the area in blue) is the only thing the code does. The rest is a custom frame done in a .nib. This enables me to embed the sliders in a view:</div><div><br />
</div><div>The .nib in interface builder looks like this:</div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3a6bp9Aehtq5aUs4rNTZGkLI1_HRdZPrZLmZzO-IHSZuPdSB2ATDzvXzo660BrAMYpeiaeFGCdLPoq3odSYMNG4ACt7t5cDI3BQme-sKvVi3_2AojbkiUupb0x8gkR0Q0qNvYVojn8g/s1600/Screen+shot+2010-09-16+at+9.28.11+PM.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3a6bp9Aehtq5aUs4rNTZGkLI1_HRdZPrZLmZzO-IHSZuPdSB2ATDzvXzo660BrAMYpeiaeFGCdLPoq3odSYMNG4ACt7t5cDI3BQme-sKvVi3_2AojbkiUupb0x8gkR0Q0qNvYVojn8g/s320/Screen+shot+2010-09-16+at+9.28.11+PM.png" /></a></div><div><br />
</div><div><br />
</div><div>Note that I have an image view under a label. This allows me to change the label and show what it looks like under the label.</div><div><br />
</div><div>The yellow area is my current color for the slider view. I set it to a unique color so that I can watch what is going on.</div><div><br />
</div><div>I'm allowing the font and background color to be set using the tab bar. This is not standard behavior for a tab bar, but it is allowable in a modal dialog. I have a 3rd tab that will eventually be used for a font choice.</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div>The code for this is:</div><div><div style="color: #418715; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">- (<span style="color: #a513a0;">void</span>)viewDidLoad {</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> [<span style="color: #a513a0;">super</span> <span style="color: #37047f;">viewDidLoad</span>]; </div><div style="color: #5e8087; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> </span>colorEditor<span style="color: black;">=[[</span>ColorEditor<span style="color: black;"> </span><span style="color: #37047f;">alloc</span><span style="color: black;">]</span><span style="color: #37047f;">init</span><span style="color: black;">];</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #5e8087;">isFont</span>=<span style="color: #6f4a2c;">true</span>;</div><div style="color: #662ea7; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> </span><span style="color: #a513a0;">self</span><span style="color: black;">.</span>view<span style="color: black;">.</span>backgroundColor<span style="color: black;">=[</span>UIColor<span style="color: black;"> </span><span style="color: #37047f;">colorWithRed</span><span style="color: black;">:</span><span style="color: #2600d5;">0</span><span style="color: black;"> </span><span style="color: #37047f;">green</span><span style="color: black;">:</span><span style="color: #2600d5;">0</span><span style="color: black;"> </span><span style="color: #37047f;">blue</span><span style="color: black;">:</span><span style="color: #2600d5;">0</span><span style="color: black;"> </span><span style="color: #37047f;">alpha</span><span style="color: black;">:</span><span style="color: #2600d5;">0</span><span style="color: black;">];</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"> </div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">}</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">- (<span style="color: #a513a0;">void</span>) initColors:(<span style="color: #662ea7;">UIColor</span>*) fColor background:(<span style="color: #662ea7;">UIColor</span>*) back text:(<span style="color: #662ea7;">NSString</span>*) textValue image:(<span style="color: #662ea7;">UIImage</span>*)background {</div><div style="color: #5e8087; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> [</span>colorEditor<span style="color: black;"> </span><span style="color: #3e585d;">loadView</span><span style="color: black;">:</span>actionView<span style="color: black;">];</span></div><div style="color: #5e8087; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> </span>colorEditor<span style="color: black;">.</span><span style="color: #662ea7;">delegate</span><span style="color: black;">=</span><span style="color: #a513a0;">self</span><span style="color: black;">;</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #5e8087;">label</span>.<span style="color: #662ea7;">textColor</span>=fColor;</div><div style="color: #662ea7; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> </span><span style="color: #5e8087;">label</span><span style="color: black;">.</span>backgroundColor<span style="color: black;">=back;</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #5e8087;">label</span>.<span style="color: #662ea7;">text</span>=textValue;</div><div style="color: #5e8087; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> [</span>colorEditor<span style="color: black;"> </span><span style="color: #3e585d;">setCurrentColor</span><span style="color: black;">:</span>label<span style="color: black;">.</span><span style="color: #662ea7;">textColor</span><span style="color: black;">];</span></div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> </span><span style="color: #5e8087;">backImage</span><span style="color: black;">.</span><span style="color: #662ea7;">contentMode</span><span style="color: black;">=</span>UIViewContentModeScaleAspectFit<span style="color: black;">;</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #5e8087;">backImage</span>.<span style="color: #662ea7;">image</span>=background;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">}</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">- (<span style="color: #a513a0;">void</span>) colorChanged:(<span style="color: #662ea7;">UIColor</span>*) newColor</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">{</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #a513a0;">if</span> (<span style="color: #5e8087;">isFont</span>)</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #5e8087;">label</span>.<span style="color: #662ea7;">textColor</span>=newColor;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #a513a0;">else</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #5e8087;">label</span>.<span style="color: #662ea7;">backgroundColor</span>=newColor;</div><div style="color: #418715; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> </span>// Tell our delegate that we have our font colors changed.</div><div style="color: #662ea7; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> [</span><span style="color: #5e8087;">delegate</span><span style="color: black;"> </span><span style="color: #3e585d;">styleChanged</span><span style="color: black;">:</span><span style="color: #5e8087;">label</span><span style="color: black;">.</span>textColor<span style="color: black;"> </span><span style="color: #3e585d;">background</span><span style="color: black;">:</span><span style="color: #5e8087;">label</span><span style="color: black;">.</span>backgroundColor<span style="color: black;">];</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">}</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="color: #418715; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;">- (</span><span style="color: #a513a0;">void</span><span style="color: black;">)tabBar:(</span><span style="color: #662ea7;">UITabBar</span><span style="color: black;"> *)tabBar didSelectItem:(</span><span style="color: #662ea7;">UITabBarItem</span><span style="color: black;"> *)item </span>// called when a new view is selected by the user (but not programatically)</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">{</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #a513a0;">if</span> (item==<span style="color: #5e8087;">fontColor</span>)</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> {</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #5e8087;">isFont</span>=<span style="color: #6f4a2c;">true</span>;</div><div style="color: #5e8087; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> [</span>colorEditor<span style="color: black;"> </span><span style="color: #3e585d;">setCurrentColor</span><span style="color: black;">:</span>label<span style="color: black;">.</span><span style="color: #662ea7;">textColor</span><span style="color: black;">];</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"> </div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> }</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #a513a0;">else</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> {</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #5e8087;">isFont</span>=<span style="color: #6f4a2c;">false</span>;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> [<span style="color: #5e8087;">colorEditor</span> <span style="color: #3e585d;">setCurrentColor</span>:<span style="color: #5e8087;">label</span>.<span style="color: #662ea7;">backgroundColor</span>];</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> }</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #a513a0;">if</span> (<span style="color: #5e8087;">label</span> !=<span style="color: #a513a0;">nil</span>)</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> {</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #5e8087;">label</span>.<span style="color: #662ea7;">text</span>=item.<span style="color: #662ea7;">title</span>; </div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> } </div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> }</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="color: #a513a0; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">@end</div></div><div><br />
</div><div>I can now change the color editor and use it to set both the background and foreground color of a font.</div><div><br />
</div><div>And the header for this defines another protocol that allows the foreground and background color to be set.</div><div><br />
</div><div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: #a513a0;">@protocol</span> StyleChanged</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">- (<span style="color: #a513a0;">void</span>) styleChanged:(<span style="color: #662ea7;">UIColor</span>*) fontColor background:(<span style="color: #662ea7;">UIColor</span>*) backColor;<span class="Apple-tab-span" style="white-space: pre;"> </span></div><div style="color: #a513a0; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">@end</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="color: #662ea7; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: #a513a0;">@interface</span><span style="color: black;"> ColorEdit : </span>UIViewController<span style="color: black;"><</span>UITabBarDelegate<span style="color: black;">,</span><span style="color: #5e8087;">ColorListener</span><span style="color: black;">> {</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #a513a0;">IBOutlet</span> <span style="color: #662ea7;">UILabel</span> *label;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #a513a0;">IBOutlet</span> <span style="color: #662ea7;">UIView</span> *actionView;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #a513a0;">IBOutlet</span> <span style="color: #662ea7;">UIImageView</span> *backImage;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #5e8087;">ColorEditor</span> *colorEditor;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #a513a0;">IBOutlet</span> <span style="color: #662ea7;">UITabBarItem</span> *fontColor;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #a513a0;">IBOutlet</span> <span style="color: #662ea7;">UITabBarItem</span> *backColor;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #a513a0;">bool</span> isFont;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #a513a0;">id</span><<span style="color: #5e8087;">StyleChanged</span>> delegate;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"> </div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">}</div><div style="color: #a513a0; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">@property<span style="color: black;"> (</span>nonatomic<span style="color: black;">,</span>retain<span style="color: black;">) </span>IBOutlet<span style="color: black;"> UIImageView *backImage;</span></div><div style="color: #a513a0; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">@property<span style="color: black;"> (</span>nonatomic<span style="color: black;">,</span>retain<span style="color: black;">) </span>IBOutlet<span style="color: black;"> UILabel *label;</span></div><div style="color: #a513a0; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">@property<span style="color: black;"> (</span>nonatomic<span style="color: black;">,</span>retain<span style="color: black;">) </span>IBOutlet<span style="color: black;"> UIView *actionView;</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: #a513a0;">@property</span> (<span style="color: #a513a0;">nonatomic</span>,<span style="color: #a513a0;">retain</span>) ColorEditor *colorEditor;</div><div style="color: #a513a0; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">@property<span style="color: black;"> (</span>nonatomic<span style="color: black;">,</span>retain<span style="color: black;">) </span>IBOutlet<span style="color: black;"> UITabBarItem *fontColor;</span></div><div style="color: #a513a0; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">@property<span style="color: black;"> (</span>nonatomic<span style="color: black;">,</span>retain<span style="color: black;">) </span>IBOutlet<span style="color: black;"> UITabBarItem *backColor;</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: #a513a0;">@property</span> (<span style="color: #a513a0;">nonatomic</span>,<span style="color: #a513a0;">retain</span>) <span style="color: #a513a0;">id</span><stylechanged> delegate;</stylechanged></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">- (<span style="color: #a513a0;">void</span>) initColors : (<span style="color: #662ea7;">UIColor</span>*) fontColor background:(<span style="color: #662ea7;">UIColor</span>*) back text:(<span style="color: #662ea7;">NSString</span>*) textValue image:(<span style="color: #662ea7;">UIImage</span>*)background ;</div></div><div><br />
</div><div><br />
</div><div><br />
</div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-686619119323333549.post-56933438444897817852010-09-10T21:39:00.000-07:002010-09-10T21:39:41.461-07:00Programmatically adding views to fit view not workingI was attempting to modify my sliders so that I add the programmatically to fit the available space. In doing so I was taking an input view and calculating the size that the 4 bars should be to fill up the available space. I was running into a problem that they weren't displaying correctly. The bottom bar was always running over.<br />
<br />
The weird thing was that all the logging showed that it was working. I finally figured out that the navigation bar at the top seemed to be messing me up. I was calculating my view sizes prior to pushing them onto the navigation stack, and that was compressing the data.<br />
<br />
The code that calculates and populates my slider where viewToUse is a parameter:<br />
this is done with the loadView command:<br />
<br />
<br />
<div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #37047f;">NSLog</span>(<span style="color: #ba2e1d;">@"view height %f, width %f x %f, y %f"</span>,viewToUse.<span style="color: #662ea7;">frame</span>.<span style="color: #662ea7;">size</span>.<span style="color: #662ea7;">height</span>,viewToUse.<span style="color: #662ea7;">frame</span>.<span style="color: #662ea7;">size</span>.<span style="color: #662ea7;">width</span>,viewToUse.<span style="color: #662ea7;">frame</span>.<span style="color: #662ea7;">origin</span>.<span style="color: #662ea7;">x</span>,viewToUse.<span style="color: #662ea7;">frame</span>.<span style="color: #662ea7;">origin</span>.<span style="color: #662ea7;">y</span>);</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"> </div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #a513a0;">int</span> sliderHeight=(viewToUse.<span style="color: #662ea7;">frame</span>.<span style="color: #662ea7;">size</span>.<span style="color: #662ea7;">height</span>)/<span style="color: #2600d5;">4</span>-<span style="color: #5e8087;">spacing</span>;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"> </div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #37047f;">NSLog</span>(<span style="color: #ba2e1d;">@"SliderHeight %d"</span>,sliderHeight);</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"> </div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #a513a0;">for</span> (<span style="color: #a513a0;">int</span> i=<span style="color: #2600d5;">0</span>;i<<span style="color: #2600d5;">4</span>;i++)</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> {</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #662ea7;">CGRect</span> labelFrame=<span style="color: #37047f;">CGRectMake</span>(<span style="color: #2600d5;">0</span>, (sliderHeight+<span style="color: #5e8087;">spacing</span>)*i+sliderHeight/<span style="color: #2600d5;">2</span>-labelHeight/<span style="color: #2600d5;">2</span>, labelWidth, labelHeight);</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #662ea7;">UILabel</span> *label=[[<span style="color: #662ea7;">UILabel</span> <span style="color: #37047f;">alloc</span>] <span style="color: #37047f;">initWithFrame</span>:labelFrame];</div><div style="color: #662ea7; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> label.</span>adjustsFontSizeToFitWidth<span style="color: black;">=</span><span style="color: #6f4a2c;">true</span><span style="color: black;">;</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> label.<span style="color: #662ea7;">text</span>=[sliderLabels <span style="color: #37047f;">objectAtIndex</span>:i];</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> [viewToUse <span style="color: #37047f;">addSubview</span>:label];</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #662ea7;">CGRect</span> areaFrame=<span style="color: #37047f;">CGRectMake</span>(<span style="color: #5e8087;">leftMargin</span>+labelWidth, (sliderHeight+<span style="color: #5e8087;">spacing</span>)*i,viewToUse.<span style="color: #662ea7;">bounds</span>.<span style="color: #662ea7;">size</span>.<span style="color: #662ea7;">width</span>-<span style="color: #5e8087;">leftMargin</span>-<span style="color: #5e8087;">rightMargin</span>-labelWidth, sliderHeight);</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"> </div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #5e8087;">sliders</span>[i]=[[<span style="color: #662ea7;">UISlider</span> <span style="color: #37047f;">alloc</span>]<span style="color: #37047f;">init</span>];</div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> [</span><span style="color: #5e8087;">sliders</span><span style="color: black;">[i] </span>addTarget<span style="color: black;">:</span><span style="color: #a513a0;">self</span><span style="color: black;"> </span>action<span style="color: black;">:</span><span style="color: #a513a0;">@selector</span><span style="color: black;">(sliderChanged:) </span>forControlEvents<span style="color: black;">:</span>UIControlEventValueChanged<span style="color: black;">];</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #5e8087;">sliders</span>[i].<span style="color: #662ea7;">tag</span>=i; </div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> sliders[i].accessibilityLabel=[sliderLabels objectAtIndex:i];</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #37047f;">NSLog</span>(<span style="color: #ba2e1d;">@"Y is %d"</span>,(sliderHeight+<span style="color: #5e8087;">spacing</span>)*i);</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #5e8087;">sliders</span>[i].<span style="color: #662ea7;">frame</span>=areaFrame;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> [viewToUse <span style="color: #37047f;">addSubview</span>:<span style="color: #5e8087;">sliders</span>[i]];</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"> </div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> } </div><div><br />
</div><div><div>This precise code looks very different depending on how my view is initialized:</div></div><div><br />
</div><div>With a navigation bar:</div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVJdzNJvwYR_F_Csre0MY9XsmZzdikj4e6jEQYWTrynuJKFjNhKz2qU774jRlKH5cLGteIonaw3hDueSdn8to_o73b2Eesrk2FdH-1DMAmoTMNaSQXfG3SA-Qu7DM_bMv0JOBHyxHT7A/s1600/withnavbar.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVJdzNJvwYR_F_Csre0MY9XsmZzdikj4e6jEQYWTrynuJKFjNhKz2qU774jRlKH5cLGteIonaw3hDueSdn8to_o73b2Eesrk2FdH-1DMAmoTMNaSQXfG3SA-Qu7DM_bMv0JOBHyxHT7A/s320/withnavbar.png" width="214" /></a></div><div><br />
</div><div><br />
</div><div><div style="color: #ba2e1d; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> </span><span style="color: #37047f;">NSLog</span><span style="color: black;">(</span>@"Select color chosen"<span style="color: black;">);</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"> </div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span>[</span><span style="color: #a513a0;">self</span><span style="color: black;">.</span><span style="color: #662ea7;">textView</span><span style="color: black;"> </span>resignFirstResponder<span style="color: black;">];<span class="Apple-tab-span" style="white-space: pre;"> </span></span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #5e8087;">ColorEdit</span> *colorEditor=[[<span style="color: #5e8087;">ColorEdit</span> <span style="color: #37047f;">alloc</span>] <span style="color: #37047f;">init</span>];</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"> </div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> [<span style="color: #a513a0;">self</span>.<span style="color: #662ea7;">navController</span> <span style="color: #37047f;">pushViewController</span>:colorEditor animated:<span style="color: #6f4a2c;">true</span>];</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> [<span style="color: #a513a0;">self</span>.<span style="color: #662ea7;">navController</span> setNavigationBarHidden:<span style="color: #6f4a2c;">false</span> ];</div></div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div>And with this, (no navigation bar)</div><div><br />
</div><div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-style-span" style="color: black;"><br />
</span></div></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiTzm4V5oxlEwZlBGAFpjp11zvGxtwQPgk7OxVsTitN2kdmDhrceMPIxEM6ZMsiGngiRFmivFd56UML0ON12QjQHNy73mmcgA4LMikAvfyXBs3g3G3hR8yQhosMCGxKcTr-J8Z_76Ac5Q/s1600/nonavbar.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiTzm4V5oxlEwZlBGAFpjp11zvGxtwQPgk7OxVsTitN2kdmDhrceMPIxEM6ZMsiGngiRFmivFd56UML0ON12QjQHNy73mmcgA4LMikAvfyXBs3g3G3hR8yQhosMCGxKcTr-J8Z_76Ac5Q/s320/nonavbar.png" width="212" /></a></div><div><div><div style="color: #37047f; font: normal normal normal 11px/normal Menlo; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span style="color: #a513a0;">self</span><span style="color: black;">.</span><span style="color: #662ea7;">textView</span><span style="color: black;"> </span>resignFirstResponder<span style="color: black;">];<span class="Apple-tab-span" style="white-space: pre;"> </span></span></div><div style="font: normal normal normal 11px/normal Menlo; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"> <span style="color: #5e8087;">ColorEdit</span> *colorEditor=[[<span style="color: #5e8087;">ColorEdit</span> <span style="color: #37047f;">alloc</span>] <span style="color: #37047f;">init</span>];</div><div style="font: normal normal normal 11px/normal Menlo; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-height: 13px;"> </div><div style="font: normal normal normal 11px/normal Menlo; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"> [<span style="color: #a513a0;">self</span>.<span style="color: #662ea7;">navController</span> <span style="color: #37047f;">pushViewController</span>:colorEditor animated:<span style="color: #6f4a2c;">true</span>];</div><div style="font: normal normal normal 11px/normal Menlo; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"> [<span style="color: #a513a0;">self</span>.<span style="color: #662ea7;">navController</span> setNavigationBarHidden:<span style="color: #6f4a2c;">true</span> ];</div></div></div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div>Finally I moved the initialization to another function instead of when the view loads and call this.</div><div><br />
</div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEik3tL3GuYT-DtyEelvs2IoUwI3tA8tV9oTjkSCeKjZN9pbedCBRvFHgWJLpJ6p7qfY3rT0ygiArUJBemQU1di15Co0UmcYftKlF1vDjCRfLm5r7pZ4FpH6w0XgNuvAritsZkqh4Mq2sA/s1600/navbarfix.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEik3tL3GuYT-DtyEelvs2IoUwI3tA8tV9oTjkSCeKjZN9pbedCBRvFHgWJLpJ6p7qfY3rT0ygiArUJBemQU1di15Co0UmcYftKlF1vDjCRfLm5r7pZ4FpH6w0XgNuvAritsZkqh4Mq2sA/s320/navbarfix.png" width="212" /></a></div><div><div style="color: #ba2e1d; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> </span><span style="color: #37047f;">NSLog</span><span style="color: black;">(</span>@"Select color chosen"<span style="color: black;">);</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"> </div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span>[</span><span style="color: #a513a0;">self</span><span style="color: black;">.</span><span style="color: #662ea7;">textView</span><span style="color: black;"> </span>resignFirstResponder<span style="color: black;">];<span class="Apple-tab-span" style="white-space: pre;"> </span></span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> <span style="color: #5e8087;">ColorEdit</span> *colorEditor=[[<span style="color: #5e8087;">ColorEdit</span> <span style="color: #37047f;">alloc</span>] <span style="color: #37047f;">init</span>];</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"> </div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> [<span style="color: #a513a0;">self</span>.<span style="color: #662ea7;">navController</span> <span style="color: #37047f;">pushViewController</span>:colorEditor animated:<span style="color: #6f4a2c;">true</span>];</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> [<span style="color: #a513a0;">self</span>.<span style="color: #662ea7;">navController</span> setNavigationBarHidden:<span style="color: #6f4a2c;">false</span> ];</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> [colorEditor <span style="color: #3e585d;">initColors</span>];</div></div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div>Note that this changes the size of the view, probably because it autoresizes then the navigation bar is added.</div><div>With the navigation bar it is 241 pixels high. Without the navigation bar it is 285 pixels high.</div><div><br />
</div><div><br />
</div><div>The lesson is that when programmatically filling in a view, pay attention to WHEN you do it. If you are going to have other items hanging around check to see if those are changing the heights of your views. This might throw off your results. This particular one cost me several hours of debugging, since I was sure my math was correct.</div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-686619119323333549.post-12233202205804224442010-09-03T21:13:00.000-07:002010-09-03T21:13:10.006-07:00Back from a long absenceI've been taking a break and trying to get back into iphone development. I decided my wall paper editor needed a navigation controller. This turned out nastier than I thought. XCode 4 has some problems with user interface editing, and I also forgot what I was doing.<br />
<br />
What I had to do to retrofit a navigation controller onto my existing project.<br />
<br />
<br />
<ol><li>Add a navigation controller to my main window</li>
<li>Add my main I was using as the view in the navigation controller. You have to set both the nib and the class to your class.</li>
<li>Add a navigation controller outlet to my application delegate.</li>
<li>Connect my navigation controller to that outlet.</li>
<li>Modify the main of the delegate to do the following:</li>
</ol><div><div style="color: #5e8087; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;">[</span>window<span style="color: black;"> </span><span style="color: #37047f;">addSubview</span><span style="color: black;">:</span>rootViewController<span style="color: black;">.</span><span style="color: #662ea7;">view</span><span style="color: black;">];</span></div></div><div><span style="color: black;"><br />
</span></div><div><span style="color: black;">where rootViewController is:</span></div><div><span style="color: black;"><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: #a513a0;">@property</span> (<span style="color: #a513a0;">nonatomic</span>, <span style="color: #a513a0;">retain</span>) <span style="color: #a513a0;">IBOutlet</span> UINavigationController *rootViewController;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-style-span" style="font-family: Times; font-size: medium;">I spent more time than I care to think doing this, I would miss one step or another and things would just not work. It was useful practice to wire the thing together, but it ate a lot of time.</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-style-span" style="font-family: Times; font-size: medium;"><br />
</span></div></span></div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-686619119323333549.post-63159174748742074372010-07-25T21:45:00.000-07:002010-07-25T21:45:36.560-07:00XCode 4I'm going to honor the NDA and not discuss details, however if you are a member of the XCode development program rush and download the beta version of XCode 4. It is a vast improvement over XCode 3 and seems to fix some of the major problems that I had with XCode 3, especially in comparison with Visual Studio and Eclipse. It's a separate install and won't trounce your XCode 3 install, and you can use both of them on your projects depending on your needs.<br />
<br />
I've only scratched the surface, and I already find it very nice.Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-686619119323333549.post-66914039073912235682010-07-25T20:42:00.001-07:002010-07-25T20:42:21.744-07:00-Release not found in protocolI had some delegates defined as id<protocolname> in my code, and have been ignoring the 'release not found in protocol' errors that I get on release (and retain). I finally decided to eliminate them, I added the NSObject protocol to my protocol definition. This removed this particular warning, which is good because I want to remove all warnings (if possible) from my code.</protocolname>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-686619119323333549.post-69463653233164731252010-07-19T20:59:00.000-07:002010-07-19T20:59:47.261-07:00Wall Paper Editor: Colored RGB SlidersI have a first cut of a pretty nice RGB slider. This will work similar to how the Apple slider works in interface builder for RGB. The color sliders indicate what you will get when you change the slider.<br />
<br />
This is a first cut, it uses a .nib file, which seems to sometimes be pesky when using a library. I would like to redo it to just do it with code.<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgxcs_kXXsF9M4e55-fpXDCca8tTzUSd1xXcRo09zcugrS-J_SxAdIF8w4_rq_4bvzD-f-aGw-SDm6N9Y6tmul88ipIQeNAMbgOuyhYWJjZcZ-gQkH6JYswaVAEZR2Wm0XjLIMBwuQd7w/s1600/Screen+shot+2010-07-18+at+11.11.45+PM.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="192" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgxcs_kXXsF9M4e55-fpXDCca8tTzUSd1xXcRo09zcugrS-J_SxAdIF8w4_rq_4bvzD-f-aGw-SDm6N9Y6tmul88ipIQeNAMbgOuyhYWJjZcZ-gQkH6JYswaVAEZR2Wm0XjLIMBwuQd7w/s200/Screen+shot+2010-07-18+at+11.11.45+PM.png" width="200" /></a></div>Here is the sample .nib file. I have a background label, and an image with an invisible label over it. I'm going to change the background color of the label as I move the text. The background label is just so you can see the transparency.<br />
<br />
I have 4 sliders for red, green, blue and Transparency. I also have a text field to display the normalized value of the field (0-255). As each slider is moved I will change the label image, and recalculate the images for the sliders.<br />
<br />
<br />
<br />
The code is still beta. It works for the principle but needs some cleaning up. Right now it has the following problems.<br />
<br />
<ol><li>When the view first comes up it doesn't display the colors.</li>
<li>You can't type a number into the text field.</li>
<li>I really want to support reorientation of the field, and larger sliders.</li>
</ol><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_6xn4Nz0KGbGS03DrntLLptO_j3pTwqptRjeGpEM3_uqVOOyrbz3oasQzAj1xUER-2IiQ0sKIhwV-cunDmpp2W5DmmCP_A2w7_nMf7cbF5iNkk0-b8aKWM7fDakh7PpNeZwswxFK6jA/s1600/IMG_0296.PNG"><img border="0" height="200" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_6xn4Nz0KGbGS03DrntLLptO_j3pTwqptRjeGpEM3_uqVOOyrbz3oasQzAj1xUER-2IiQ0sKIhwV-cunDmpp2W5DmmCP_A2w7_nMf7cbF5iNkk0-b8aKWM7fDakh7PpNeZwswxFK6jA/s200/IMG_0296.PNG" width="133" /> </a><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvGKcL-kcoQ4mqzXEp_BwlS7jVEf9RkTTg0WP80kfVa4cr8Wd3CoHhnt2zy62ZJwL2G9VXpksIhs9C0pGaaUB6hjpTNjNpS8U58Pb9085bKoqb1WEO_6-lrVqJQ27yY69Z_0rXGLiUFQ/s1600/IMG_0297.PNG"><img border="0" height="200" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvGKcL-kcoQ4mqzXEp_BwlS7jVEf9RkTTg0WP80kfVa4cr8Wd3CoHhnt2zy62ZJwL2G9VXpksIhs9C0pGaaUB6hjpTNjNpS8U58Pb9085bKoqb1WEO_6-lrVqJQ27yY69Z_0rXGLiUFQ/s200/IMG_0297.PNG" width="133" /></a><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiBrVppQrrHd0GtJFexlhhAs7XjBGlq22pbCHZnpPo_rAr_W8zm-UpRrjFuEOHHjsXjrBFCSHKV-sUwmcTV-CR5f-ULyvzFwW0_PDmPRfxqsK_wZ5isuYNYZPE9Ab0ytQ908CsOSgpS-w/s1600/IMG_0298.PNG"><img border="0" height="200" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiBrVppQrrHd0GtJFexlhhAs7XjBGlq22pbCHZnpPo_rAr_W8zm-UpRrjFuEOHHjsXjrBFCSHKV-sUwmcTV-CR5f-ULyvzFwW0_PDmPRfxqsK_wZ5isuYNYZPE9Ab0ytQ908CsOSgpS-w/s200/IMG_0298.PNG" width="133" /></a><br />
<br />
Above is the form running in three different selections. I display the current color up above, and for each slider I display a gradient showing what happens when you move that slider to the given location. This allows for a quick visual indicator of how to traverse the RGB color space.<br />
<br />
The key to this is the use of UISlider setMinimumTrackImage and setMaximumTrackImage. Both of these routines take an image and use it. So whenever the slider is moved I calculate a gradient for all 4 sliders of what moving that slider will do. I then set the min and max images for the slider. On my iPhone 3G this runs pretty nicely, although I personally have trouble distinguishing all the colors in the gradient. The screenshot looks better actually :)<br />
<br />
<br />
The key is that whenever a slider value is changed I go through and recalculate all 4 sliders. For each slider I assume all of the other color coordinates are going to remain constant, however that particular slider will vary from 0 to 1 (which is how CoreGraphics handles color values).<br />
<br />
I use the slider's position to determine where my two images start and begin, as well as what the value is at that point (which since the sliders go from 0 to 1 just like colors is pretty easy).<br />
<br />
My routine to calculate a slider takes the index of the slider and uses it.<br />
<br />
<div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">- (<span style="color: #a513a0;">void</span>) setSlider:(<span style="color: #662ea7;">UISlider</span>*) slide colorIndex:(<span style="color: #a513a0;">int</span>) colorIndex</div><br />
<br />
I have two arrays of floats for colors. lcolors and rcolors for the gradient on the left and right of the slider.<br />
Each array has 8 elements for Red, Green, Blue and Alpha for the start and end.<br />
<br />
<div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #a513a0;">float</span> lcolors[<span style="color: #2600d5;">8</span>];</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #a513a0;">float</span> rcolors[<span style="color: #2600d5;">8</span>];</div><div><br />
</div><br />
First I set all values equal to the current color. Only one color is varying. I could put a continue statement for the color I'm working with, but I just override it a little later.<br />
<br />
<div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: #a513a0;">for</span> (<span style="color: #a513a0;">int</span> i=<span style="color: #2600d5;">0</span>;i<<span style="color: #2600d5;">4</span>;i++)</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span>{</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span>lcolors[i]=<span style="color: #5e8087;">colors</span>[i];</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span>lcolors[i+<span style="color: #2600d5;">4</span>]=<span style="color: #5e8087;">colors</span>[i];</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span>rcolors[i]=<span style="color: #5e8087;">colors</span>[i];</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span>rcolors[i+<span style="color: #2600d5;">4</span>]=<span style="color: #5e8087;">colors</span>[i];<span class="Apple-tab-span" style="white-space: pre;"> </span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span>}</div><br />
<br />
Next I set the start and end colors for current color. I'm going to vary the left gradient from 0 to the currentValue, and the right gradient from the current value to 1.<br />
<br />
<br />
<div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: #a513a0;">float</span> currentColorValue=<span style="color: #5e8087;">colors</span>[colorIndex];</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">lcolors[colorIndex]=<span style="color: #2600d5;">0</span>;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span>lcolors[colorIndex+<span style="color: #2600d5;">4</span>]=currentColorValue;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span>rcolors[colorIndex]=currentColorValue;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span>rcolors[colorIndex+<span style="color: #2600d5;">4</span>]=<span style="color: #2600d5;">1.0</span>;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-style-span" style="font-family: Times;"><span class="Apple-style-span" style="font-size: medium;">Now I calculate the left and right gradient and build an image with them. The size of the image is the portion of the slider to the left/right of the current value. (Note that I don't need a image if I'm at the left or right end).</span></span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-style-span" style="font-family: Times;"><span class="Apple-style-span" style="font-size: medium;"><br />
</span></span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-style-span" style="font-family: Times;"><span class="Apple-style-span" style="font-size: medium;"></span></span></div><span class="Apple-style-span" style="font-family: Times;"><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: #a513a0;">float</span> middlePoint=slide.<span style="color: #662ea7;">bounds</span>.<span style="color: #662ea7;">size</span>.<span style="color: #662ea7;">width</span>*currentColorValue;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #662ea7;">CGRect</span> leftSize=<span style="color: #37047f;">CGRectMake</span>(<span style="color: #2600d5;">0</span>, <span style="color: #2600d5;">0</span>, middlePoint, slide.<span style="color: #662ea7;">bounds</span>.<span style="color: #662ea7;">size</span>.<span style="color: #662ea7;">height</span>);</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #662ea7;">CGRect</span> rightSize=<span style="color: #37047f;">CGRectMake</span>(<span style="color: #2600d5;">0</span>, <span style="color: #2600d5;">0</span>, slide.<span style="color: #662ea7;">bounds</span>.<span style="color: #662ea7;">size</span>.<span style="color: #662ea7;">width</span>-middlePoint, slide.<span style="color: #662ea7;">bounds</span>.<span style="color: #662ea7;">size</span>.<span style="color: #662ea7;">height</span>);</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #a513a0;">if</span> (leftSize.<span style="color: #662ea7;">size</span>.<span style="color: #662ea7;">width</span>><span style="color: #2600d5;">1</span>)</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span>{</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #662ea7;">UIImage</span> *lImage=[<span style="color: #a513a0;">self</span> <span style="color: #3e585d;">createImageWithGradient</span>:lgradient <span style="color: #3e585d;">withSize</span>:leftSize];</div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span>[slide </span>setMinimumTrackImage<span style="color: black;">:lImage </span>forState<span style="color: black;">:</span><span style="color: #2600d5;">0</span><span style="color: black;">];</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span>}</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #a513a0;">if</span> (rightSize.<span style="color: #662ea7;">size</span>.<span style="color: #662ea7;">width</span>><span style="color: #2600d5;">1</span>)</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span>{</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #662ea7;">UIImage</span> *rImage=[<span style="color: #a513a0;">self</span> <span style="color: #3e585d;">createImageWithGradient</span>:rgradient <span style="color: #3e585d;">withSize</span>:rightSize];</div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span>[slide </span>setMaximumTrackImage<span style="color: black;">:rImage </span>forState<span style="color: black;">:</span><span style="color: #2600d5;">0</span><span style="color: black;"> ];</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span>}</div><div><br />
</div><div><br />
</div><div>Where createImageWithGradient is:</div><div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">- (<span style="color: #662ea7;">UIImage</span>*) createImageWithGradient :(<span style="color: #662ea7;">CGGradientRef</span>) gradient withSize: (<span style="color: #662ea7;">CGRect</span>)size</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">{</div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span>UIGraphicsBeginImageContext<span style="color: black;">(size.</span><span style="color: #662ea7;">size</span><span style="color: black;">);</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #662ea7;">CGPoint</span> start=<span style="color: #37047f;">CGPointMake</span>(size.<span style="color: #662ea7;">origin</span>.<span style="color: #662ea7;">x</span>,size.<span style="color: #662ea7;">origin</span>.<span style="color: #662ea7;">y</span>+size.<span style="color: #662ea7;">size</span>.<span style="color: #662ea7;">height</span>*<span style="color: #2600d5;">0.25</span>);</div><div style="color: #662ea7; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span>CGPoint<span style="color: black;"> end=</span><span style="color: #37047f;">CGPointMake</span><span style="color: black;">(size.</span>origin<span style="color: black;">.</span>x<span style="color: black;">+size.</span>size<span style="color: black;">.</span>width<span style="color: black;">,size.</span>origin<span style="color: black;">.</span>y<span style="color: black;">+size.</span>size<span style="color: black;">.</span>height<span style="color: black;">*</span><span style="color: #2600d5;">0.25</span><span style="color: black;">);</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span></div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span>CGContextDrawLinearGradient<span style="color: black;">(</span>UIGraphicsGetCurrentContext<span style="color: black;">(), gradient, start, end, </span><span style="color: #2600d5;">0</span><span style="color: black;">);</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span></div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #662ea7;">UIImage</span><span style="color: black;"> *viewImage=</span>UIGraphicsGetImageFromCurrentImageContext<span style="color: black;">();</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span></div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span>UIGraphicsEndImageContext<span style="color: black;">();</span></div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #a513a0;">return</span><span style="color: black;"> [viewImage </span>stretchableImageWithLeftCapWidth<span style="color: black;">:</span><span style="color: #2600d5;">1</span><span style="color: black;"> </span>topCapHeight<span style="color: black;">:</span><span style="color: #2600d5;">1</span><span style="color: black;">];</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">}</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><br />
</div></div></span><br />
<br />
<div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-style-span" style="font-family: Times; font-size: medium;">Finally release the gradients:</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-style-span" style="font-family: Times; font-size: medium;"><br />
</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-style-span" style="font-family: Times; font-size: medium;"></span></div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span>CGGradientRelease<span style="color: black;">(lgradient);</span></div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span>CGGradientRelease<span style="color: black;">(rgradient);</span></div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><br />
</span></div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-style-span" style="font-family: Times; font-size: medium;">Everytime I move the slider, I calculate all the colors and fill in the text boxes:</span></span></div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-style-span" style="font-family: Times; font-size: medium;"></span></span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">- (<span style="color: #a513a0;">void</span>) calcColors</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">{</div><div style="color: #3e585d; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span>[</span><span style="color: #a513a0;">self</span><span style="color: black;"> </span>setSlider<span style="color: black;">:</span><span style="color: #5e8087;">redSlider</span><span style="color: black;"> </span>colorIndex<span style="color: black;">:</span><span style="color: #2600d5;">0</span><span style="color: black;">];</span></div><div style="color: #3e585d; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span>[</span><span style="color: #a513a0;">self</span><span style="color: black;"> </span>setSlider<span style="color: black;">:</span><span style="color: #5e8087;">greenSlider</span><span style="color: black;"> </span>colorIndex<span style="color: black;">:</span><span style="color: #2600d5;">1</span><span style="color: black;">];</span></div><div style="color: #3e585d; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span>[</span><span style="color: #a513a0;">self</span><span style="color: black;"> </span>setSlider<span style="color: black;">:</span><span style="color: #5e8087;">blueSlider</span><span style="color: black;"> </span>colorIndex<span style="color: black;">:</span><span style="color: #2600d5;">2</span><span style="color: black;">];</span></div><div style="color: #3e585d; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span>[</span><span style="color: #a513a0;">self</span><span style="color: black;"> </span>setSlider<span style="color: black;">:</span><span style="color: #5e8087;">transSlider</span><span style="color: black;"> </span>colorIndex<span style="color: black;">:</span><span style="color: #2600d5;">3</span><span style="color: black;">];</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #662ea7;">UIColor</span> *currentColor=[<span style="color: #662ea7;">UIColor</span> <span style="color: #37047f;">colorWithRed</span>:<span style="color: #5e8087;">colors</span>[<span style="color: #2600d5;">0</span>] <span style="color: #37047f;">green</span>:<span style="color: #5e8087;">colors</span>[<span style="color: #2600d5;">1</span>] <span style="color: #37047f;">blue</span>:<span style="color: #5e8087;">colors</span>[<span style="color: #2600d5;">2</span>] <span style="color: #37047f;">alpha</span>:<span style="color: #5e8087;">colors</span>[<span style="color: #2600d5;">3</span>]];</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #a513a0;">self</span>.<span style="color: #5e8087;">refLabel</span>.<span style="color: #662ea7;">backgroundColor</span>=currentColor;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #a513a0;">self</span>.<span style="color: #5e8087;">redTextField</span>.<span style="color: #662ea7;">text</span>=[<span style="color: #662ea7;">NSString</span> <span style="color: #37047f;">stringWithFormat</span>:<span style="color: #ba2e1d;">@"%d"</span>,(<span style="color: #a513a0;">int</span>) (<span style="color: #5e8087;">colors</span>[<span style="color: #2600d5;">0</span>]*<span style="color: #2600d5;">255</span>)];</div><div style="color: #5e8087; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #a513a0;">self</span><span style="color: black;">.</span>greenTextField<span style="color: black;">.</span><span style="color: #662ea7;">text</span><span style="color: black;">=[</span><span style="color: #662ea7;">NSString</span><span style="color: black;"> </span><span style="color: #37047f;">stringWithFormat</span><span style="color: black;">:</span><span style="color: #ba2e1d;">@"%d"</span><span style="color: black;">,(</span><span style="color: #a513a0;">int</span><span style="color: black;">) ( </span>colors<span style="color: black;">[</span><span style="color: #2600d5;">1</span><span style="color: black;">]*</span><span style="color: #2600d5;">255</span><span style="color: black;">)];</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #a513a0;">self</span>.<span style="color: #5e8087;">blueTextField</span>.<span style="color: #662ea7;">text</span>=[<span style="color: #662ea7;">NSString</span> <span style="color: #37047f;">stringWithFormat</span>:<span style="color: #ba2e1d;">@"%d"</span>,(<span style="color: #a513a0;">int</span>) (<span style="color: #5e8087;">colors</span>[<span style="color: #2600d5;">2</span>]*<span style="color: #2600d5;">255</span>)];</div><div style="color: #5e8087; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #a513a0;">self</span><span style="color: black;">.</span>transTextField<span style="color: black;">.</span><span style="color: #662ea7;">text</span><span style="color: black;">=[</span><span style="color: #662ea7;">NSString</span><span style="color: black;"> </span><span style="color: #37047f;">stringWithFormat</span><span style="color: black;">:</span><span style="color: #ba2e1d;">@"%d"</span><span style="color: black;">,(</span><span style="color: #a513a0;">int</span><span style="color: black;">) ( </span>colors<span style="color: black;">[</span><span style="color: #2600d5;">3</span><span style="color: black;">]*</span><span style="color: #2600d5;">255</span><span style="color: black;">)];</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">}</div><div><br />
</div><br />
<div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-style-span" style="color: black; font-family: Times;"><span class="Apple-style-span" style="font-size: medium;">To make my actions easier, I set the tags of the sliders and text fields equal to the color index associated with them.</span></span></div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-style-span" style="color: black; font-family: Times;"><span class="Apple-style-span" style="font-size: medium;"><br />
</span></span></div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-style-span" style="color: black; font-family: Times;"><span class="Apple-style-span" style="font-size: medium;">For future use I want to migrate this from a view with a nib to a control that doesn't have a nib. It would make it easier to use in multiple projects. I also plan to expand it so that I can support selecting from a list of colors, and eventually from a spectrum map.</span></span></div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-style-span" style="color: black; font-family: Times;"><span class="Apple-style-span" style="font-size: medium;"><br />
</span></span></div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-style-span" style="color: black; font-family: Times;"><span class="Apple-style-span" style="font-size: medium;"><br />
</span></span></div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-style-span" style="color: black; font-family: Times;"><span class="Apple-style-span" style="font-size: medium;">Below is the entire class, feel free to use it for your own projects. I would appreciate a attribution (Creative Commons attribution license).</span></span></div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-style-span" style="color: black; font-family: Times;"><span class="Apple-style-span" style="font-size: medium;"><br />
</span></span></div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-style-span" style="color: black; font-family: Times;"><span class="Apple-style-span" style="font-size: medium;"><br />
</span></span></div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-style-span" style="color: black; font-family: Times;"><span class="Apple-style-span" style="font-size: medium;">The header</span></span></div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-style-span" style="color: black; font-family: Times;"><span class="Apple-style-span" style="font-size: medium;"></span></span></div><span class="Apple-style-span" style="color: black; font-family: Times;"><div style="color: #ba2e1d; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: #6f4a2c;">#import </span><uikit/uikit.h></uikit/uikit.h></div><div style="color: #ba2e1d; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: #6f4a2c;">#import </span><quartzcore/quartzcore.h></quartzcore/quartzcore.h></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: #a513a0;">@interface</span> ColorEditor : UIViewController {</div><div style="color: #5e8087; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #a513a0;">IBOutlet</span><span style="color: black;"> </span><span style="color: #662ea7;">UISlider</span><span style="color: black;"> *</span>redSlider<span style="color: black;">;</span></div><div style="color: #5e8087; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #a513a0;">IBOutlet</span><span style="color: black;"> </span><span style="color: #662ea7;">UISlider</span><span style="color: black;"> *</span>greenSlider<span style="color: black;">;</span></div><div style="color: #5e8087; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #a513a0;">IBOutlet</span><span style="color: black;"> </span><span style="color: #662ea7;">UISlider</span><span style="color: black;"> *</span>blueSlider<span style="color: black;">;</span></div><div style="color: #5e8087; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #a513a0;">IBOutlet</span><span style="color: black;"> </span><span style="color: #662ea7;">UISlider</span><span style="color: black;"> *</span>transSlider<span style="color: black;">;</span></div><div style="color: #a513a0; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span>IBOutlet<span style="color: black;"> </span><span style="color: #662ea7;">UILabel</span><span style="color: black;"> *</span><span style="color: #5e8087;">refLabel</span><span style="color: black;">;</span></div><div style="color: #5e8087; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #a513a0;">IBOutlet</span><span style="color: black;"> </span><span style="color: #662ea7;">UITextField</span><span style="color: black;"> *</span>redTextField<span style="color: black;">;</span></div><div style="color: #5e8087; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #a513a0;">IBOutlet</span><span style="color: black;"> </span><span style="color: #662ea7;">UITextField</span><span style="color: black;"> *</span>greenTextField<span style="color: black;">;</span></div><div style="color: #5e8087; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #a513a0;">IBOutlet</span><span style="color: black;"> </span><span style="color: #662ea7;">UITextField</span><span style="color: black;"> *</span>transTextField<span style="color: black;">;<span class="Apple-tab-span" style="white-space: pre;"> </span></span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #a513a0;">float</span> <span style="color: #5e8087;">colors</span>[<span style="color: #2600d5;">4</span>];</div><div style="color: #5e8087; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #662ea7;">CGColorSpaceRef</span><span style="color: black;"> </span>currentColorSpace<span style="color: black;">;</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">}</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">- (<span style="color: #a513a0;">id</span>) initWithColor:(<span style="color: #662ea7;">UIColor</span> *)color;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: #a513a0;">@property</span> (<span style="color: #a513a0;">nonatomic</span>,<span style="color: #a513a0;">retain</span>) UISlider *redSlider;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: #a513a0;">@property</span> (<span style="color: #a513a0;">nonatomic</span>,<span style="color: #a513a0;">retain</span>) UISlider *greenSlider;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: #a513a0;">@property</span> (<span style="color: #a513a0;">nonatomic</span>,<span style="color: #a513a0;">retain</span>) UISlider *blueSlider;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: #a513a0;">@property</span> (<span style="color: #a513a0;">nonatomic</span>,<span style="color: #a513a0;">retain</span>) UISlider *transSlider;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: #a513a0;">@property</span> (<span style="color: #a513a0;">nonatomic</span>) CGColorSpaceRef currentColorSpace;</div><div style="color: #a513a0; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">@property<span style="color: black;"> (</span>nonatomic<span style="color: black;">,</span>retain<span style="color: black;">) UILabel *refLabel;</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: #a513a0;">@property</span> (<span style="color: #a513a0;">nonatomic</span>,<span style="color: #a513a0;">retain</span>) UITextField *redTextField;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: #a513a0;">@property</span> (<span style="color: #a513a0;">nonatomic</span>,<span style="color: #a513a0;">retain</span>) UITextField *greenTextField;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: #a513a0;">@property</span> (<span style="color: #a513a0;">nonatomic</span>,<span style="color: #a513a0;">retain</span>) UITextField *blueTextField;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: #a513a0;">@property</span> (<span style="color: #a513a0;">nonatomic</span>,<span style="color: #a513a0;">retain</span>) UITextField *transTextField;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">- (<span style="color: #a513a0;">IBAction</span>) bob:(<span style="color: #a513a0;">id</span>) sender;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">- (<span style="color: #a513a0;">IBAction</span>) sliderChanged:(<span style="color: #a513a0;">id</span>) sender;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">- (<span style="color: #662ea7;">UIImage</span>*) createImageWithGradient :(<span style="color: #662ea7;">CGGradientRef</span>) gradient withSize: (<span style="color: #662ea7;">CGRect</span>)size;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">- (<span style="color: #a513a0;">void</span>) setSlider:(<span style="color: #662ea7;">UISlider</span>*) slide colorIndex:(<span style="color: #a513a0;">int</span>) colorIndex;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">- (<span style="color: #a513a0;">void</span>) calcColors;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="color: #a513a0; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">@end</div><div><br />
</div></span><br />
<div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-style-span" style="color: black; font-family: Times;"><span class="Apple-style-span" style="font-size: medium;"><br />
</span></span></div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-style-span" style="color: black; font-family: Times;"><span class="Apple-style-span" style="font-size: medium;">The source</span></span></div><br />
<br />
<br />
<br />
<div style="color: #418715; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="color: #ba2e1d; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: #6f4a2c;">#import </span>"ColorEditor.h"</div><div style="color: #ba2e1d; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: #6f4a2c;">#import </span><quartzcore/quartzcore.h></quartzcore/quartzcore.h></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="color: #a513a0; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">@implementation<span style="color: black;"> ColorEditor</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: #a513a0;">@synthesize</span> redSlider;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: #a513a0;">@synthesize</span> greenSlider;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: #a513a0;">@synthesize</span> blueSlider;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: #a513a0;">@synthesize</span> transSlider;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: #a513a0;">@synthesize</span> currentColorSpace;</div><div style="color: #a513a0; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">@synthesize<span style="color: black;"> refLabel;</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: #a513a0;">@synthesize</span> redTextField;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: #a513a0;">@synthesize</span> greenTextField;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: #a513a0;">@synthesize</span> blueTextField;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: #a513a0;">@synthesize</span> transTextField;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="color: #418715; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">/*</div><div style="color: #418715; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> // The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.</div><div style="color: #418715; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {</div><div style="color: #418715; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {</div><div style="color: #418715; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> // Custom initialization</div><div style="color: #418715; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> }</div><div style="color: #418715; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> return self;</div><div style="color: #418715; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">}</div><div style="color: #418715; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">*/</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="color: #418715; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">/*</div><div style="color: #418715; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.</div><div style="color: #418715; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">- (void)viewDidLoad {</div><div style="color: #418715; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> [super viewDidLoad];</div><div style="color: #418715; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">}</div><div style="color: #418715; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">*/</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="color: #418715; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">/*</div><div style="color: #418715; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">// Override to allow orientations other than the default portrait orientation.</div><div style="color: #418715; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {</div><div style="color: #418715; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> // Return YES for supported orientations</div><div style="color: #418715; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> return (interfaceOrientation == UIInterfaceOrientationPortrait);</div><div style="color: #418715; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">}</div><div style="color: #418715; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">*/</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">- (<span style="color: #a513a0;">void</span>)didReceiveMemoryWarning {</div><div style="color: #418715; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> </span>// Releases the view if it doesn't have a superview.</div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> [</span><span style="color: #a513a0;">super</span><span style="color: black;"> </span>didReceiveMemoryWarning<span style="color: black;">];</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"> </div><div style="color: #418715; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> </span>// Release any cached data, images, etc that aren't in use.</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">}</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">- (<span style="color: #a513a0;">void</span>)viewDidUnload {</div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> [</span><span style="color: #a513a0;">super</span><span style="color: black;"> </span>viewDidUnload<span style="color: black;">];</span></div><div style="color: #418715; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> </span>// Release any retained subviews of the main view.</div><div style="color: #418715; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"> </span>// e.g. self.myOutlet = nil;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">}</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">- (<span style="color: #a513a0;">void</span>)dealloc {</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"> [<span style="color: #a513a0;">super</span> <span style="color: #37047f;">dealloc</span>];</div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span>CGColorSpaceRelease<span style="color: black;">(</span><span style="color: #5e8087;">currentColorSpace</span><span style="color: black;">);</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">}</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">- (<span style="color: #a513a0;">void</span>) setSlider:(<span style="color: #662ea7;">UISlider</span>*) slide colorIndex:(<span style="color: #a513a0;">int</span>) colorIndex</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">{</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #a513a0;">float</span> currentColorValue=<span style="color: #5e8087;">colors</span>[colorIndex];</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #a513a0;">float</span> lcolors[<span style="color: #2600d5;">8</span>];</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #a513a0;">float</span> rcolors[<span style="color: #2600d5;">8</span>];</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #a513a0;">for</span> (<span style="color: #a513a0;">int</span> i=<span style="color: #2600d5;">0</span>;i<<span style="color: #2600d5;">4</span>;i++)</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span>{</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span>lcolors[i]=<span style="color: #5e8087;">colors</span>[i];</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span>lcolors[i+<span style="color: #2600d5;">4</span>]=<span style="color: #5e8087;">colors</span>[i];</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span>rcolors[i]=<span style="color: #5e8087;">colors</span>[i];</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span>rcolors[i+<span style="color: #2600d5;">4</span>]=<span style="color: #5e8087;">colors</span>[i];<span class="Apple-tab-span" style="white-space: pre;"> </span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span>}</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span>lcolors[colorIndex]=<span style="color: #2600d5;">0</span>;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span>lcolors[colorIndex+<span style="color: #2600d5;">4</span>]=currentColorValue;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span>rcolors[colorIndex]=currentColorValue;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span>rcolors[colorIndex+<span style="color: #2600d5;">4</span>]=<span style="color: #2600d5;">1.0</span>;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span></div><div style="color: #418715; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span>// Now we have two gradients</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #662ea7;">CGGradientRef</span> lgradient = <span style="color: #37047f;">CGGradientCreateWithColorComponents</span>(<span style="color: #5e8087;">currentColorSpace</span>, lcolors, <span style="color: #a513a0;">NULL</span>, <span style="color: #a513a0;">sizeof</span>(lcolors)/(<span style="color: #a513a0;">sizeof</span>(lcolors[<span style="color: #2600d5;">0</span>])*<span style="color: #2600d5;">4</span>));</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #662ea7;">CGGradientRef</span> rgradient = <span style="color: #37047f;">CGGradientCreateWithColorComponents</span>(<span style="color: #5e8087;">currentColorSpace</span>, rcolors, <span style="color: #a513a0;">NULL</span>, <span style="color: #a513a0;">sizeof</span>(rcolors)/(<span style="color: #a513a0;">sizeof</span>(rcolors[<span style="color: #2600d5;">0</span>])*<span style="color: #2600d5;">4</span>));</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #a513a0;">float</span> middlePoint=slide.<span style="color: #662ea7;">bounds</span>.<span style="color: #662ea7;">size</span>.<span style="color: #662ea7;">width</span>*currentColorValue;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #662ea7;">CGRect</span> leftSize=<span style="color: #37047f;">CGRectMake</span>(<span style="color: #2600d5;">0</span>, <span style="color: #2600d5;">0</span>, middlePoint, slide.<span style="color: #662ea7;">bounds</span>.<span style="color: #662ea7;">size</span>.<span style="color: #662ea7;">height</span>);</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #662ea7;">CGRect</span> rightSize=<span style="color: #37047f;">CGRectMake</span>(<span style="color: #2600d5;">0</span>, <span style="color: #2600d5;">0</span>, slide.<span style="color: #662ea7;">bounds</span>.<span style="color: #662ea7;">size</span>.<span style="color: #662ea7;">width</span>-middlePoint, slide.<span style="color: #662ea7;">bounds</span>.<span style="color: #662ea7;">size</span>.<span style="color: #662ea7;">height</span>);</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #a513a0;">if</span> (leftSize.<span style="color: #662ea7;">size</span>.<span style="color: #662ea7;">width</span>><span style="color: #2600d5;">1</span>)</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span>{</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #662ea7;">UIImage</span> *lImage=[<span style="color: #a513a0;">self</span> <span style="color: #3e585d;">createImageWithGradient</span>:lgradient <span style="color: #3e585d;">withSize</span>:leftSize];</div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span>[slide </span>setMinimumTrackImage<span style="color: black;">:lImage </span>forState<span style="color: black;">:</span><span style="color: #2600d5;">0</span><span style="color: black;">];</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span>}</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #a513a0;">if</span> (rightSize.<span style="color: #662ea7;">size</span>.<span style="color: #662ea7;">width</span>><span style="color: #2600d5;">1</span>)</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span>{</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #662ea7;">UIImage</span> *rImage=[<span style="color: #a513a0;">self</span> <span style="color: #3e585d;">createImageWithGradient</span>:rgradient <span style="color: #3e585d;">withSize</span>:rightSize];</div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span>[slide </span>setMaximumTrackImage<span style="color: black;">:rImage </span>forState<span style="color: black;">:</span><span style="color: #2600d5;">0</span><span style="color: black;"> ];</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span>}</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span>CGGradientRelease<span style="color: black;">(lgradient);</span></div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span>CGGradientRelease<span style="color: black;">(rgradient);</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">}</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">- (<span style="color: #662ea7;">UIImage</span>*) createImageWithGradient :(<span style="color: #662ea7;">CGGradientRef</span>) gradient withSize: (<span style="color: #662ea7;">CGRect</span>)size</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">{</div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span>UIGraphicsBeginImageContext<span style="color: black;">(size.</span><span style="color: #662ea7;">size</span><span style="color: black;">);</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #662ea7;">CGPoint</span> start=<span style="color: #37047f;">CGPointMake</span>(size.<span style="color: #662ea7;">origin</span>.<span style="color: #662ea7;">x</span>,size.<span style="color: #662ea7;">origin</span>.<span style="color: #662ea7;">y</span>+size.<span style="color: #662ea7;">size</span>.<span style="color: #662ea7;">height</span>*<span style="color: #2600d5;">0.25</span>);</div><div style="color: #662ea7; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span>CGPoint<span style="color: black;"> end=</span><span style="color: #37047f;">CGPointMake</span><span style="color: black;">(size.</span>origin<span style="color: black;">.</span>x<span style="color: black;">+size.</span>size<span style="color: black;">.</span>width<span style="color: black;">,size.</span>origin<span style="color: black;">.</span>y<span style="color: black;">+size.</span>size<span style="color: black;">.</span>height<span style="color: black;">*</span><span style="color: #2600d5;">0.25</span><span style="color: black;">);</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span></div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span>CGContextDrawLinearGradient<span style="color: black;">(</span>UIGraphicsGetCurrentContext<span style="color: black;">(), gradient, start, end, </span><span style="color: #2600d5;">0</span><span style="color: black;">);</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span></div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #662ea7;">UIImage</span><span style="color: black;"> *viewImage=</span>UIGraphicsGetImageFromCurrentImageContext<span style="color: black;">();</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span></div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span>UIGraphicsEndImageContext<span style="color: black;">();</span></div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #a513a0;">return</span><span style="color: black;"> [viewImage </span>stretchableImageWithLeftCapWidth<span style="color: black;">:</span><span style="color: #2600d5;">1</span><span style="color: black;"> </span>topCapHeight<span style="color: black;">:</span><span style="color: #2600d5;">1</span><span style="color: black;">];</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">}</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">- (<span style="color: #a513a0;">id</span>) initWithColor:(<span style="color: #662ea7;">UIColor</span> *)color</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">{</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="color: #37047f; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #5e8087;">currentColorSpace</span><span style="color: black;"> = </span>CGColorSpaceCreateDeviceRGB<span style="color: black;">();</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #a513a0;">const</span> <span style="color: #662ea7;">CGFloat</span> *components = <span style="color: #37047f;">CGColorGetComponents</span>(color.<span style="color: #662ea7;">CGColor</span>);</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #a513a0;">for</span> (<span style="color: #a513a0;">int</span> i=<span style="color: #2600d5;">0</span>;i<<span style="color: #2600d5;">4</span>;i++)</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span>{</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #5e8087;">colors</span>[i]=components[i];</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span>}</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span></div><div style="color: #418715; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span>//[self calcColors];</div><div style="color: #a513a0; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span>return<span style="color: black;"> </span>self<span style="color: black;">;</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">}</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">- (<span style="color: #a513a0;">void</span>) calcColors</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">{</div><div style="color: #3e585d; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span>[</span><span style="color: #a513a0;">self</span><span style="color: black;"> </span>setSlider<span style="color: black;">:</span><span style="color: #5e8087;">redSlider</span><span style="color: black;"> </span>colorIndex<span style="color: black;">:</span><span style="color: #2600d5;">0</span><span style="color: black;">];</span></div><div style="color: #3e585d; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span>[</span><span style="color: #a513a0;">self</span><span style="color: black;"> </span>setSlider<span style="color: black;">:</span><span style="color: #5e8087;">greenSlider</span><span style="color: black;"> </span>colorIndex<span style="color: black;">:</span><span style="color: #2600d5;">1</span><span style="color: black;">];</span></div><div style="color: #3e585d; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span>[</span><span style="color: #a513a0;">self</span><span style="color: black;"> </span>setSlider<span style="color: black;">:</span><span style="color: #5e8087;">blueSlider</span><span style="color: black;"> </span>colorIndex<span style="color: black;">:</span><span style="color: #2600d5;">2</span><span style="color: black;">];</span></div><div style="color: #3e585d; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span>[</span><span style="color: #a513a0;">self</span><span style="color: black;"> </span>setSlider<span style="color: black;">:</span><span style="color: #5e8087;">transSlider</span><span style="color: black;"> </span>colorIndex<span style="color: black;">:</span><span style="color: #2600d5;">3</span><span style="color: black;">];</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #662ea7;">UIColor</span> *currentColor=[<span style="color: #662ea7;">UIColor</span> <span style="color: #37047f;">colorWithRed</span>:<span style="color: #5e8087;">colors</span>[<span style="color: #2600d5;">0</span>] <span style="color: #37047f;">green</span>:<span style="color: #5e8087;">colors</span>[<span style="color: #2600d5;">1</span>] <span style="color: #37047f;">blue</span>:<span style="color: #5e8087;">colors</span>[<span style="color: #2600d5;">2</span>] <span style="color: #37047f;">alpha</span>:<span style="color: #5e8087;">colors</span>[<span style="color: #2600d5;">3</span>]];</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #a513a0;">self</span>.<span style="color: #5e8087;">refLabel</span>.<span style="color: #662ea7;">backgroundColor</span>=currentColor;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #a513a0;">self</span>.<span style="color: #5e8087;">redTextField</span>.<span style="color: #662ea7;">text</span>=[<span style="color: #662ea7;">NSString</span> <span style="color: #37047f;">stringWithFormat</span>:<span style="color: #ba2e1d;">@"%d"</span>,(<span style="color: #a513a0;">int</span>) (<span style="color: #5e8087;">colors</span>[<span style="color: #2600d5;">0</span>]*<span style="color: #2600d5;">255</span>)];</div><div style="color: #5e8087; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #a513a0;">self</span><span style="color: black;">.</span>greenTextField<span style="color: black;">.</span><span style="color: #662ea7;">text</span><span style="color: black;">=[</span><span style="color: #662ea7;">NSString</span><span style="color: black;"> </span><span style="color: #37047f;">stringWithFormat</span><span style="color: black;">:</span><span style="color: #ba2e1d;">@"%d"</span><span style="color: black;">,(</span><span style="color: #a513a0;">int</span><span style="color: black;">) ( </span>colors<span style="color: black;">[</span><span style="color: #2600d5;">1</span><span style="color: black;">]*</span><span style="color: #2600d5;">255</span><span style="color: black;">)];</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #a513a0;">self</span>.<span style="color: #5e8087;">blueTextField</span>.<span style="color: #662ea7;">text</span>=[<span style="color: #662ea7;">NSString</span> <span style="color: #37047f;">stringWithFormat</span>:<span style="color: #ba2e1d;">@"%d"</span>,(<span style="color: #a513a0;">int</span>) (<span style="color: #5e8087;">colors</span>[<span style="color: #2600d5;">2</span>]*<span style="color: #2600d5;">255</span>)];</div><div style="color: #5e8087; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #a513a0;">self</span><span style="color: black;">.</span>transTextField<span style="color: black;">.</span><span style="color: #662ea7;">text</span><span style="color: black;">=[</span><span style="color: #662ea7;">NSString</span><span style="color: black;"> </span><span style="color: #37047f;">stringWithFormat</span><span style="color: black;">:</span><span style="color: #ba2e1d;">@"%d"</span><span style="color: black;">,(</span><span style="color: #a513a0;">int</span><span style="color: black;">) ( </span>colors<span style="color: black;">[</span><span style="color: #2600d5;">3</span><span style="color: black;">]*</span><span style="color: #2600d5;">255</span><span style="color: black;">)];</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">}</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">- (<span style="color: #a513a0;">IBAction</span>) sliderChanged:(<span style="color: #a513a0;">id</span>) sender</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">{</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #662ea7;">UISlider</span> *slider=(<span style="color: #662ea7;">UISlider</span>*) sender;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #a513a0;">int</span> colorIndex=slider.<span style="color: #662ea7;">tag</span>;</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #5e8087;">colors</span>[colorIndex]=slider.<span style="color: #662ea7;">value</span>;</div><div style="color: #3e585d; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;"><span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span>[</span><span style="color: #a513a0;">self</span><span style="color: black;"> </span>calcColors<span style="color: black;">];</span></div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">}</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">- (<span style="color: #a513a0;">IBAction</span>) bob:(<span style="color: #a513a0;">id</span>)sender</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">{</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">}</div><div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;"><br />
</div><div style="color: #a513a0; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">@end</div><div><br />
</div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-686619119323333549.post-41214616924943306362010-07-15T17:24:00.000-07:002010-07-15T17:24:12.930-07:00Otter Box case for IPadI have an Otterbox case for my iPhone and love it, it's very protective and feels nice. I signed up for a mailing list for when OtterBox releases an iPad version. Well <a href="http://www.otterbox.com/ipad-cases/">they have finally done so</a>. <br />
<br />
I purchased the defender case, and received it in the mail a couple days ago. The free shipping was very fast, it took 3 days to get to my house.<br />
<br />
First impressions:<br />
<br />
What you get is a film you can apply to your screen, a 2 layer case that covers most of the iPad, and a back that can either serve as a stand, or a greater protective cover to your screen. It also includes a large screen cleaning cloth, and a plastic piece to help attach the film to the screen.<br />
<br />
The case feels very solid, and I think it will be very impact resistant. If you have the back cover over the screen it provides an air gap that should protect your screen against direct impact. The other sides are covered with rubber over a hard plastic case.<br />
<br />
There is a back to the case that can be removed. This allows access to the power outlet and to be used as a docking station. <br />
<br />
<br />
Pros:<br />
Very protective<br />
When the case is put on the front of the iPad it should allow you to put it in any bag.<br />
The back can be used as a stand for your iPad.<br />
Cons:<br />
<br />
The you need to remove the small piece in the back to charge it. I would have liked a rubber plug such as my iPhone has for charging.<br />
It's a little heavy.<br />
<br />
<br />
Pictures will be forthcoming.Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-686619119323333549.post-15974314654090254562010-07-11T21:30:00.000-07:002010-07-11T21:30:24.616-07:00Wallpaper Editor: Color slidebarOK, I could just put up a standard slide bar and let the user set RGB values, but that is not too great for general users, and even developers can have troubles visualizing the RGB color space. Taking a look in interface builder, I see that IT's color selector is extremely nice. Lets steal some of its design. (All of its design would be too much, it packs a lot of functionality).<br />
<br />
The sliders it has are very nice. As you change the value of one slider, the other sliders change their gradient so that they show the possible values:<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhy6TCFSUkaAR0mDy66pKniVMBKxm2PD50S1XKJ5L1Z0_q2Usfu6sp6oBamX-YjIqAFePzvdNbCw-dRK0EdxqWh9ziqRPkG6wC7060duaSYL-uoCK5ka1rArxN9-28lIjuwtSMFxw_C9Q/s1600/Screen+shot+2010-07-11+at+11.14.23+PM.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="150" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhy6TCFSUkaAR0mDy66pKniVMBKxm2PD50S1XKJ5L1Z0_q2Usfu6sp6oBamX-YjIqAFePzvdNbCw-dRK0EdxqWh9ziqRPkG6wC7060duaSYL-uoCK5ka1rArxN9-28lIjuwtSMFxw_C9Q/s200/Screen+shot+2010-07-11+at+11.14.23+PM.png" width="200" /></a></div>The iphone editor has 4 different types of sliders RGB, grey scale, CMYK, HSB. In addition it has the ability to select from an image, to select 'crayons', to select a list of names, etc...<br />
<br />
The opacity button allows the Alpha to be set. I was originally going to call mine 'transparency', but Opacity works as well. Either way using Alpha is something only computer geeks will use. As the alpha is set, a combination of black and white is displayed behind color sample.<br />
<br />
<br />
I plan to implement RGB sliders in a similar fashion and maybe a list of names for my initial cut. If I do it right I should be able to support the other color spaces. Right now I think giving too many color spaces might confuse people. <br />
<br />
The first implementation is to create a single slide bar that takes an object that helps set the color gradient for that slider. <br />
<br />
My sliders will not have the ability to specify a full 256 values for color. The iphone's UI doesn't really let this happen unless I force the sliders to be landscape, and get picky about colors. However I should be able to support increments of 10, and to possibly have a 'zoom down' mode where you get to the general vicinity and then choose a specific number. <br />
<br />
If you collapse the Apple dialog then it will not allow you to choose every number.<br />
<br />
Note that for my final version I might want to do HSV instead of RGB, it might be easier for the general user to find a color using that model.Unknownnoreply@blogger.com0