-5

I have multiple string value in one .m file and i want to access these file to another .m file to use value in file.

  • You should refer this answer http://stackoverflow.com/a/22934200/3883040. – Sushil Sharma Oct 15 '15 at 09:55
  • 1
    Possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – pkc456 Oct 15 '15 at 10:58

2 Answers2

1

if you wants to pass data from ViewControlerOne to ViewControllerTwo try these..

do these in ViewControlerOne.h

 @property (nonatomic, strong) NSString *str1;

do these in ViewControllerTwo.h

 @property (nonatomic, strong) NSString *str2;

Synthesize str2 in ViewControllerTwo.m

@interface ViewControllerTwo ()
@end
@implementation ViewControllerTwo
@synthesize str2;

do these in ViewControlerOne.m

 - (void)viewDidLoad
 {
   [super viewDidLoad];

  // Data or string you wants to pass in ViewControllerTwo..
  self.str1 = @"hello world";

 }

on the buttons click event do this..

-(IBAction)ButtonClicked
{ 
  //Navigation on buttons click event from ViewControlerOne to ViewControlerTwo with transferring data or string..
  ViewControllerTwo *objViewTwo=[self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerTwo"];
  objViewTwo.str2=str1;
  [self.navigationController pushViewController: objViewTwo animated:YES];
}

do these in ViewControllerTwo.m

- (void)viewDidLoad
{
 [super viewDidLoad];
  NSLog(@"%@",str2);
}
krushnsinh
  • 874
  • 1
  • 10
  • 11
  • in button clicked event could you tell me please what is "obj"?obj.str2=str1; – harsh rajput Oct 15 '15 at 09:57
  • i updated that line..it is object of ViewControllerTwo.. – krushnsinh Oct 15 '15 at 10:03
  • I got exception when i hit on button ---Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Storyboard () doesn't contain a view controller with identifier 'CustomerDetailViewController'' – harsh rajput Oct 15 '15 at 10:13
  • @harshrajput show your code. – Sushil Sharma Oct 15 '15 at 10:38
  • refer these links for giving storyboard Id..http://stackoverflow.com/questions/15478411/how-do-i-give-an-identifier-to-a-view-controller-within-my-storyboard and http://stackoverflow.com/questions/8295471/storyboard-doesnt-contain-a-view-controller-with-identifier – krushnsinh Oct 15 '15 at 10:40
  • - (IBAction)CheckAvailablity:(id)sender { CustomerDetailViewController *objViewTwo=[self.storyboard instantiateViewControllerWithIdentifier:@"CustomerDetailViewController"]; objViewTwo.getData=_goData; [self.navigationController pushViewController: objViewTwo animated:YES]; } – harsh rajput Oct 15 '15 at 10:48
  • @Sushil on the click event of button i write this code and no segue link on storyboard.Than is shows this error.And also i have no storyboard id set. – harsh rajput Oct 15 '15 at 10:56
  • If you are using storyboard, you need to set storyboardID for your `CustomerDetailViewController`. Follow the link in @krushnsinh's comment. – Sushil Sharma Oct 15 '15 at 11:01
  • you gave Navigation Controller to your first view controller??and set storyboard id..In my code second view controllers name is ViewControllerTwo and its storyboard id is also ViewControllerTwo.. – krushnsinh Oct 15 '15 at 11:01
  • Thanks this code works but in my case . – harsh rajput Oct 15 '15 at 11:33
-1

With the following two ways you can achieve this,
1) You can create public property in .h file.
2) Can also use delegate or notification to pass data between controllers.