不正确地在视图之间传递数据

时间:2013-12-10 06:08:19

标签: objective-c uiviewcontroller message-passing

我正在尝试将用户生成的文本从AuthenticationViewController发送到MainProfileViewController。我没有收到错误报告。该标签甚至没有出现在MainProfileViewController中。插座正确连接。谢谢你的帮助!

    #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 个答案:

答案 0 :(得分:1)

检查您的故事板,看看您是否正确连接了奥特莱斯。 视图控制器中应该有一个IBOutlet对象,您应该在故事板中进行连接。

同时检查是否将正确的视图控制器类连接到故事板中的视图控制器

答案 1 :(得分:1)

#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

答案 2 :(得分:1)

好的,有一些事情你做错了。 首先,您要实例化本地 MainProfileViewController,而不是实例化您有ivar指向的那个。

你做错的第二件事是尝试将视图从AuthenticationViewController发送到MainProfileViewController。你不应该这样做。相反,传递文本本身;否则你只是覆盖指针,什么都不会出现。

- (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];
}

编辑:我想你不需要你想要呈现的新视图控制器的ivar ...但关键是你不能实例化一个,然后在你没有实例化的那个上设置参数(它将是零。

答案 3 :(得分:1)

您需要更改此部分的代码......

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

快乐编码......