传送数据 Passing Data Between Views (实例)

ps:在presentModalViewController或者pushviewController之前,都是不会加载该view的nib文件的,

也就没有new IBOutlet的实体对象,所以在此之前使用IBOutlet对象保存数据都是错误的行为。需要

一个中间的成员变量来做中介,才能正确的传递数据在view之间。


Passing Data Between Views

In this tutorial i will be showing you how to create multi functioning buttons 

Features:

  • 1 Label
  • 1 UITextField
  • 2 Buttons
  • 2 Views

Passing dada between views is very handy if you require content to move throughout you application its also very helpful for games where your score or info can be passed to the next view where theres leader boards or extra content

 

The Code

Play the video to get a step by step walkthrough and all code can be copy and pasted

 

ViewController.h 

C代码   收藏代码
  1. #import <UIKit/UIKit.h>  
  2. #import "secondview.h"  
  3.   
  4. @interface ViewController : UIViewController  
  5. {  
  6.     secondview *secondviewData;   
  7.     __weak IBOutlet UITextField *textfield;  
  8. }  
  9.   
  10. @property (nonatomic,retain)secondview *secondviewData;  
  11.   
  12. - (IBAction)passdata:(id)sender;  
  13.   
  14. @end  
  

ViewController.m 

C代码   收藏代码
  1. #import "ViewController.h"  
  2. #import "secondview.h"  
  3.   
  4. @implementation ViewController  
  5.   
  6. @synthesize secondviewData;  
  7.   
  8. -(IBAction)passdata:(id)sender {  
  9.     secondview *second = [[secondview alloc] initWithNibName:nil bundle:nil];    
  10.     self.secondviewData = second;   
  11.     secondviewData.passedValue = textfield.text;  
  12.     [self presentModalViewController:second animated:YES];  
  13. }  
 

Secondview.h

C代码   收藏代码
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface secondview : UIViewController  
  4. {  
  5.     __weak IBOutlet UILabel *label;  
  6.     NSString *passedValue;      
  7. }  
  8.   
  9. @property (nonatomic,retain)NSString *passedValue;  
  10.   
  11. - (IBAction)back:(id)sender;  
  12.   
  13. @end  

 

Secondview.m 

C代码   收藏代码
  1. #import "ViewController.h"  
  2.   
  3. @implementation secondview  
  4.   
  5. @synthesize passedValue;  
  6.   
  7. -(IBAction)back:(id)sender {  
  8.     ViewController *second = [[ViewController alloc] initWithNibName:nil bundle:nil];  
  9.     [self presentModalViewController:second animated:YES];  
  10. }   
  11.   
  12. - (void)viewDidLoad  
  13. {  
  14.     label.text = passedValue;  
  15.     [super viewDidLoad];  
  16.     // Do any additional setup after loading the view from its nib.  
  17. }   

 

 

视频:http://www.youtube.com/watch?v=Y4zmM_Rqv6Y&feature=player_embedded


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值