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
#import <UIKit/UIKit.h>
#import "secondview.h"
@interface ViewController : UIViewController
{
secondview *secondviewData;
__weak IBOutlet UITextField *textfield;
}
@property (nonatomic,retain)secondview *secondviewData;
- (IBAction)passdata:(id)sender;
@end
ViewController.m
#import "ViewController.h"
#import "secondview.h"
@implementation ViewController
@synthesize secondviewData;
-(IBAction)passdata:(id)sender {
secondview *second = [[secondview alloc] initWithNibName:nil bundle:nil];
self.secondviewData = second;
secondviewData.passedValue = textfield.text;
[self presentModalViewController:second animated:YES];
}
Secondview.h
#import <UIKit/UIKit.h>
@interface secondview : UIViewController
{
__weak IBOutlet UILabel *label;
NSString *passedValue;
}
@property (nonatomic,retain)NSString *passedValue;
- (IBAction)back:(id)sender;
@end
Secondview.m
#import "ViewController.h"
@implementation secondview
@synthesize passedValue;
-(IBAction)back:(id)sender {
ViewController *second = [[ViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated:YES];
}
- (void)viewDidLoad
{
label.text = passedValue;
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
视频:http://www.youtube.com/watch?v=Y4zmM_Rqv6Y&feature=player_embedded
本文档通过实例演示了如何在iOS应用中实现视图间的交互及数据传递,包括使用按钮触发视图切换并传递UITextField中的数据到下一个视图,并在该视图中的UILabel显示这些数据。
1919

被折叠的 条评论
为什么被折叠?



