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

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

 

 

 

 

 

你可能感兴趣的:(view)