Saturday 24 July 2010

Tab Bars and Pickers

(Pickerview) or just a picker are the controls with the dials that spin. Pickers are more complex than other iPhone controls.
Pickers can be configured to display one dial or many, by default pickers display text, but many can also be made to display images.
Date picker is the easiest type of picker to implement.


Picker has very few attributes that can be configured from within the interface builder. Date picker is the only one that can be  grabbed from the interface builder. 


A picker needs to be provided with both a picker delegate and a picker data source. The picker defers several jobs to its delegate. The most important of these is the task of determining what to actually draw for each of the rows in each of its components.


The picker asks the delegate for either a string or a view that will be drawn at a given spot or a given component. The picker gets its data from the delegate. In additon to the delegate, a data source is required.  The data source tells the picker how many components it will be working with and how many rows make up each component.


Its very common for the data source and the delegate to be the same object and just as common for that object to be the view controller for the picker's endorsing view.


The data source isn't actually an object designed to hold data.


Root controller to manage swapping of applications other views. Tab bars use icons to represent each of the tabs. 24 x 24 pixel .png pics need to be added to the resources folder.
The root controller controls the very first view that the user will see when your program runs.
'Badge' in the 'Tab bar item' can be used to put a red icon onto a tab bar item, similar to red number placed on mail icon.
For tab bar controller first single click the first tab and use the atttributes inspector to specify nib name for that tab's associated view controller. Then open the identity inspector and change the underlying class of the view controller associated with the tab.
Click the tab again, instead of the view controller gave the tab bar item a title icon.










Root controller to manage the swapping of application's other views.
Tab bars use icons to represent each of the tabs. 24 x 24 pixel .png pics need to be added to the resources folder.


The root controller controls the very first view that the user will see when your program runs.


'Badge' in the 'Tab Bar Item' can be used to put a red icon onto a tab bar item, similar to red number placed on email icon.
For tab bar controller first single check the first tab and use the attributed inspector to specify a nib name for that tab's associated view controller. Then open the identity inspector and changed the underlying class of the view controller associated with the tab.
Click tab again, instead of the view controller gave the tab item a title and icon.


To implement the date picker a single outlet and a single action is required. The outlet will be used to grab the value from the date picker. The action will be triggered by a button and will throw up an alert to show the date value pulled from the picker.  









Date picker is relatively easy, creating a picker that holds values requires NSArray, as pickers don't hold values themselves. Instead they call methods on their data source and delegates to get the data they need to display. The picker doesn't care where the underlying data is. It asks for the data when it needs it, and the data source and delegate work together to supply that data. As a result, the data could be coming from a static list, or could be loaded from a file or URL, or even made up or calculated on the fly.


controller.h class can act as both the data source and delegate for it's picker. Unlike a date picker, a regular picker cannot tell us what data it holds, because it doesn't maintain the data. It hands the job off to the data source and the delegate. Instead, we have to ask the picker which now is selected and grab the corresponding data from our pickerDataArray.


Using NSInteger as opposed to int or long means that the compiler automatically chooses whichever size is best for the platforms on which the code is being compiled. It will create a 32 bit processor and a longer 64 bit long when compiling for a 64 bit architecture.


Usually data will come from a property file in projects resources folder. Any line of code that begins with #pragma is technically a compiler directive.  







For multi component pickers defining 2 constants will represent 2 components.
Components are assigned numbers, with the left most component being assigned zero and increasing by one each move to the right.  




Sunday 18 July 2010

Multiview Applications

Real Power of iPhone platform emerges when you can switch out views based on user input.
Simplest example of a multiview application is a utility app. A utility view focusses primarily on a single view but offers a second view that can be used to provide more detail than the primary view.
Several tab bar apps that ship with the iPhone.

A tab bar is used for selecting one and only one option from among two or more. A toolbar can hold buttons and certain other controls, but those items are not mutually exclusive.

Nearly all multiview applications use the same basic pattern. The nib file is a key player.
The root controller is the primary view controller for the application and as such is the view that specifies whether it is ok to automatically rotate to a new orientation, though the root controller can pass responsibility for things like that to the current active controller.

In multiview apps most of the screen will be taken up by a content view and each content view will have its own controller with its own outlets and actions.
In a tab bar application, for example tapping on the tab bar will go to the tab bar controller, but taps anywhere else on the screen will go to the controller that corresponds to the content view currently being displayed.

Each content view generally consists of up to three pieces: the view controller, the nib and a subclass of UIView.
Content view will always usually have an associated view controller, will usually have a nib and will sometimes have a subclass UIView.

One of the most daunting aspect of creating a multiview app from scratch is that several interconnected objects have to be created.
Toolbar buttons aren't like iPhone controls. They support only a single target action and the trigger that action only at one well defined moment, the equivelant of a touch up inside event on other iPhone controls.

