Not Properly Passing Data Between Views

60 views Asked by At

I'm trying to send text generated by the user from the AuthenticationViewController to MainProfileViewController. I'm not receiving an error report. The label just doesn't even appear in the MainProfileViewController. The outlets are correctly hooked up. Thanks for the help!

    #import <UIKit/UIKit.h>

    @class MainProfileViewController;

    @interface AuthenticationViewController : UIViewController

    {
        MainProfileViewController *mainProfileViewController;
    }

    @property (retain, nonatomic) IBOutlet UITextField *usernameTextField;

    @end



    #import "AuthenticationViewController.h"
    #import "MainProfileViewController.h"

    @interface AuthenticationViewController ()

    @end

    @implementation AuthenticationViewController

    @synthesize usernameTextField;

    - (IBAction)createAccount: (id)sender {
             {
                mainProfileViewController = [[MainProfileViewController alloc] init];
                mainProfileViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
                mainProfileViewController.userText.text = usernameTextField.text;
                [self presentViewController:mainProfileViewController animated:YES completion:nil];
              }
    }

    @end



    #import <UIKit/UIKit.h>
    #import "AuthenticationViewController.h"

    @interface MainProfileViewController : UIViewController

    {
       UITextField *userText;
    }

    @property (retain, nonatomic) IBOutlet UILabel *resultLabel;
    @property (retain, nonatomic) IBOutlet UITextField *userText;
    @property (retain, nonatomic) NSString *textPass;

    @end




    #import "MainProfileViewController.h"
    #import "AuthenticationViewController.h"

    @interface MainProfileViewController ()

    @end

    @implementation MainProfileViewController
    @synthesize resultLabel, userText, textPass;

    - (void)viewDidLoad
{

    userText.text = textPass;
    [super viewDidLoad];

}
    @end
4

There are 4 answers

5
cjwirth On BEST ANSWER

OK, there's a few things you're doing wrong. First off, you're instantiating a local MainProfileViewController instead of instantiating the one that you have an ivar pointing to.

The second thing that you're doing wrong is trying to send over the view from the AuthenticationViewController to the MainProfileViewController. You shouldn't do that. Instead, pass the text itself; otherwise you are just overwriting pointers, and nothing will show up.

- (IBAction)createAccount: (id)sender {
    // DON'T CREATE A LOCAL VARIABLE
    // MainProfileViewController *controller = [[MainProfileViewController alloc] initWithNibName:@"MainProfileViewController" bundle:nil];
    mainProfileViewController = [[MainProfileViewController alloc] initWithNibName:@"MainProfileViewController" bundle:nil];
    mainProfileViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

    // DON'T TRY SENDING THE VIEW OVER
    // mainProfileViewController.userText = usernameTextField;
    mainProfileViewController.userText.text = usernameTextField.text;
    [self presentViewController:mainProfileViewController animated:YES completion:nil];
}

edit: I suppose it's not required that you have an ivar to the new view controller you want to present... But the point is that you cannot instantiate one, and then set the parameter on the one you did not instantiate (it will be nil).

0
slaveCoder On

Check your story board and see if you connected the Outlets properly. There should be an IBOutlet object in your view controller that you should connect in story board.

Also check if you connected the correct view controller class to the view controller in storyboard

1
Pradhyuman sinh On
#import <UIKit/UIKit.h>

@class MainProfileViewController;

@interface AuthenticationViewController : UIViewController

{
    MainProfileViewController *mainProfileViewController;
}

@property (retain, nonatomic) IBOutlet UITextField *usernameTextField;

@end



#import "AuthenticationViewController.h"
#import "MainProfileViewController.h"

@interface AuthenticationViewController ()

@end

@implementation AuthenticationViewController

@synthesize usernameTextField;

- (IBAction)createAccount: (id)sender {
         {
                MainProfileViewController *controller = [[MainProfileViewController alloc] initWithNibName:@"MainProfileViewController" bundle:nil];
                controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

                mainProfileViewController.textpass = usernameTextField.text;

                [self presentViewController:controller animated:YES completion:nil];
          }
}

@end



#import <UIKit/UIKit.h>
#import "AuthenticationViewController.h"

@interface MainProfileViewController : UIViewController

{
   UITextField *userText;
}

@property (retain, nonatomic) IBOutlet UILabel *resultLabel;
@property (retain, nonatomic) IBOutlet UITextField *userText;
@property (retain, nonatomic) NSString *textpass;
@end




#import "MainProfileViewController.h"
#import "AuthenticationViewController.h"

@interface MainProfileViewController ()

@end

@implementation MainProfileViewController
@synthesize resultLabel, userText;
- (void)viewDidLoad {
   usertext.text=textpass;
}
@end
1
Lutful Kabir On

You need to change your code on this part...

 - (IBAction)createAccount: (id)sender 
   {
       mainProfileViewController = [[MainProfileViewController alloc] init];
       mainProfileViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
       mainProfileViewController.userText.text = usernameTextField.text;
       [self presentViewController:mainProfileViewController animated:YES completion:nil];
   }

Happy Coding...