<body><script type="text/javascript"> function setAttributeOnload(object, attribute, val) { if(window.addEventListener) { window.addEventListener('load', function(){ object[attribute] = val; }, false); } else { window.attachEvent('onload', function(){ object[attribute] = val; }); } } </script> <div id="navbar-iframe-container"></div> <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script> <script type="text/javascript"> gapi.load("gapi.iframes:gapi.iframes.style.bubble", function() { if (gapi.iframes && gapi.iframes.getContext) { gapi.iframes.getContext().openChild({ url: 'https://www.blogger.com/navbar.g?targetBlogID\x3d8334277\x26blogName\x3dSriram\x27s+Blog\x26publishMode\x3dPUBLISH_MODE_BLOGSPOT\x26navbarType\x3dBLUE\x26layoutType\x3dCLASSIC\x26searchRoot\x3dhttp://metallicatony.blogspot.com/search\x26blogLocale\x3den\x26v\x3d2\x26homepageUrl\x3dhttp://metallicatony.blogspot.com/\x26vt\x3d-8718433808682107797', where: document.getElementById("navbar-iframe-container"), id: "navbar-iframe" }); } }); </script>

Saturday, March 26, 2011

iPhone Code Samples

I decided to host all my sample iPhone code somewhere in the web. I believe by doing this way i wont lose them and they might also be helpful to other developers. Tried out a couple of project hosting sites and finally boiled down to either Google Code (svn) or GitHub (git). After getting a feel of both, i decided to go for GitHub. Currently this project " iPhoneCodeSamples " contains a handful of programs that can be used to learn about views, view controllers, basic interactions, events, actions, controls, ActionSheets, Alerts, screen orientation using autosizing and autorotation. There are also programs that use content views, multiview architecture, picker components, tab bars, navigation controllers and tableviews. I have decided to keep adding more sample code to this project as and when i write something useful. This project can be downloaded for your use or you can even browse code snippets using GitHub's inline viewer too. Feel free to modify and use at your own ease and risk :)

Project repository can be accessed from iPhoneCodeSamples - GitHub.

LRButtonClick is the simplest of all examples. It presents two buttons and a label. Based on the button that was tapped by the user, it changes the text in the label.

This example introduces various controls like buttons, segments, switch, sliders and passive controls like labels, image views and static controls like text fields. It pops with a number or alphabetic keyboard according to the type of text field and resigns the keyboard when the user taps anywhere else. The slider control changes the number value as per the slided control. This example also hides the unneeded controls and is capable of detecting which segment was tapped. It makes use of Action sheets, alerts and demonstrates how to use a custom image for a button and to switch the image based on the state of the button that changes again based on the user action.

This example shows how to make use of the autorotation feature. The screen size of iPhone is 320x480 (widthxheight) in which 20 pixels will be taken by top status bar. So effectively it is 320x460 (in portrait mode) and becomes 480x300 (in landscape mode) when the phone is tilted by 90 degrees. Accordingly the view of your app should be rearranged to present the contents better. There are various ways to do this task but this example takes the route of having two pre-constructed views - one for portrait mode and the other for landscape mode. It shows how to detect the rotation and animate the view rotation accordingly.

SwitchView has a Root Controller with two different view nibs and a controller for each of those views. It shows how to make use of the root controller and switch the views determining the tap action if performed by the user on the button provided in the app. While doing so it animates the view transition with a flip and curl up effect. This is accomplished by using animation blocks and view transition effects. Switching is done by detecting whether they are already associated with their superview, inserting if not and removing the previous view from the view hierarchy. The views also have a center button that when tapped, pops up with an alert (yes, the same type of alert that pops when your battery is low).

Single Component Picker
This sample program dictates how to make use of a picker component and tie it up with a datasource and delegate. The datasource will be needed by the picker to show the information to the user and the delegate will be used to draw the title and to respond to the actions performed by the user as well.

Dependent Component Picker
This sample takes us to the next level of showing how to use a dependent picker. By that i mean, when the user has to select a date, say we can show 3 pickers - one each for Year, month and date or when the user has to choose zip code, we can show 3 pickers state, city, zip. These pickers are called dependent pickers because the data that needs to be shown in any of the child pickers is dependent on the selection of data in the parent pickers. The source of data is a plist file and a set of array and dictionary data structures are used to accomplish this.

Custom Picker
Custom picker can be visualized something similar to a slot machine that is commonly found in casinos. It dictates how to create a picker component and use image views on it. It also uses a "spin" button with which you can make the picker wheels to rotate randomly. If the images in the picker wheels become the same after you spin the wheels, then it displays that you won a jackpot! Not really though :) But to speak technically, the spin method involves looping through all the components in the picker, generate a random number and then selecting a row in the component based on the generated random number. Sounds tricky?! huh?

Simple TableView
Tableviews (UITableView) and tableview controllers (UITableViewController) are some of the most important concepts. Almost every app in your mobile uses them. This example code incorporates a simple tableview and demonstrates the usage of UITableViewDelegate and UITableViewDataSource protocols to tie the tableview with the data structures (model) using the controller. It takes advantage of dequeued cells and thereby handles memory efficiently. It explores the different elements (Image, TextLabel and Detail Text Label) that are available in every table view cell and shows how to use them.

Tableviews come in two flavors - Plain & Grouped. While the above example shows how to create a plain tableview, this example depicts how to create a grouped tableview. Grouped tableview (similar to settings in every app) can be created by configuring the datasource methods that correspond to sections - like number of sections, number of rows and title name for every section. We can retain the same configuration and switch back the tableview mode to plain style to get an indexed tableview with sections (similar to contacts app).

Nav sample is all about navigation controller. Without navigation controller, things would have been much more difficult in the app world. This sample comprises a rootview controller and a number of sub controllers. The root controller is the parent controller that presents with a broad level of categories for the user to select. Based on the selection of the tableview cell, the root controller dynamically creates and pushes the corresponding subcontroller to the navigation stack! Again the sub controllers or first level controllers have subcontrollers or second level controllers. All the views for the child controllers are managed dynamically based on the user selection.

This sample shows the categories using the rootview controller.
If the movies cell is selected, the movielistcontroller is pushed onto the navigation controller stack. The movie list controller has an accessory button for every cell. If the accessory button was pressed, that in turn pushes the moviedetail controller to stack.
If the directors cell is selected, the DirectorsCheckList controller is pushed from the rootview controller. This tableview is unique in the way that the user can enable only one of the check marks from the list of cells that are provided. It remembers the cell's indexpath of old selection and the cell's index path of the new selection to accomplish this.

Labels: , , ,


Post a Comment

<< Home