ios学习笔记(二)xcode 4.3.2下实现基本交互

来源:互联网 发布:阿里云客服抢班语录 编辑:程序博客网 时间:2024/05/15 19:22

想必大家都阅读过iphone4与ipad2开发基础教程吧,这本书的xcode与现在的最新版本有些区别,去掉了view base application,只有比较接近的single view application.

首先我们创建一个single view application,注意这里我们不用自动引用计数。

接着我们点击工程列表中的MainStoryboard_iphone.storyboard 来编辑界面:

我们创建两个按钮和一个用来输出的空白文本:

目录结构与视图结构:


2.接着我们写代码来用来与IB界面编辑器来进行连接:在这里我们可以认为IBOutlet是与IB交互的输出,而IBAction则是IB交互的事件。

先编写ViewContoller.h:

#import <UIKit/UIKit.h>@interface ViewController : UIViewController{}@property (nonatomic,retain) IBOutlet UILabel * statusText;-(IBAction)buttonPressed:(id)sender;@end

接着在ViewContoller.m来实现功能:

#import "ViewController.h"@implementation ViewController@synthesize statusText;-(IBAction)buttonPressed:(id)sender{        NSString *title = [sender titleForState:UIControlStateNormal];//sender用于获得了不同的button的文字 titleForState是根据button状态获取文字的函数        NSString *newText = [[NSString alloc] initWithFormat:@"%@ button pressed.", title];//将title中的文字放入newText中        statusText.text = newText;    }- (void)viewDidLoad{    [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.}- (void)viewDidUnload{    [super viewDidUnload];    self.statusText = nil;    // Release any retained subviews of the main view.}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);    } else {        return YES;    }}- (void)dealloc{    [statusText release];        [super dealloc];}@end

接着我们把这些代码与IB连接起来:

选择MainStoryboard_iphone.storyboard 右击View Controller Scene中的 Buton,选择Touch down与界面的

bottonPressed相连接:


接着将lable和statusText连接在一块:


最后运行程序,点击botton看看效果:


原创粉丝点击