-1

I need to pass data between two ViewControllers but without UIButton, in a few words, I need to access a variable which is in other ViewController. My code is:

LoginViewController *lvc;
NSString name=lvc.name;
Nischal Hada
  • 3,230
  • 3
  • 27
  • 57
Luis
  • 55
  • 1
  • 7
  • You wanted to access variable from LoginViewController? – iGatiTech May 20 '15 at 08:56
  • Provide more information about what you are trying to achieve and what you have done. – Vivek Molkar May 20 '15 at 08:58
  • I approved the edit by mistake, please don't approve it as the title is caps-locked. – Maroun May 20 '15 at 09:00
  • yes, I need to access a variable from LoginViewController. In LoginViewController I connect with a bd and I need to pass to other View some variables, for example, the name of Login... And I need do all of this without any button.@VivekMolkar @Gati – Luis May 20 '15 at 09:03
  • How you are moving to other ViewController without button click? – iGatiTech May 20 '15 at 09:05
  • I have a ViewController intermediate between the two views I want to pass data. @Gati – Luis May 20 '15 at 09:13
  • have you tried using a property? what is the problem with the curent code? – katzenhut May 20 '15 at 09:54
  • The problem with the code is when I am trying show name the value is null @katzenhut – Luis May 20 '15 at 10:02
  • Hey Luis, the value will be nil as you are just initialising the LoginViewController lvc.name. It is never retained! I suggest that you should first pass the data to the intermediate controller first and from there pass it on to the third controller. Better approach will be to save the value in a singleton class or in User Defaults if it is accessed frequently – Jen Jose May 20 '15 at 10:13
  • @Luis - ok. and what are you expecting name to be? where are you setting its value? are you even initialising the viewcontroller? or is lvc == nil, too? – katzenhut May 20 '15 at 10:23
  • In LoginViewController the code is: nombreUser=@"Jon"; and if I pass to the intermediate View the variable, the value is correct, but to the third View I do not know how i can do that without button @katzenhut – Luis May 20 '15 at 10:33
  • @Luis - why would having a button make any difference? if name is a property, it should be accessible from any object with a reference to lvc. and please check if lvc == nil. – katzenhut May 20 '15 at 10:56
  • lvc isn't equals to nil but lvc.name is equals to nil @katzenhut – Luis May 20 '15 at 11:21
  • do u want to pass variables between viewcontrollerls? – Mumthezir VP May 20 '15 at 11:24
  • I want pass the data of those variables. @mvp – Luis May 20 '15 at 11:29
  • have u tried passing variables, by creating object of the vewcontroller you want to pass? ex: viewcontrollerToPas .variable = a; – Mumthezir VP May 20 '15 at 11:32
  • Do you say something with that? ViewController *vc=[storyboard instantiateViewControllerWithIdentifier:@"HomeViewController"]; vc.nonameUser=name; @mvp – Luis May 20 '15 at 11:37
  • I have up voted urs hope to get it back in my answer too – Nischal Hada Jun 20 '15 at 14:02

3 Answers3

1

This specific case might be a little easier than delegates.

From what I see, you're trying to pass login credentials (name/login/password/something). I would use two things depending on the actual matter here.

Either NSUserDefaults or -performSegueWithIdentifier:

NSUserDefaults is a file that is loaded in every app that you can read and edit, simply using the following commands :

Setting a variable :

NSString *aName;
[[NSUserDefaults standardUserDefaults]setObject:aName forKey:@"userName"];

Getting a variable :

NSString *aName = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];

Note that you can save the following objects NSDictionary, NSArray, NSString, NSNumber, NSData, and probably a couple that I'm forgetting but someone can edit if I do.

Note that this file is loaded at every startup, so you don't wanna use that as a database but more of a small-sized storage easy to use, like for user name, preferences/settings, and stuff like that.

The other way is using performsegue between two controllers, but that requires storyboards.

Drag a segue between two of your controllers, name it (for example) fromLoginToHome. I'm assuming that the flow goes from the login controller to the home controller.

when you move between the two views (when the user presses "Login" for example), call this method

[self performSegueWithidentifier:@"fromLoginToHome" sender:self];

Then you'll need to implement this method, that is usually there but in a comment block (it's always like that when you create your Vc)

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if([segue.identifier isEqualToString:@"fromLoginToHome"]){

        HomeViewController *vc = (HomeViewController*)segue.destinationViewController;
        vc.myName = _myName;

      }
}
Gil Sand
  • 5,802
  • 5
  • 36
  • 78
0

Xcode using delegate to pass data between controllers This is for child to parent by usuing delegates

And For parent to child,you can use segues simply.

HTH!enjoy Coding.

Community
  • 1
  • 1
Naresh Reddy M
  • 1,096
  • 1
  • 10
  • 27
0

You can have a look of delegate method in here delegate. can you tell me if you are looking for delegate or not

Try using as below FirstViewController.h

@interface FirstViewController: UIViewController
- (void)GetItNow;

FirstViewController.m

- (void)GetItNow{
    NSLog(@"I acheived");    } 

  - (IBAction)goToSecondView:(id)sender {

    SecondViewController* Second= [[SecondViewControlleralloc] initWithNibName:@"SecondViewController" bundle:nil];
    rqVC.addId = self.addId;
    [self.view addSubview:Second.view];

}

SecondViewController.h

@property (nonatomic, assign) id delegate;

SecondViewController.m

- (IBAction)Action_LoadFunds:(id)sender {
    [self.view removeFromSuperview];
    [_delegate GetItNow];

}
Community
  • 1
  • 1
Nischal Hada
  • 3,230
  • 3
  • 27
  • 57