Khyati Vaishnav
Khyati Vaishnav

Reputation: 43

To fetch data from one page to another

I know this question has been answered before, but i have difficulty among those. There is a textfield on one page and i want to retrieve that it's content into label which is located on the next page by clicking on button.

i have tried this. tfName is id of textfield in page 1.

 -(IBAction)onClick:(id)sender
{
 NSString *string1 =tfName.text;
}

and in page2

- (void)viewDidLoad {
    [super viewDidLoad];
label1.text=string1;
}

Upvotes: 1

Views: 92

Answers (3)

TechBee
TechBee

Reputation: 1941

Make a Singleton class

class DataModel {

class var sharedInstance: DataModel {
    get {
        struct Static {
            static var instance: DataModel? = nil
            static var token: dispatch_once_t = 0
        }
        dispatch_once(&Static.token, {
            Static.instance = DataModel()
        })
        return Static.instance!
    }
}

var textData:String = ""

}

on button tap do this:

DataModel.sharedInstance.textData = "page 1 data"

on 2nd page viewDidLoad fund

label1.text = DataModel.sharedInstance.textData

That should work fine!

Upvotes: 0

Aruna kumari
Aruna kumari

Reputation: 319

First create ViewController object and then assign value to that property.

-(IBAction)onClick:(id)sender
    {
    SecondViewController *tempView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
        tempView. string1 = tfName.text;
         [self.navigationController pushViewController:tempView animated:YES];

    }

on your SecondViewController.h create the one global string like

@Property(nonatomic, Strong)NSString *string1;

on your implementation SecondViewController.m systensize your String like

@Synthesize string1;

finally call like

- (void)viewDidLoad {
[super viewDidLoad];
 if (string1.length >0)
 label1.text=string1;
}

Upvotes: 0

Satish A
Satish A

Reputation: 584

Here is the simple example code:

Class A.h :

UITextfield *tfName;

Class A.m :

tfName.text = @"Hello world";




-(IBAction)onClick:(id)sender
{
    ClassB *b = [[ClassB alloc]initwithnibname@"ClassB"];
     b.string1 =tfName.text;
    [self.navigationController pushViewController:b animated:YES];
}

Class B.h :

@Property(..)NSString *string1;

Class B.m :

label1.text = _string1.

Note: you need to import ClassB in ClassA, i.e., #import "ClassB.h"

Hope this helps.

Upvotes: 1

Related Questions