Lazy loading is when the view is loaded the first time it is needed and is a standard way of keeping memory overhead down.
Lazy loading is a key component of resource management on the iPhone and should be implemented anywhere possible.

In a complex multiview application, being responsible and flushing unused objects from memory can be the difference between an application that works well and one that crashes periodically because it ran out of memory,

Autorotation and Autoresizing

Auto rotation might not be right for every application. Several apps support only a single orientation rotation.
If auto rotation enhances user experience add it to your app.
Auto rotation is specified in the view controller.
Most of the work in actually moving the pixels around the screen is managed by the iPhone OS. There are three approaches to auto rotation dependent upon the complexity of the interface. With simpler interfaces simply specify the auto resize attributes for all the objects that make up the interface.
More complex interfaces have to handle auto rotation in a different manner. One approach is to manually reposition the objects in the view when notified that the view is rotating.
The second approach is to actually design two different versions of the view in the interface builder, one for portrait mode and a seperate one for landscape mode.
In both cases need to override methods from UIViewController in view's controller class.
Add code to .m file, method already provided (commented out) called shouldAutoRotateToInterface need to uncomment and add code.
Four defined orientations in the way that the phone is held:
  • UIInterfaceOrientationPortrait
  • UIInterfaceOrientationUpsideDown
  • UIInterfaceOrientationLandscapeLeft
  • UIInterfaceOrientationLandscapeRight
When view changed this method called on active view controller. Possible for some views to support auto rotation but not others.
Upside down orientation is discouraged as the phone will remain that way if it rings. Most controls default to a setting where items on the screen stay where they are when rotated in relation to the left side and the top of the screen.






For larger items specify new positions when view is rotated to prevent overlapping. To change a control's attributes need an outlet to point to the object that needs to be changed.
The size and position of all views, including controls such as buttons are specified within a property called frame, which is a struct of type GRect. GRectMake is a function by apple that lets you easily create a GRect by specifying x and y positions along with the width and height.
GREct comes from core graphics framework.
Designing portrait and landscape view seperately and swapping in and out is a moderately complex action. Requires 2 completely distinct set of outlets one for each view. Cannot change the size of the default view.

Saturday 10 July 2010

More User Interface Fun (iPhone Dev)

Breaking the process of building a complex application into smaller chunks makes it less intimidating. User interface controls come in 3 basic forms: active, static (or inactive) and passive. 

Active Controls involves user participation and something happens (i.e push a button and it fires a method).
Static Control like a text label, user does not interact with it.
Passive Control is something that holds onto a value until a user needs it. Controls don't trigger action methods but the user can interact with them and change their values.

All iPhone controls are subclasses of UI Control. 

Because of multitouch interface, all iPhone controls can trigger multiple actions depending on how they are touched. User may trigger different actions with finger swipe then a touch.

Images to be used need to be added to the resources folder.
If images and label views have no user interactions then no need to declare actions.
Choosing any options that causes images to scale can add processing overhead.
Alpha slider any value <1 causes processing overhead as the image is transparent.
Text fields are one of the most complex controls on iPhone dev.

Keyboard is software based. To make it 'go away' need to add code to the controller class.
First responder is the control that the user is currently interacting with.

Changing UIView to UIControl gives the view the ability to trigger action methods.

Fairly common to bounce around xcode and interface builder.
Action sheets  and alerts are used to provide the user with feedback. They are used to force the user to make a choice between 2 or more items. They are displayed from the bottom of the screen and display a series of buttons for the user to select from. The user cannot continue until they have made a choice.

Alerts also enforce a response from the user before they can continue.

Delegation is a very common design pattern in cocoa touch. 

Action sheets always have a parent, which must be a view that is currently visible to the user.

viewDidUnload() method used for memory management, important to set all controller outlets to nil when the controller has been notified that the view has been unloaded.  

Handling Basic User Interaction (iPhone Development)

Uses the Model-View-Control methodology. MVC divides all the functionality up into 3 distinct categories.
  • Model - The classes that hold your application's data
  • View - Made up of the windows, controls and other elements that the user can see and interact with. 
  • Controller - Binds the model and view together and is the application logic that decides how to handle the users input.
Controller class can refer to objects in the nib by using a special kind of instance variable called an outlet. 
An outlet is like a pointer that points to an object within a nib.
In the opposite direction , interface objects in the nib can be set up to trigger special methods in the controller class. These special methods are known as action methods.

@synthesize tells the compiler to automatically create the accessor and mutator methods for us (getters and setters).

Releasing objects when you are done with them is very important as iPhone device is constrained.

Delegates are classes that take responsibility for doing certain things on behalf of another object.

Hold down the otpion key, move the cursor over the word so it turns into a crosshair, when it does double click!  

Search This Blog