App Example of 《Beginning iPhone Development》

网上查到一本好书,如题。

下面解释下第一个例子:让label显示按下了哪个buttonLeft or Right)。(书中第一例helloworld只用label显示了一下,没有用户事件,我就不讲了)

1. 建立View-based Application,起个名字,下面代号AppName

2. AppNameViewController.xib,打Interface Builder

library里拖一个label、两个button(文字分LeftRight)到界面上。

Files Owner,打Inspectoroutlet里加一个statusText UILabelAction里加一个buttonPressed:事件;

连接处:statusText的+拖到Label上,buttonPressed:的+拖到Left上,再拖到Right上(会显示Multiple)。

3. 接下去只需改AppNameViewController.h,和AppNameViewController.m即可。

头文件中已有:

#import <UIKit/UIKit.h>

#import <Foundation/Foundation.h>

 

@interface myHelloWorldViewController : UIViewController {

    IBOutlet UILabel *statusText;

}

– (IBAction)buttonPressed:(id)sender;

@end

 

源文件中已有:

#import "myHelloWorldViewController.h"

 

@implementation myHelloWorldViewController

– (IBAction)buttonPressed:(id)sender {

   

}

@end

 

头文件的变量定义之后添加一句属性声明:@property (retain, nonatomic) UILabel *statusText;

在源文件中实现属性及方法:

@synthesize statusText;

-(IBAction)buttonPressed:(id)sender

{

       NSString  *title = [sender titleForState:UIControlStateNormal];

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

    statusText.text = newText;

    [newText release];

}

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s