iphone开发小记

时间:2023-03-10 01:11:36
iphone开发小记

程序功能:点击Button实现按钮的文本变换

一,打开Xcode,新建single view based application,拖动一个Button控件到屏幕中间

项目目录树下包含AppDelegate.h AppDelegate.m MainStoryboard.storyboard ViewController.h ViewController.h等文件
其中MainStoryboard.storyboard用来设计UI界面,ViewController.*文件用来编码

ViewController.m代码如下

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
} - (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
} - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} - (void)dealloc {
[super dealloc];
}
@end

ViewController.h代码如下

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

二,通过图形界面实现Button控件的属性(ID)及动作(Action)的配置

1,鼠标拖动Touch Down事件到ViewController.h文件,xcode会自动提示插入Action

iphone开发小记

2,设置Action名称为(OnBtnTouch)

iphone开发小记

3,同理,配置按钮的名称(ID),可以在程序中通过这个ID号控制控件

iphone开发小记

三,打开ViewController.m文件实现OnBtnTouch的具体逻辑,代码如下:

ViewController.m代码如下

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize ibtn; - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
} - (void)viewDidUnload
{
[self setIbtn:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
} - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} - (void)dealloc {
[ibtn release];
[super dealloc];
}
- (IBAction)OnBtnTouch:(id)sender {
[ibtn setTitle:@"Go to Work!" forState:UIControlStateNormal];
}
@end

ViewController.h代码如下

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

- (IBAction)OnBtnTouch:(id)sender;

@property (retain, nonatomic) IBOutlet UIButton *ibtn;

@end

我们只手动添加了下面这行代码,其他都是xcode帮我们自动生成的

[ibtnsetTitle:@"Go to Work!"forState:UIControlStateNormal];