<?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>Mac / iPhone App Development &#187; modal prompt</title>
	<atom:link href="http://benreeves.co.uk/tag/modal-prompt/feed/" rel="self" type="application/rss+xml" />
	<link>http://benreeves.co.uk</link>
	<description>Home of a Small Time Developer</description>
	<lastBuildDate>Wed, 26 May 2010 14:24:12 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>UIKit Modal Prompt</title>
		<link>http://benreeves.co.uk/uikit-modal-prompt/</link>
		<comments>http://benreeves.co.uk/uikit-modal-prompt/#comments</comments>
		<pubDate>Tue, 26 Jan 2010 19:20:05 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[modal prompt]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.benreeves.co.uk/?p=121</guid>
		<description><![CDATA[in order to not lock the ui iVersion runs it&#8217;s svn commands in a seperate threads. The promblem arises when one of thses threads needs to prompt for a value e.g. a username / password. Firstly UIKit does not provide a modal alert view and secondly UIKit should only be called from the mainThread.
First we [...]]]></description>
			<content:encoded><![CDATA[<p>in order to not lock the ui iVersion runs it&#8217;s svn commands in a seperate threads. The promblem arises when one of thses threads needs to prompt for a value e.g. a username / password. Firstly UIKit does not provide a modal alert view and secondly UIKit should only be called from the mainThread.</p>
<p>First we create a new class which has a view which will be loaded from a nib, a thread condition to lock the calling thread and two NSString fields to store the result and the string of the button which was clicked.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> MessagePrompt <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>
<span style="color: #002200;">&#123;</span>
&nbsp;
UIView <span style="color: #002200;">*</span> view;
<span style="color: #400080;">NSCondition</span> <span style="color: #002200;">*</span> promptCondition;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span> clickedButton Text;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span> result;
<span style="color: #002200;">&#123;</span>
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Our new prompt method can be called from the thread which needs to be locked, it will subsequently call UIKit on the main thread.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">+</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>newPrompt<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>MessagePrompt<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>prompt
<span style="color: #002200;">&#123;</span>
&nbsp;
     <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSThread</span> currentThread<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSThread</span> mainThread<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
          <span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;Should not be called by Main thread<span style="color: #2400d9;">\n</span>&quot;</span><span style="color: #002200;">&#41;</span>;
          <span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
     prompt.promptCondition <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSCondition</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>prompt performSelectorOnMainThread<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>prompt<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span>view waitUntilDone<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>prompt.promptCondition wait<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">return</span> prompt;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>This method is called by +(id)NewPrompt and displays a prompt in our window. It also adds targets to each UIButton to receive user actions.</p>
<p style="text-align: center;"><a href="http://www.benreeves.co.uk/wp-content/uploads/2010/01/screen-shot-2009-10-23-at-15.44.00.png"><img class="size-full wp-image-123  aligncenter" title="Prompt Nib Example" src="http://www.benreeves.co.uk/wp-content/uploads/2010/01/screen-shot-2009-10-23-at-15.44.00.png" alt="" width="380" height="269" /></a></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>prompt<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIView<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>inView
<span style="color: #002200;">&#123;</span>
     <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span> nib <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBundle</span> mainBundle<span style="color: #002200;">&#93;</span> loadNibNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;MyPromptView&quot;</span> owner<span style="color: #002200;">:</span>self options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
     inView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>nib objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>;	
&nbsp;
     <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span> subviews <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self.view subviews<span style="color: #002200;">&#93;</span>;
     <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span>UIView <span style="color: #002200;">*</span> cview <span style="color: #a61390;">in</span> subviews<span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
          <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>cview isKindOfClass<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIButton class<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
          <span style="color: #002200;">&#123;</span>
               UIButton <span style="color: #002200;">*</span> button <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>UIButton<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>cview;
                <span style="color: #002200;">&#91;</span>button addTarget<span style="color: #002200;">:</span>self action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>clicked<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> forControlEvents<span style="color: #002200;">:</span>UIControlEventTouchDown<span style="color: #002200;">&#93;</span>;
          <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #002200;">&#91;</span>inView.superview addSubview<span style="color: #002200;">:</span>view<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>This is called when a user clicks a button, it finds the value field and retreives and stores the value in self.message. The prompt condition is then signalled which causes the calling thread to continue running.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>clicked<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIButton<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sender
<span style="color: #002200;">&#123;</span>
     self.clickedButton <span style="color: #002200;">=</span> sender.currentTitle;
&nbsp;
     UITextView <span style="color: #002200;">*</span> text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>iVersionAppDelegate findSubViewOfKind<span style="color: #002200;">:</span>self.view class<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UITextView class<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
     <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>text<span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
         self.message <span style="color: #002200;">=</span> text.text;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #002200;">&#91;</span>self.view removeFromSuperview<span style="color: #002200;">&#93;</span>;
     self.view <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>promptCondition <span style="color: #a61390;">signal</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Usage</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">/* Called when svn requires a log message */</span>
svn_error_t <span style="color: #002200;">*</span>
iversion_svn_cl__get_log_message<span style="color: #002200;">&#40;</span><span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">**</span>log_msg,
<span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">**</span>tmp_file,
<span style="color: #a61390;">const</span> apr_array_header_t <span style="color: #002200;">*</span>commit_items,
<span style="color: #a61390;">void</span> <span style="color: #002200;">*</span>baton,
apr_pool_t <span style="color: #002200;">*</span>pool<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
&nbsp;
CommitLogMessagePrompt <span style="color: #002200;">*</span> prompt <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>CommitLogMessagePrompt newPrompt<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">/* Clicked Continue */</span>
     <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>prompt.clickedButton isEqualToString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Continue&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">*</span>log_msg <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>prompt.message UTF8String<span style="color: #002200;">&#93;</span>;
         <span style="color: #a61390;">return</span> SVN_NO_ERROR;
    <span style="color: #002200;">&#125;</span>
&nbsp;
     <span style="color: #a61390;">return</span> svn_error_create<span style="color: #002200;">&#40;</span>SVN_ERR_CANCELLED, <span style="color: #a61390;">NULL</span>, _<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;Delete Cancelled&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://benreeves.co.uk/uikit-modal-prompt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
