iPhone & iPad Tutorial: Working With Buttons

Posted by Dr. Burton on June 10, 2010 in iPad, iPhone, Mobile |

Lesson 4:

Let’s go the next step after our little ‘hello world’ demonstration of coding for the iPhone/iPad. In this tutorial, we are going to provide an example of the M-V-C (Model, View, Controller) process of creating an app for the iPhone or the iPad.

To get started we need to create our model.  This translates into creating the header and main file of the program code.  Our project is just a simple app with two buttons.  When you click on the left one, ‘Left button pressed’ will appear in a label. When you click on the right button, ‘Right button pressed’ will appear in the same label.  Nothing overly challenging, but it will give us the opportunity to practice the MVC.

The code for the header file is:

After @interface type:

UILabel  *myText;

@property(retain, nonatomic) IBOutlet UILabel *myText;


In the main file:

@synthesize myText;

NSString *title = [ sender titleForState:UIControlStateNormal];
NSString *newText = [[NSString alloc] initWithFormat: @”%@ button pressed.”, title];
myText.text = newText;
[newText release];

[myText release];

[super dealloc]

[tubepress video=”lnrSZg_5a_w”]

View and Controller
The view and controller are easy to configure.   This short video should demonstrate how to do it effectively:

[tubepress video=”3ehRE_IVfks”]

Return to iPhone Course

Tags: , , , , , ,


  • Don Davis Jr says:

    Dr. B,
    Your videos are very helpful to an old FORTRAN hacker (DOD trained).
    I am attempting to learn Objective-C in my retirement and have a bad attitude to begin with.

    Question: Once I “link” myText with the UI Label how do I “unlink” it. Example,I had myText1 and myText2 defined in the .h file and made a mistake and linked Label1 to myText2 instead of myText1 – how do I “unlink”??
    Any help would be appreciated.


  • Dr. Burton says:

    Having learned FORTRAN in college in the 80’s, I completely understand!
    You might want to go with Corona which uses the LUA language. I’ve found it much friendlier than Obj-C.

    To unlink, you just need to open the inspector. It will show the links that have been created. If you click on the x beside the link, it will be removed.

  • Bas says:

    Thanks, helpful!

    But when I copied your code I had an error because of the quotes in the next line:

    NSString *newText = [[NSString alloc] initWithFormat: @”%@ button pressed.”, title];

    So you need to replace ” with ”

    NSString *newText = [[NSString alloc] initWithFormat: @”%@ button pressed.”, title];

  • Bas says:

    WordPress is transforming the quotes so in the code you need to replace the right double quotation mark (”) with double quotes (")

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Copyright © 2010-2022 Burtons Media Group All rights reserved.
This site is using the Desk Mess Mirrored theme, v2.5, from BuyNowShop.com.