1

How can i pass one string variable from my firstviewcontroller class to secondviewcontroller class? here secondviewcontroller class have only ".m" and ".h" files; currently in secondviewcontroller ".h" file

@property(nonatomic,retain) NSString *value2;

in secondviewcontroller.m file i am using

NSString *str=value2;
NSLog(@"%@",str);

// it prints a null 

in my firstviewcontroller.m file am assigning like

secondviewcontroller *second=[[secondviewcontroller alloc]init];
second.value2=value1;

value1 is declared in firstviewcontroller as string ..please tell me one solution

iPatel
  • 46,010
  • 16
  • 115
  • 137
Naveen
  • 1,251
  • 9
  • 20
  • 38

5 Answers5

4

Try to assign your value1 like.

firstviewcontroller.m

NSString *value1 = [[NSString alloc] initWithString:@"my value1"];

and then assign vc.value2 = value1;

βhargavḯ
  • 9,786
  • 1
  • 37
  • 59
1

Generally logic is :

NOTE : Data type of both value1 and value2 must be same.

First put in secondVC.h file

NSString *value2;

give its @property and @synthesize properly.

In your firstVC.m

NSString *value1 = @"this is string"; // in your case check it is, proper or not (i mean  nil/not nill)??

and at create Object of secondVC

secondVC *vc = [[secondVC alloc] init];
vc.value2 = value1;
[self presentModalViewController:vc animated:YES];

and write in secondVC.m file

NSLog(@"%@",value2); 

and check out put in console.

iPatel
  • 46,010
  • 16
  • 115
  • 137
0

Most probable reason appears here is you have not synthesized the property in second view controller's .m file. Unless you synthesize the property it will not initiate and will be nil. Please post the entire code.

You need to declare a variable for the property and synthesize the property so that it assigns the value to the variable.

Let me know if it is not so.

Murtuza Kabul
  • 6,438
  • 6
  • 27
  • 34
0

synthesized your propery and used it with self. value2.

Hope this will solve your problem.

All the best !!!

Yashesh
  • 1,799
  • 1
  • 11
  • 29
0

ok this is my First View Controller it has xib file

.h

#import "SecondViewController.h"
@interface ViewController : UIViewController

@property(strong, nonatomic) SecondViewController * svc;

@end

and second with no xib.

.h

#import <UIKit/UIKit.h>
@class ViewController;

    @interface SecondViewController : UIViewController

    @property (strong, nonatomic) ViewController * vc;

    @property (strong, nonatomic) NSMutableString * passValue;


    @end

in implementation

first .m is viewDidLoad

- (void)viewDidLoad
{
     NSLog(@"viewDidLoad");

    [super viewDidLoad];

    svc=[[SecondViewController alloc] init];

    svc.vc=self;

    NSString * str= svc.passValue;

    NSLog(@"%@",str);

    svc.passValue =[@"StringFromFirstView" mutableCopy];

    [svc viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.
}

and in Second .m

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        passValue = @"mySValue";
        // Custom initialization
    }
    return self;
}


- (void)viewDidLoad
{

    [super viewDidLoad];

    NSLog(@"passValue %@",passValue);

    // Do any additional setup after loading the view.
}

and when i Run the code

    2013-03-16 13:11:40.178 ali[787:c07] viewDidLoad
2013-03-16 13:11:40.179 ali[787:c07] mySValue
2013-03-16 13:11:40.180 ali[787:c07] passValue StringFromFirstView
meth
  • 1,887
  • 2
  • 18
  • 33