-2

Possible Duplicate:
iOS - Passing variable to view controller

I have two Viewcontrollers (ViewController1 and ViewController2) , And i want to pass a String value along with a IBAction from ViewController1 to ViewController2. I am trying to read the NSString (*check) value in ViewController2's ViewDidLoad method, but it's returning a null value.

Help me out and Thanks in advance.

// ViewController1

@interface ViewController1 : UIViewController {

        IBOutlet UIButton *btn1;
        IBOutlet UIButton *btn2;
        NSString *check;
    }
#import "ViewController.h"

-(IBAction)buttonClicked:(id)sender {

    check = [[NSString alloc] init];

    if (btn1.tag == 0) {
        check = @"foo";
        NSLog(@"%@",check);    
    }
    if (btn2.tag == 1) {
        check = @"bar";
        NSLog(@"%@",check);
    }
} 

in my second view controller ,

#import <UIKit/UIKit.h>

@class ViewController1;

@interface ViewController2 : UIViewController {

    ViewController1 *viewCon;
}

@property(nonatomic,retain) ViewController *viewCon;

//.m

#import "ViewController2.h"
#import "ViewController1.h"

@synthesize viewCon,

 - (void)viewDidLoad
{
    ViewController *myVC = [[ViewController alloc] init];
    NSLog(@"  Label  %@",myVC.check);
    [super viewDidLoad];
}
Community
  • 1
  • 1
BigAppleBump
  • 1,096
  • 12
  • 26
  • http://stackoverflow.com/questions/4998623/passing-nsstring-from-one-class-to-the-other , see duplicate of this.... :D :D: D – HarshIT Sep 15 '12 at 11:05

3 Answers3

2

Pass data from ViewController1 to ViewController2 using instance ofViewController2 followed by dot operator:

Create a property of NSString *strCheck in ViewController2 as u want to pass data to ViewController2.

Now in button's click event of ViewController1

-(IBAction)buttonClicked:(id)sender {
{

 .......
 .......

 ViewController2 *objViewController2 = [ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];
 objViewController2.strCheck = check; //ViewController1's value to ViewController2

}
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
2
-(IBAction)buttonClicked:(id)sender {

   //check = [[NSString alloc] init]; -- NO Need

    if (btn1.tag == 0) {
        check = @"foo";
        NSLog(@"%@",check);    
    }
    if (btn2.tag == 1) {
        check = @"bar";
        NSLog(@"%@",check);
    }

  ViewController2 *obj = [[ViewController2 alloc] initWithNibName:@"ViewController2"];
  [obj setCheck:check];
  //push to ViewController2
  [obj release];

}

In your second view controller,

#import <UIKit/UIKit.h>

@interface ViewController2 : UIViewController {

        NSString *check;
}
@property (nonatomic, retain) NSString *check;


//.m

#import "ViewController2.h"

@synthesize check;


 - (void)viewDidLoad
{
    NSLog(@"  Label  %@",check);
}
Hemang
  • 26,840
  • 19
  • 119
  • 186
1

See this link -
iOS - Passing variable to view controller .
I have already given answer for same thing.

You need to set variable's @property and @synthesized. For more check the link.

Community
  • 1
  • 1
TheTiger
  • 13,264
  • 3
  • 57
  • 82