Archive for May, 2009

How to dismiss the keyboard in iPhone

May 27, 2009
  • In the Interface Builder add the text field to the view and use the inspector to change the Text Input Traits so that the Return Key is set to Done (see thumbnail).Text Input Traits
  • In the .h file for your view controller, define an IBAction method to dismiss the keyboard, e.g.
    - (IBAction)dismissKeyboard:(id)sender;
  • In the .m file define the method:
    - (IBAction)dismissKeyboard:(id)sender {
    	[sender resignFirstResponder];
    }
    
  • In the Interface Builder, control click on the text field object, and connect the “Did End On Exit” event to the view controller’s File’s Owner object.

You can use the same method to dismiss the keyboard for all of the text fields in the view.

This technique no longer works in the latest version of IB (v3.2.1). Instead, you’ll need to connect the event “Did End of Exit” to the “First Responder” object in the “MainWindow.xib” and then select the “dismissKeyboard” method that you just introduced.

Another bike route to top of St. Joseph Hill

May 26, 2009

At 6:30 AM this morning, our bike club tried yet another route to the summit of St. Joseph Hill in Los Gatos. The 6.44 miles ride took one hour and 10 minutes.

Water laws and water use in USA

May 25, 2009

Interesting lecture on the laws of water use in USA by Prof. Glennon given at the UC Berkeley California Colloquium on Water. Some of the facts cited in this talk:

  • Groundwater use for domestic purposes alone 8 BGD (Billion Gallons per Day) in 1965 to 19.7 BGD in 2000.
  • Farmers use 2/3 of all groundwater pumped.
  • Total groundwater use in 2000: 30 trillion gallons
  • Groundwater makes up 25% of the nation’s water supply
  • 1/2 population relies on groundwater for drinking water

But these numbers are quite out of date. During the past decade people have turned to groundwater to supplement the short supply of water caused by draughts. This means that much more water must have been pumped from new and existing groundwater sources than in 2000 (the last year for which there is data from the US Geological survey).

Surface water law. If you own property on a lake or river, then you’ve water rights. This is called the Riparian Water Rights (which has its origin in English common law). It is a shared water rights (all land owners that are on the river or lake share the water rights). In the western USA, a different law is in place: Prior Appropriation. In this system, water rights are unconnected to land ownership. The first person to use a quantity of water for a beneficial use (agricultural, industrial or household use), has the right to use that quantity of water for that purpose. Each water right has a yearly quantity and an appropriation date.

Groundwater law: Rights of Capture & Reasonable Use. The problem with the groundwater law capture and reasonable use is that it allows for over-drafting or mining the resource to the extend of exhausting the supply. Basically anything you can with groundwater is considered reasonable.

San Joaquin Valley subsidence

San Joaquin Valley subsidence

These are the basic laws for water. One set of rules for surface water and one set for groundwater. Think of a ground water as a glass of water and each groundwater user having a straw. The groundwater laws allows for unlimited supply of straws. This leads to exploitation of groundwater aquifers.

Water is measured in acre-foot. That is how much water it takes to cover an acre of land to the depth of one foot with water. An acre water is 325,000 gallons, and weights 1358 tons. So water is really heavy and it takes a lot of energy to pump it out of the aquifers. Pumping water out of a 500 feet well will require many kilo watts of energy (multi $K per month).

Problems with over-drafting:

  • The water quality is lower the deeper the well.
  • Salt-water intrusion.
  • Subsidence. The land collapse when groundwater is pumped too quickly without giving the aquifers times to naturally replenish (rate of 2 inches per year).

Groundwater moves laterally and it actually provides water to the rivers.

3 hour bike ride in Los Gatos

May 23, 2009

This morning at 6:30 AM Jim and I went for a bike ride that started in Los Gatos, over Kennedy, to Hicks, to Quicksliver park, back to Hicks, then Shannon, and finally back to Los Gatos, The ride took over 3 hours. Here is the route.

AT&T network registration every 54 minutes

May 22, 2009

The Telit modem on the AT&T network will re-register every 54 minutes (if the modem is stationary, for a moving modem the re-registration will happen more frequently). For a GPRS session, you’ll lose your IP address assignment after 4 hours of inactivity. These are the properties of the network and have nothing to do with the make of the modem.

SERVINFO, AT&T

May 22, 2009

In US, AT&T operates on two frequencies 850MHz and 1900 MHZ. With the Telit GM862 module, the 850MHz frequency has current consumption peaks of 2 Amps and on the 1900 MHz network, current peaks of 1 Amps. The T-mobile network operates on 1900 MHz. You can use the AT SERVINFO command to find out which network (850MHz or 1900 MHz) are you on. If the channel number is between 128 to 251, then you’re on the 850 MHz network and for 512 to 810 channel numbers, you’re on the 1900 MHz network.

SMS to e-mail?

May 21, 2009

I knew that you were able to send e-mail and get it as SMS, but I was not aware the reverse also existed. On AT&T you can send an e-mail via SMS by sending a message that starts with e-mail address followed by a space and then the text of the message to the magic number “+121″. I wonder what other magic numbers there are out there?

