【问题标题】:Not Properly Passing Data Between Views未在视图之间正确传递数据
【发布时间】:2013-12-10 06:08:18
【问题描述】:

我正在尝试将用户生成的文本从 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

【问题讨论】:

  • 你必须传递字符串

标签: objective-c uiviewcontroller message-passing


【解决方案1】:

好的,你做错了一些事情。 首先,您正在实例化 local 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……但关键是你不能实例化一个,然后在你没有实例化的那个上设置参数(它将为零)。

【讨论】:

  • 谢谢!这有助于清理代码。标签现在出现,但它不会更改为用户在 usernameTextField 中输入的内容。有问题吗?
  • 你是如何清理代码的?你有什么改变?您是否在 MainProfileViewController 的任何 -init、-viewDidLoad 或其他方法中设置了任何内容?当你 NSLog(@"%@", usernameTextField.text); 时会发生什么?
  • 好的,我认为你需要放慢速度,看看什么指向什么,什么时候指向什么。现在你正在使用你的 mainProfileViewController——这很好!然后你在 userText 上设置文本——也很好!然后,在 viewDidLoad(稍后调用)中,您将使用您从未设置过的 textPass 在 userText 上设置文本 - 覆盖您所做的更改。我认为使用调试器逐步完成此操作并检查变量会对您有很大帮助。
  • 明白了!非常感谢。告诉我停下来放慢速度思考有很大帮助。我必须创建 resultLabel.text = testPass;。我急于完成这件事,以至于我没有把事情想清楚。再次感谢!
  • :) 没问题。很多时候事情看起来应该有效,直到你真正放慢速度,看看发生了什么。我很高兴你能成功。
【解决方案2】:

检查您的故事板,看看您是否正确连接了 Outlets。 您的视图控制器中应该有一个 IBOutlet 对象,您应该在情节提要中连接它。

还要检查是否将正确的视图控制器类连接到情节提要中的视图控制器

【讨论】:

    【解决方案3】:
    #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
    

    【讨论】:

    • 所以现在标签出现了,但它不会更改为用户设置的文本。有问题吗?
    【解决方案4】:

    您需要在这部分更改您的代码...

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

    快乐编码...

    【讨论】:

    • 这有助于清理代码。标签现在出现,但没有更改为用户输入的内容。有问题吗?
    猜你喜欢
    • 2017-07-20
    • 2014-06-18
    • 1970-01-01
    • 2022-01-07
    • 2011-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多