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

来源:未知 责任编辑:责任编辑 发表时间:2013-08-27 11:16 点击:

想必大家都阅读过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 
#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 
#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看看效果:

\

 

摘自 亨利摩根的专栏
 

    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • Lexical or Preprocessor Issue 'xxx.h
    • ios学习笔记(二)xcode 4.3.2下实现基本交互
    • ios版本的helloworld
    • iphone(object-c) 内存管理(3) 有效的内存管理 前半部分
    • ios学习笔记(一)xcode 4.3.2下创建第一个ios项目
    • IOS类似iphone通讯录TableView的完整demo【附源码】
    • UITableView一些方法
    • [iPhone中级]iPhone团购信息客户端的开发 (二)
    • 如何为Iphone应用创建启动界面
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1