Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[SAMPLE] [iOS] Transferring Data from one UIViewController to another using Storyboar

Initiate Mage
Joined
Sep 12, 2012
Messages
21
Reaction score
1
In this same we have 2 ViewControllers, a textbox and a button on the first and a label on the second. The idea is that the textbox from the first view sets the label in the second view, via a button.

FirstViewController.h:
Code:
#import <UIKit/UIKit.h>

@interface  FirstViewController : UIViewController{
    IBOutlet UIButton *button;
    IBOutlet UITextField *textbox;
}


@end

FirstViewController.m:
Code:
#import "FirstViewController.h"
#import "SecondViewController.h"


@implementation FirstViewController




#pragma mark - View lifecycle


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    textbox.text = @"Test";
    [button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
}


-(void) buttonPressed
{
    [self performSegueWithIdentifier:@"SecondViewSegue" sender:self];
}




-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
    if ([[segue identifier] isEqualToString:@"SecondViewSegue"]) {
        SecondViewController *secondview = [segue destinationViewController];
        secondview.PushedData = textbox.text;
    }
}


@end

SecondViewController.h:
Code:
#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController
{
    NSString *PushedData;
    IBOutlet UILabel *lable;
}


@property (nonatomic,strong) NSString *PushedData;


@end

Code:
#import "SecondViewController.h"

@implementation SecondViewController
@synthesize PushedData;


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    lable.text = PushedData;
}


@end

The important part of the code is here
Code:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {    
    if ([[segue identifier] isEqualToString:@"SecondViewSegue"]) {
        SecondViewController *secondview = [segue destinationViewController];
        secondview.PushedData = textbox.text;
    }

Replacing SecondViewSegue with the name of your segue.

~Youmu
 
Back
Top