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

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

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

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

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

目录结构与视图结构:


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

先编写ViewContoller.h:

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

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

  1. #import "ViewController.h"  
  2. @implementation ViewController  
  3. @synthesize statusText;  
  4. -(IBAction)buttonPressed:(id)sender  
  5.   
  6. {  
  7.       
  8.     NSString *title = [sender titleForState:UIControlStateNormal];//sender用于获得了不同的button的文字 titleForState是根据button状态获取文字的函数  
  9.       
  10.     NSString *newText = [[NSString alloc] initWithFormat:@"%@ button pressed.", title];//将title中的文字放入newText中  
  11.       
  12.     statusText.text = newText;  
  13.       
  14. }  
  15.   
  16. - (void)viewDidLoad  
  17. {  
  18.     [super viewDidLoad];  
  19.     // Do any additional setup after loading the view, typically from a nib.  
  20. }  
  21.   
  22. - (void)viewDidUnload  
  23. {  
  24.     [super viewDidUnload];  
  25.     self.statusText = nil;  
  26.     // Release any retained subviews of the main view.  
  27. }  
  28.   
  29. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  30. {  
  31.     if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {  
  32.         return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);  
  33.     } else {  
  34.         return YES;  
  35.     }  
  36. }  
  37. - (void)dealloc  
  38. {  
  39.     [statusText release];  
  40.       
  41.     [super dealloc];  
  42. }  
  43. @end  

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

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

bottonPressed相连接:


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


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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值