MACDesign Guidelines

Embed Size (px)

Citation preview

  • 8/13/2019 MACDesign Guidelines

    1/7

    View-Based Application .

    In this example you'll learn how to create an iPhone application. This post will show youhow to design a simple Cocoa touch interface and then link objects in the interface toobjects and methods in the code to complete the application.

    Let's get started,

    Open Xcode and select new project from the file menu. Select iPhone OS from the menu and then View-Based Application . Check that

    "iPhone " is selected on Product .

    Save as: ViewBasedApplication . Click on Save .

    Double-click on Resources .There are two .xib files. These files are xml files that are used to define user interfaces foriphone and os x applications.

    http://4.bp.blogspot.com/_iC4DSJmKLio/S-nwLx6NbSI/AAAAAAAAAMU/lBpO2_RvEIQ/s1600/Screen+shot+2010-05-11+at+5.02.29+PM.pnghttp://2.bp.blogspot.com/_iC4DSJmKLio/S-C5Rh3ZrwI/AAAAAAAAAEc/_P0Hi9muHLg/s1600/Picture+2.pnghttp://3.bp.blogspot.com/_iC4DSJmKLio/S-nv5kcFFHI/AAAAAAAAAMM/eNDDKx1gwHY/s1600/Screen+shot+2010-05-11+at+4.57.48+PM.pnghttp://4.bp.blogspot.com/_iC4DSJmKLio/S-nwLx6NbSI/AAAAAAAAAMU/lBpO2_RvEIQ/s1600/Screen+shot+2010-05-11+at+5.02.29+PM.pnghttp://2.bp.blogspot.com/_iC4DSJmKLio/S-C5Rh3ZrwI/AAAAAAAAAEc/_P0Hi9muHLg/s1600/Picture+2.pnghttp://3.bp.blogspot.com/_iC4DSJmKLio/S-nv5kcFFHI/AAAAAAAAAMM/eNDDKx1gwHY/s1600/Screen+shot+2010-05-11+at+4.57.48+PM.pnghttp://4.bp.blogspot.com/_iC4DSJmKLio/S-nwLx6NbSI/AAAAAAAAAMU/lBpO2_RvEIQ/s1600/Screen+shot+2010-05-11+at+5.02.29+PM.pnghttp://2.bp.blogspot.com/_iC4DSJmKLio/S-C5Rh3ZrwI/AAAAAAAAAEc/_P0Hi9muHLg/s1600/Picture+2.pnghttp://3.bp.blogspot.com/_iC4DSJmKLio/S-nv5kcFFHI/AAAAAAAAAMM/eNDDKx1gwHY/s1600/Screen+shot+2010-05-11+at+4.57.48+PM.png
  • 8/13/2019 MACDesign Guidelines

    2/7

    As you can see, there is one file called MainWindow and another one calledViewBasedApplicationViewController. The MainWindow file contains the "Main Window" foryour iPhone application. This window will be called when your application starts running.A window is used to load views on an iPhone application. Most applications will use onemain window and will use different views to display all the screens your application mayhave.

    Creating a Cocoa Interface Now, double-click on the ViewBasedApplicationViewController.xib file to open up the

    interface builder. Interface Builder is the application you will be using to build theinterfaces for your iPhone apps.

    Click on the view window.

    Press Command + Shift + L to bring up the library window . Type " label" on the filter field. A label will appear on the window. Drag and drop it to

    the view window. Go back to the library window and type " button" . Take the button and drop it on the

    view window too. Click on an element on the view and press Command + 1 to bring up the attributes for

    that particular element. From here, you can edit each element of the view. Try tomake the view window look like this.

    Linking ElementsNow, let me explain something here. Everything you do in the interface builder iscompletely separated from your Objective-C code. So, the elements you add to the viewwon't be accessible from the code at the beginning. In order to have access to theseelements, you must define an equivalent object in your code and then "link it" to theinterface using the interface builder. We will see how to do this next.

    Go to the ViewBasedApplicationViewController.xib window and click on File's owner . Press command + 4 to bring up the identity inspector .

    http://4.bp.blogspot.com/_iC4DSJmKLio/S-C-2h9ferI/AAAAAAAAAE0/RV48lMMs0pI/s1600/Picture+5.pnghttp://2.bp.blogspot.com/_iC4DSJmKLio/S-C9Tj5tVxI/AAAAAAAAAEs/75ASHRN_aR0/s1600/Picture+4.pnghttp://4.bp.blogspot.com/_iC4DSJmKLio/S-C-2h9ferI/AAAAAAAAAE0/RV48lMMs0pI/s1600/Picture+5.pnghttp://2.bp.blogspot.com/_iC4DSJmKLio/S-C9Tj5tVxI/AAAAAAAAAEs/75ASHRN_aR0/s1600/Picture+4.png
  • 8/13/2019 MACDesign Guidelines

    3/7

    Click on the arrow on the Class section of the window.

    The class ViewBasedApplicationViewController will open on the Library Window

    Click on Outlets and then on the + . Type " label" for the outlet and "UILabel" insteadof " id" for the type. Add another entry for button .

    Now go to Actions and click on the +. Add a new action named "doSomething": .

    http://3.bp.blogspot.com/_iC4DSJmKLio/S-nx14yEK3I/AAAAAAAAAMs/wElHkZRw5T0/s1600/Screen+shot+2010-05-11+at+5.09.04+PM.pnghttp://4.bp.blogspot.com/_iC4DSJmKLio/S-nxIEC4lnI/AAAAAAAAAMk/0eG12OVeVw0/s1600/Screen+shot+2010-05-11+at+5.06.40+PM.pnghttp://2.bp.blogspot.com/_iC4DSJmKLio/S-nw1acVoSI/AAAAAAAAAMc/pWZQEVakhDU/s1600/Screen+shot+2010-05-11+at+5.05.27+PM.pnghttp://3.bp.blogspot.com/_iC4DSJmKLio/S-nx14yEK3I/AAAAAAAAAMs/wElHkZRw5T0/s1600/Screen+shot+2010-05-11+at+5.09.04+PM.pnghttp://4.bp.blogspot.com/_iC4DSJmKLio/S-nxIEC4lnI/AAAAAAAAAMk/0eG12OVeVw0/s1600/Screen+shot+2010-05-11+at+5.06.40+PM.pnghttp://2.bp.blogspot.com/_iC4DSJmKLio/S-nw1acVoSI/AAAAAAAAAMc/pWZQEVakhDU/s1600/Screen+shot+2010-05-11+at+5.05.27+PM.pnghttp://3.bp.blogspot.com/_iC4DSJmKLio/S-nx14yEK3I/AAAAAAAAAMs/wElHkZRw5T0/s1600/Screen+shot+2010-05-11+at+5.09.04+PM.pnghttp://4.bp.blogspot.com/_iC4DSJmKLio/S-nxIEC4lnI/AAAAAAAAAMk/0eG12OVeVw0/s1600/Screen+shot+2010-05-11+at+5.06.40+PM.pnghttp://2.bp.blogspot.com/_iC4DSJmKLio/S-nw1acVoSI/AAAAAAAAAMc/pWZQEVakhDU/s1600/Screen+shot+2010-05-11+at+5.05.27+PM.png
  • 8/13/2019 MACDesign Guidelines

    4/7

    Press Command + S to save. Now click on File's Owner again, go to the file menu and click on write class files . Click on save and then select merge .

    The View Controller For ViewBasedApplicationViewController.h select left for both differences from the

    Actions menu (bottom right corner of the window) .

    For ViewBasedApplicationViewController.m select left for the first difference andanything for the second one (since it's the same code, the only difference are theblank lines).

    http://4.bp.blogspot.com/_iC4DSJmKLio/S-GZ4CnQ0II/AAAAAAAAAFM/AZI0sjgl7Gc/s1600/Picture+8.pnghttp://2.bp.blogspot.com/_iC4DSJmKLio/S-nyxTxlc2I/AAAAAAAAAM0/7HNeXlFeEiM/s1600/Screen+shot+2010-05-11+at+5.13.41+PM.pnghttp://4.bp.blogspot.com/_iC4DSJmKLio/S-GZ4CnQ0II/AAAAAAAAAFM/AZI0sjgl7Gc/s1600/Picture+8.pnghttp://2.bp.blogspot.com/_iC4DSJmKLio/S-nyxTxlc2I/AAAAAAAAAM0/7HNeXlFeEiM/s1600/Screen+shot+2010-05-11+at+5.13.41+PM.png
  • 8/13/2019 MACDesign Guidelines

    5/7

    Click on the red button and then save each file. Now, we have to link the elements from our view to the ones we just created in our

    class files. Go to the view and press ctrl + click on the label. Click on the circle next to new

    referencing outlet.

    Drag the mouse to File's Owner .

    Click on label .

    http://1.bp.blogspot.com/_iC4DSJmKLio/S-Gfw_DqJdI/AAAAAAAAAFs/HQcdqpw73yc/s1600/Picture+13.pnghttp://2.bp.blogspot.com/_iC4DSJmKLio/S-GfmrDwdII/AAAAAAAAAFk/NN0QzKDDa0U/s1600/Picture+12.pnghttp://1.bp.blogspot.com/_iC4DSJmKLio/S-GfQMLROSI/AAAAAAAAAFc/_wXyOUc81AM/s1600/Picture+11.pnghttp://4.bp.blogspot.com/_iC4DSJmKLio/S-GaemLaxdI/AAAAAAAAAFU/k-leZExIbQA/s1600/Picture+9.pnghttp://1.bp.blogspot.com/_iC4DSJmKLio/S-Gfw_DqJdI/AAAAAAAAAFs/HQcdqpw73yc/s1600/Picture+13.pnghttp://2.bp.blogspot.com/_iC4DSJmKLio/S-GfmrDwdII/AAAAAAAAAFk/NN0QzKDDa0U/s1600/Picture+12.pnghttp://1.bp.blogspot.com/_iC4DSJmKLio/S-GfQMLROSI/AAAAAAAAAFc/_wXyOUc81AM/s1600/Picture+11.pnghttp://4.bp.blogspot.com/_iC4DSJmKLio/S-GaemLaxdI/AAAAAAAAAFU/k-leZExIbQA/s1600/Picture+9.pnghttp://1.bp.blogspot.com/_iC4DSJmKLio/S-Gfw_DqJdI/AAAAAAAAAFs/HQcdqpw73yc/s1600/Picture+13.pnghttp://2.bp.blogspot.com/_iC4DSJmKLio/S-GfmrDwdII/AAAAAAAAAFk/NN0QzKDDa0U/s1600/Picture+12.pnghttp://1.bp.blogspot.com/_iC4DSJmKLio/S-GfQMLROSI/AAAAAAAAAFc/_wXyOUc81AM/s1600/Picture+11.pnghttp://4.bp.blogspot.com/_iC4DSJmKLio/S-GaemLaxdI/AAAAAAAAAFU/k-leZExIbQA/s1600/Picture+9.pnghttp://1.bp.blogspot.com/_iC4DSJmKLio/S-Gfw_DqJdI/AAAAAAAAAFs/HQcdqpw73yc/s1600/Picture+13.pnghttp://2.bp.blogspot.com/_iC4DSJmKLio/S-GfmrDwdII/AAAAAAAAAFk/NN0QzKDDa0U/s1600/Picture+12.pnghttp://1.bp.blogspot.com/_iC4DSJmKLio/S-GfQMLROSI/AAAAAAAAAFc/_wXyOUc81AM/s1600/Picture+11.pnghttp://4.bp.blogspot.com/_iC4DSJmKLio/S-GaemLaxdI/AAAAAAAAAFU/k-leZExIbQA/s1600/Picture+9.png
  • 8/13/2019 MACDesign Guidelines

    6/7

    Now, the label of the view is linked to the label in the code, properly. So, when youupdate the label in the code, the label of the view will be updated too.

    Let's link the button and the action from the button too. This time I'll show you a differentway to do it, but you can do it one way or the other.

    Click on File's Owner and press Command + 2 . This will bring up the Connections

    Inspector window. Click on the circle next to button and drag it to the button onthe view.

    If you did it correctly it will look like this.

    Now, click on the circle next to doSomething: and drag it to the button too. SelectTouch Up Inside .

    Now, the actions and elements of the view are linked to the ones on our code, properly.

    Press Command + S to save. Go to Xcode and open ViewBasedApplicationViewController.h . And change your

    code so it looks like this:#import @interface ViewBasedApplicationViewController : UIViewController {

    IBOutlet UIButton * button ;

    IBOutlet UILabel * label ;

    int counter;}

    - ( IBAction )doSomething:( id )sender;

    @end

    The only line you need to add is " int counter; "

    Open ViewBasedApplicationController.m and add the following code to thedoSomething: method.

    http://1.bp.blogspot.com/_iC4DSJmKLio/S-GiGTu8VmI/AAAAAAAAAGM/v7YLYfaHflQ/s1600/Picture+18.pnghttp://3.bp.blogspot.com/_iC4DSJmKLio/S-GhYfY2iGI/AAAAAAAAAF8/PNK4rWw42Ng/s1600/Picture+15.pnghttp://2.bp.blogspot.com/_iC4DSJmKLio/S-GhKkm4-sI/AAAAAAAAAF0/N_D0OUPLnRA/s1600/Picture+14.pnghttp://1.bp.blogspot.com/_iC4DSJmKLio/S-GiGTu8VmI/AAAAAAAAAGM/v7YLYfaHflQ/s1600/Picture+18.pnghttp://3.bp.blogspot.com/_iC4DSJmKLio/S-GhYfY2iGI/AAAAAAAAAF8/PNK4rWw42Ng/s1600/Picture+15.pnghttp://2.bp.blogspot.com/_iC4DSJmKLio/S-GhKkm4-sI/AAAAAAAAAF0/N_D0OUPLnRA/s1600/Picture+14.pnghttp://1.bp.blogspot.com/_iC4DSJmKLio/S-GiGTu8VmI/AAAAAAAAAGM/v7YLYfaHflQ/s1600/Picture+18.pnghttp://3.bp.blogspot.com/_iC4DSJmKLio/S-GhYfY2iGI/AAAAAAAAAF8/PNK4rWw42Ng/s1600/Picture+15.pnghttp://2.bp.blogspot.com/_iC4DSJmKLio/S-GhKkm4-sI/AAAAAAAAAF0/N_D0OUPLnRA/s1600/Picture+14.png
  • 8/13/2019 MACDesign Guidelines

    7/7