UIPickerView sample

May 20, 2009

UI to select a state from the picker to update the image/label. The arrow button controls the visibility of the picker.

UI to select a state from the picker to update the image/label. The arrow button controls the visibility of the picker.

There is no built-in drop down list in iPhone SDK. You’ll need to create the interaction from the combination of the Picker and some other control, e.g., a button. Here are the steps to create and initialize a UIPickerView for all the 50 States:

Start Interface Builder and drag a Picker View object from the Library tool to the View object. This will create a UIPickerView object for you. Alternatively you can programmatically create this object in Xcode. But it is easier to use the IB. If you now build and run your project, you wont see the picker. To see it you’ll need to set the data source and delegate.

In IB, control-click on the Picker and then drag and connect the dataSource and delegate outlets to the File’s Owner for the view controller. Now if you build and run your application, you’ll see an empty picker. The control-click on the picker is a short cut for connecting the data source and the delegate properties. In addition, you’ll need to connect the picker to your view controller to create the object so that you can access it in Xcode. You do this by dragging the New Referencing Outlet from the IB’s Picker VIew Connection to the File’s Owner object in the xib file view. Alternatively, when you control-click, you can drag a New Referencing Outlet from the popup.
(more…)

XML view plugin for Safari

May 20, 2009

To display XML responses in Safari look no further than the excellent plugin by Marc Liyanage.

Adding button and its handler in Objective-C using iPhone SDK

May 17, 2009

In JavaScript, it is fairly easy to add a button and its event handlers. HTML and the browser are your Interface Builder and your text editor is your Xcode. There is no need to graphically connect anything and there is no confusion where you add the button and where you add the event handlers. For iPhone programming, the model is more sophisticated and hence it is more complicated to nail the basics. So how do you add a simple button and its event handler in Objective-C and iPhone SDK?

Typically you will want to add a button using the Interface Builder and then add the button’s event handlers in Xcode. Here are the steps:

  1. In Xcode double click on the NIB file that defines the view to which you wish to add a button. This will start the Interface Builder (IB).
  2. In IB, open the Library tool and drag a button to the view. Position and resize this button and give it a title.
  3. Now you need to connect the button to the View Controller class so that you can control and customize this button from Xcode. You can control-click on the button and drag the connection line to the File’s Owner in the xib view (alternatively, you can start the IB’s Inspector, select the Button Connection tab and create a Referencing Outlet – drag the connection similar to before).
  4. In Xcode you’ll need to define the button to the view controller’s interface file and then define the methods to customize the button and define its events handlers.

In this sample, I’ve added a UILabel and a UIButton class so that when you click on the button the value of the label is changed.

@interface TestViewController : UIViewController {
	IBOutlet UILabel *mylabel;
	IBOutlet UIButton *mybutton;
}
@property (nonatomic, retain) UILabel *mylabel;
@property (nonatomic, retain) UIButton *mybutton;
@end

In the implementation file, you need “synthesize” the label and button objects.

@implementation TestViewController

@synthesize mylabel;
@synthesize mybutton;

Next I try to customize the label on the button and add an event handler.

- (void)viewDidLoad {
        [super viewDidLoad];
	mylabel.text = @"www.aquacue.com";
	[mybutton setTitle:@"Aquacue - Normal" forState: (UIControlState)UIControlStateNormal];
	[mybutton setTitle:@"Aquacue - Highlighted" forState: (UIControlState)UIControlStateHighlighted];
	[mybutton addTarget:self action:@selector(myButtonClick:) forControlEvents:(UIControlEvents)UIControlEventTouchDown];
}

Now here is the definition of the handler:

-  (void)myButtonClick:(id)sender {
	mylabel.text = @"Clicked";
}

Next I add another event handler so that when you click on the screen background, the value of the label is changed:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    mylabel.text = [NSString stringWithFormat: @"Hello Aquacue %@ %d", @"AAA", event.timestamp];
    [super touchesBegan:touches withEvent:event];
}

You can also use the IB to define event handlers (aka Actions). The easiest way is to actually define the handler/action in Xcode using the IBAction declaration in the interface file (add the declaration just before the @end statement).

- (IBAction)myButtonPressed:(id)sender;

Next you’ll need to define the myButtonPressed handler in the implementation file:

- (IBAction)myButtonPressed:(id)sender {
	mylabel.text = @"Change the label, when user clicks on the button";
}

Note that in this sample, I’ve defined two handlers one using the UIButton addTarget method and another using the IBAction. Both of these handlers are called when the user clicks on the button. As it turns it both of these handlers get called IBAction gets called first followed by the handler that was added via addTarget.

Only the methods defined with the IBAction declaration are exposed to the IB. You can control-click on the button object in IB and you’ll see a popup menu that defines all of the events that the object is willing to handle. Using the normal IB way, you’ll need to drag an event to the File’s Owner object to expose the event handler in Xcode.


Follow

Get every new post delivered to your Inbox.