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

<channel>
	<title>Science At Hand LLC</title>
	<atom:link href="http://www.scienceathand.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.scienceathand.com</link>
	<description>Mobile Software with a scientific bent.</description>
	<lastBuildDate>Sat, 24 Nov 2012 19:41:51 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
		<item>
		<title>A New Year</title>
		<link>http://www.scienceathand.com/idevblogaday/a-new-year/</link>
		<comments>http://www.scienceathand.com/idevblogaday/a-new-year/#comments</comments>
		<pubDate>Mon, 23 Jan 2012 02:19:08 +0000</pubDate>
		<dc:creator>todd</dc:creator>
				<category><![CDATA[iDevBlogADay]]></category>

		<guid isPermaLink="false">http://www.scienceathand.com/?p=219</guid>
		<description><![CDATA[It&#8217;s a new year, and like lots of people, I made resolutions about many things, including spending time updating this blog. Then life interfered and I&#8217;m behind already&#8211; Life has a penchant for getting in the way. So to give &#8230; <a href="http://www.scienceathand.com/idevblogaday/a-new-year/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s a new year, and like lots of people, I made resolutions about many things, including spending time updating this blog. Then life interfered and I&#8217;m behind already&#8211; Life has a penchant for getting in the way. So to give it a quick start, I&#8217;ll share a useful bit of code hacking.</p>
<p>Recently, my friend wanted to add print statements to every function for a debug build. This was early in the development process, and it seemed that quick and dirty would work as well as anything. After struggling with it for a while he dashed off a quick &#8220;ah ha&#8221; email. His results:</p>
<div><span style="font-family: Monaco;">search expression</span></div>
<div><span style="font-family: Monaco;"><br />
</span></div>
<div>^([-](.*)([\r]?)([\n]?)[{])</div>
<div><span style="font-family: Monaco;"><br />
</span></div>
<div><span style="font-family: Monaco;">replace expression</span></div>
<div><span style="font-family: Monaco;"><br />
</span></div>
<div><span style="font-family: Monaco;">\1 \n\tBBTrace;</span></div>
<p>I wouldn&#8217;t rely on this for final code but it is useful for a quick debug.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.scienceathand.com/idevblogaday/a-new-year/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Custom Build Phases and Scripts</title>
		<link>http://www.scienceathand.com/idevblogaday/custom-build-phases-and-scripts/</link>
		<comments>http://www.scienceathand.com/idevblogaday/custom-build-phases-and-scripts/#comments</comments>
		<pubDate>Tue, 13 Dec 2011 19:46:04 +0000</pubDate>
		<dc:creator>todd</dc:creator>
				<category><![CDATA[iDevBlogADay]]></category>

		<guid isPermaLink="false">http://www.scienceathand.com/?p=192</guid>
		<description><![CDATA[I&#8217;m a fan of unit testing. It has saved me from much embarrassment during my career. But I find tests tedious to write and often worry that they pass not because the results are correct but because the tests are &#8230; <a href="http://www.scienceathand.com/idevblogaday/custom-build-phases-and-scripts/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m a fan of unit testing. It has saved me from much embarrassment during my career. But I find tests tedious to write and often worry that they pass not because the results are correct but because the tests are wrong. Which is long way of saying that I&#8217;m also a huge fan of compilers. A critical portion of my testing is writing code that forces the compiler to verify my code. And if I can get that with minimal work, so much the better.</p>
<p>So one bug, that I manage to repeat just about every project, is errors with the constants that reference resources. I get the names of PNG files, tags and identifiers wrong all the time. Or I change the name in the resource, even something simple like capitalization. Fortunately with enough testing these errors come to light. But I&#8217;ve also solved this problem before. And in a way that uses the compiler to test for some form of correctness. The simple solution is to create a process that preprocesses the resources and creates a source file with all the constants.</p>
<p>But how to drive the preprocess? Xcode custom build phases and custom build scripts are nearly ideal for this type of work and they&#8217;re easy to set up. Just select the project root in the navigator, then the target and finally either the build phase or build rules.</p>
<p><a href="http://www.scienceathand.com/wp-content/uploads/2011/12/Build-Phases.png"><img class="aligncenter size-full wp-image-202" title="Build Phases" src="http://www.scienceathand.com/wp-content/uploads/2011/12/Build-Phases.png" alt="" width="1001" height="620" /></a></p>
<p>This is the Build Phases Panel. Build Phase scripts run once for every compile. I use these for output that is affected by many files, such as a list of all PNGs. In the bottom right corner is the Add Build Phase picker. Select &#8220;Add Run Script&#8221; to add a new Build Phase script. The ordering of phases can be easily changed by dragging the Run Script title bar. Make sure that this comes before the compile sources because the compile is going to use the results of this output.</p>
<p>I generally find it useful to have the majority of the script in a separate file. That way I can easily reuse the functionality. Its also helpful while debugging the script to be able to run it directly from the command line.</p>
<p>Those actions that are more specific to the project I tend to not place in the script file. The &#8216;cd&#8217; and &#8216;cp&#8217; in the illustration above are examples of this. Even as I write this I&#8217;m not fully convinced that this is the best solution. The nice thing is that if I change the script and it doesn&#8217;t work the compiler will tell me something is wrong. To me thats testing early and testing often.</p>
<p><a href="http://www.scienceathand.com/wp-content/uploads/2011/12/Build-Rules.png"><img class="aligncenter size-full wp-image-195" title="Build Rules" src="http://www.scienceathand.com/wp-content/uploads/2011/12/Build-Rules.png" alt="" width="900" height="532" /></a></p>
<p>This is the Build Rules Panel. The Add Build Rule is a simple button. Build Rules are run once for each file, and the Process menu can be used to select which type of file it is run with. You could replicate the functionality of a Build Rule with a Build Phase that walks the project hierarchy. The advantage of a Build Rule is that it is sensitive to changes to the input file and will only process files that have an actual change. It also ensures that the input files are actually in the project.</p>
<p>The Build Phase script that I&#8217;ve included in this project creates a list of defines for the PNG files. Its written in Ruby. I&#8217;m not a Ruby programmer so it may be a bit rough.</p>
<pre class="brush: ruby; title: ; notranslate">
#! /usr/bin/env ruby

dest = File.open(ARGV[0],File::CREAT|File::WRONLY,0777)

Dir.foreach(&quot;.&quot;) { |file|
    expression =  /.png$/
    if expression.match file #filename ends with .png
        name = File.basename(file,&quot;.png&quot;)
        dest.puts &quot;#define k_png_&quot;+ name + &quot; @\&quot;&quot; + name + &quot;\&quot;&quot;
    end
}

dest.close
</pre>
<p>and its output is</p>
<pre class="brush: objc; title: ; notranslate">
#define k_png_glyphicons_002_dog @&quot;glyphicons_002_dog&quot;
#define k_png_glyphicons_031_bus @&quot;glyphicons_031_bus&quot;
#define k_png_glyphicons_042_group @&quot;glyphicons_042_group&quot;
#define k_png_glyphicons_070_umbrella @&quot;glyphicons_070_umbrella&quot;
#define k_png_glyphicons_132_inbox_minus @&quot;glyphicons_132_inbox_minus&quot;
#define k_png_glyphicons_143_database_ban @&quot;glyphicons_143_database_ban&quot;
#define k_png_glyphicons_158_playlist @&quot;glyphicons_158_playlist&quot;
#define k_png_glyphicons_193_circle_ok @&quot;glyphicons_193_circle_ok&quot;
#define k_png_glyphicons_264_fishes @&quot;glyphicons_264_fishes&quot;
#define k_png_glyphicons_304_vimeo @&quot;glyphicons_304_vimeo&quot;
</pre>
<p>Your could perform all sorts of remapping of the of the characters, such as all caps or camel case and remove the underscores, but I prefer that generated items be as easily identified with their source as possible and so do a minimal amount of processing.</p>
<p>The Build Script script uses an xml parser to extract the attributes of various elements and again uses a very simple define for the mapping.</p>
<pre class="brush: ruby; title: ; notranslate">
#! /usr/bin/env ruby

require &quot;rexml/document&quot;
dest = File.open(ARGV[1],File::CREAT|File::WRONLY,0777)
src = REXML::Document.new File.open(ARGV[0])

src.elements.each(&quot;//@storyboardIdentifier&quot;).each do
    |element|
    if element.value != &quot;&quot;
        dest.puts &quot;#define k_storyboard&quot;+ element.value + &quot; @\&quot;&quot; + element.value + &quot;\&quot;&quot;
    end
end

src.elements.each(&quot;//@reuseIdentifier&quot;).each do
    |element|
    if element.value != &quot;&quot;
        dest.puts &quot;#define k_reuseIdentifier&quot;+ element.value + &quot; @\&quot;&quot; + element.value + &quot;\&quot;&quot;
    end
end

src.elements.each(&quot;//@tag&quot;).each do
    |element|
    if element.value != &quot;&quot;
        dest.puts &quot;#define k_tag&quot;+ element.value + &quot; @\&quot;&quot; + element.value + &quot;\&quot;&quot;
    end
end

dest.close
</pre>
<p>and its output is</p>
<pre class="brush: objc; title: ; notranslate">
#define k_storyboardMasterObject @&quot;MasterObject&quot;
#define k_storyboardTableController2 @&quot;TableController2&quot;
#define k_reuseIdentifierTitle_1 @&quot;Title_1&quot;
#define k_reuseIdentifierCell_Type_2 @&quot;Cell_Type_2&quot;
#define k_tag12345 @&quot;12345&quot;
</pre>
<p>The final step in both of these is to copy the files produced back to a location that can be referenced from your normal source code. By convention I create a directory to hold all the generated files. This makes it really easy to identify them so you don&#8217;t do things like edit by hand.</p>
<p>This technique doesn&#8217;t solve all errors. You can still reference the wrong file, tag or identifier, rather that a nonexistent one. But it does prevent the most common error with little effort. And any help, however minor, adds to my satisfaction with the result.</p>
<p>Theres a demo project <a href="https://github.com/toddwbates/Script-Demo" target="_blank">here</a>. I&#8217;m still refining the scripts and maybe next time I&#8217;ll include type checked code rather than just pound defines. Wouldn&#8217;t it be cool to be able to write [[CodeGenerator shared] loadPNG_foo] and know that it will work.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.scienceathand.com/idevblogaday/custom-build-phases-and-scripts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adventures in UISplitViewController</title>
		<link>http://www.scienceathand.com/idevblogaday/adventures-in-uisplitviewcontroller-2/</link>
		<comments>http://www.scienceathand.com/idevblogaday/adventures-in-uisplitviewcontroller-2/#comments</comments>
		<pubDate>Mon, 28 Nov 2011 04:48:41 +0000</pubDate>
		<dc:creator>todd</dc:creator>
				<category><![CDATA[iDevBlogADay]]></category>
		<category><![CDATA[idevblogaday]]></category>

		<guid isPermaLink="false">http://www.scienceathand.com/?p=88</guid>
		<description><![CDATA[I love sample code. Its a reassuring proof of the documentation and make it really easy to copy and paste. With fewer keystrokes I make fewer mistakes. So as a payment to the karma of all the code that I&#8217;ve &#8230; <a href="http://www.scienceathand.com/idevblogaday/adventures-in-uisplitviewcontroller-2/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I love sample code. Its a reassuring proof of the documentation and make it really easy to copy and paste. With fewer keystrokes I make fewer mistakes. So as a payment to the karma of all the code that I&#8217;ve pored over Im contributing sample code as well.</p>
<p>Recently, I finished an application that used a UITabViewController to manage multiple master controllers. The starting point was <a href="http://developer.apple.com/library/ios/#samplecode/MultipleDetailViews/Introduction/Intro.html">MultipleDetailViews</a>. Its a great starting point but doesn&#8217;t leverage the new Storyboard paradigm. I&#8217;m going to re-implement this sample to iOS 5.</p>
<p>The UISplitViewController manages two view controllers, a master controller and a detail controller. The master controller manages the left portion in landscape mode and the popover while in portrait mode. The detail view controller is always visible.</p>
<p>This demo extends that by using a Tab view controller to manage multiple master controllers and swapping in detail controller associated with the active master controller.</p>
<p><a href="http://www.scienceathand.com/wp-content/uploads/2011/11/Master-11.jpg"><img class="aligncenter size-medium wp-image-183" title="Master-1" src="http://www.scienceathand.com/wp-content/uploads/2011/11/Master-11-226x300.jpg" alt="" width="226" height="300" /></a></p>
<p style="text-align: center;"><a href="http://www.scienceathand.com/wp-content/uploads/2011/11/Master-1.tiff"><br />
</a></p>
<h2>Step 1: Create the Project</h2>
<p>Open up Xcode and from the main menu choose File\New\New Project. Choose the iOS\Application\Master-Detail Application template, and click Next. Name the product MultipleMasterDetailViews, select iPad for the Device family, and make sure just the Use Automatic Reference Counting checkbox is checked, click Next and save the project by clicking Create.</p>
<p><a href="http://www.scienceathand.com/wp-content/uploads/2011/11/New-Project-Options11.png"><img class="aligncenter size-medium wp-image-184" title="New-Project-Options1" src="http://www.scienceathand.com/wp-content/uploads/2011/11/New-Project-Options11-300x202.png" alt="" width="300" height="202" /></a></p>
<h2>Step 2: Modify the Storyboard</h2>
<p>The UISplitViewController contains UINavigationController contains MDMasterViewController. This project inserts a UITabBarController as a container between the UISplitViewContoller and the UINavigationController. Do this by dragging a UITabBarController onto the storyboard and option drag from the UISplitViewController to the UITabBarController. Select Relationship-masterViewController when prompted. Delete the default UIViewControllers from the UITabBarController and add the UINavigationController/MDMasterViewController as a tab. Duplicate the UINavigationController/MDMasterViewController and this second one as a tab as well. We&#8217;re reusing the MDMasterViewController for demonstration purposes only. In practice they might be different classes.</p>
<p>The final modification to the storyboard is to add another scene for the second detail view. Using the current UINavigationController/MDDetailViewController as a prototype, copy and paste a new scene into the Storyboard. This new scene needs an identifier because it is loaded using instantiateViewControllerWithIdentifier:(NSString*).</p>
<p><a href="http://www.scienceathand.com/wp-content/uploads/2011/11/detail-ident.png"><img class="aligncenter size-medium wp-image-188" title="detail ident" src="http://www.scienceathand.com/wp-content/uploads/2011/11/detail-ident-300x247.png" alt="" width="300" height="247" /></a>Set the identifier property of the UINavigationController to &#8220;Detail 2 Root&#8221; (I wish that Xcode would automatically create a header file to automate this fragile mapping, hint, hint).</p>
<p>The Storyboard layout should now be similar to this.<a href="http://www.scienceathand.com/wp-content/uploads/2011/11/Storyboard1.jpg"><img class="aligncenter size-medium wp-image-185" title="Storyboard" src="http://www.scienceathand.com/wp-content/uploads/2011/11/Storyboard1-300x266.jpg" alt="" width="300" height="266" /></a></p>
<p style="text-align: center;"><a href="http://www.scienceathand.com/wp-content/uploads/2011/11/Storyboard.tiff"><img class="aligncenter size-full wp-image-128" title="Storyboard" src="http://www.scienceathand.com/wp-content/uploads/2011/11/Storyboard.tiff" alt="" width="0" height="0" /></a></p>
<h2>Step 3: Master Detail Manager</h2>
<p>The Apple sample code implements the UISplitViewControllerDelegate in the detail view controller. I&#8217;m using the Composition pattern to implement this functionality for increased reusability. Create a new class that implements the UISplitViewControllerDelegate and  UITabBarDelegate. This implementation assumes that all the DetailViewControllers are imbedded in a UINavigationController and that the first item  of the UISplitVeiwController.viewControllers is a UITabBarController. This initWithSplitViewController: updates the delegate properties of both the splitViewController and the master controller (the first controller) to self. The array that&#8217;s provided must also have the same number of detail controllers as controllers in the tabBarController.viewControllers.</p>
<pre class="brush: objc; title: ; notranslate">
@interface MDMultipleMasterDetailManager
           : NSObject

-(id)initWithSplitViewController:(UISplitViewController*)splitViewController
       withDetailRootControllers:(NSArray*)detailControllers;

@end
</pre>
<p>The implementation of initWithSplitController is relatively straight forward.</p>
<pre class="brush: objc; title: ; notranslate">
@implementation MDMultipleMasterDetailManager

@synthesize splitViewController = __splitViewController;
@synthesize detailControllers = __detailControllers;
@synthesize masterBarButtonItem = __masterBarButtonItem;
@synthesize masterPopoverController = __masterPopoverController;
@synthesize currentDetailController = __currentDetailController;

-(id)initWithSplitViewController:(UISplitViewController*)splitViewController withDetailRootControllers:(NSArray*)detailControllers
{
    self = [super init];
    if(self){
        __splitViewController = splitViewController;
        __detailControllers = [detailControllers copy];
        UINavigationController* detailRoot = [splitViewController.viewControllers objectAtIndex:1];
        __currentDetailController = detailRoot.topViewController;

        splitViewController.delegate = self;
        UITabBarController* tabBar = [splitViewController.viewControllers objectAtIndex:0];
        tabBar.delegate = self;
    }

    return self;
}
</pre>
<p>The UISplitViewControllerDelagate methods have to manage the currently active detail controller and maintain enough state to update other detail controllers if they become active. This implementation modifies the left button of the detail controllers directly.</p>
<pre class="brush: objc; title: ; notranslate">
/* forward the message to the current detail view
 */
-(void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)pc
{
    self.masterBarButtonItem = barButtonItem;
    self.masterPopoverController = pc;

    barButtonItem.title = NSLocalizedString(@&quot;Master&quot;, @&quot;Master&quot;);

    [self.currentDetailController.navigationItem setLeftBarButtonItem:self.masterBarButtonItem animated:YES];
}

/* forward the message to the current detail view
 * all detail views must implement UISplitViewControllerDelegate
 */
-(void)splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
    self.masterBarButtonItem = nil;
    self.masterPopoverController = nil;

    [self.currentDetailController.navigationItem setLeftBarButtonItem:nil animated:YES];

}
</pre>
<p>The UITabBarControllerDelegate just swaps the button and popover from the previous detail controller to the detail controller associated with the currently selected master controller.<br />
According the the UIPopoverController documentation the navigation bar is automatically included in the passthrough views. As the detail controllers are exchanged, the passthrough views of the popover need to be updated as well.</p>
<pre class="brush: objc; title: ; notranslate">
// change detail view to reflect the current master controller
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    UINavigationController* detailRootController = [self.detailControllers objectAtIndex:tabBarController.selectedIndex];
    UIViewController* detailControler = detailRootController.topViewController;

    if(detailControler != self.currentDetailController)
    {
        [self.currentDetailController.navigationItem setLeftBarButtonItem:nil animated:NO];
        self.currentDetailController = detailControler;

        UIViewController* tabBarController = [self.splitViewController.viewControllers objectAtIndex:0];

        self.splitViewController.viewControllers = [NSArray arrayWithObjects:tabBarController,detailRootController, nil];

        // replace the passthrough views with current detail navigationbar
        if([self.masterPopoverController isPopoverVisible]){
            self.masterPopoverController.passthroughViews = [NSArray arrayWithObject:detailRootController.navigationBar];
        }
    }
}
</pre>
<p>Finally, the application delegate needs to be modified to wire this all up. Because we&#8217;ve used the delegate pattern, it&#8217;s only a couple of lines of code in didFinishLaunchingWithOptions.</p>
<pre class="brush: objc; title: ; notranslate">
@interface MDAppDelegate()
@property (strong,nonatomic)MDMultipleMasterDetailManager* masterDetailManager;
@end

@implementation MDAppDelegate

@synthesize window = _window;
@synthesize masterDetailManager = __masterDetailManager;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;

    UIViewController* detail1 = [splitViewController.viewControllers objectAtIndex:1];
    UIViewController* detail2 = [splitViewController.storyboard instantiateViewControllerWithIdentifier:@&quot;Detail 2 Root&quot;];

    self.masterDetailManager = [[MDMultipleMasterDetailManager alloc] initWithSplitViewController:splitViewController
                                withDetailRootControllers:[NSArray arrayWithObjects:detail1,detail2,nil]];

    return YES;
}
</pre>
<h2>And the Link to the GIT Repository</h2>
<p>Here&#8217;s a <a href="https://github.com/toddwbates/MultipleMasterDetailViews">link</a> to public GIT repository project if you want to look at a working example. I hope you find it useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.scienceathand.com/idevblogaday/adventures-in-uisplitviewcontroller-2/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>A Modest Begining</title>
		<link>http://www.scienceathand.com/uncategorized/44/</link>
		<comments>http://www.scienceathand.com/uncategorized/44/#comments</comments>
		<pubDate>Thu, 21 Jul 2011 12:53:18 +0000</pubDate>
		<dc:creator>todd</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.scienceathand.com/?p=44</guid>
		<description><![CDATA[Science at Hand,LLC, produces applications with a scientific bent. I have a few ideas in mind that run a range of interests; what ties them together is that they all have to do with science, particularly physics. Quick Scope is &#8230; <a href="http://www.scienceathand.com/uncategorized/44/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Science at Hand,LLC, produces applications with a scientific bent. I have a few ideas in mind that run a range of interests; what ties them together is that they all have to do with science, particularly physics. Quick Scope is the first application I&#8217;ve created entirely on my own. As it stands, it&#8217;s the most obviously science oriented. It is, after all, a view of a data acquisition system. I have a list of improvements for it, including adding support for the microphone. I still view Quick Scope&#8217;s main emphasis as sharing, but the microphone would allow the app to be useful even without. We&#8217;ll see how it goes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.scienceathand.com/uncategorized/44/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
