从一个视图控制器传递到另一个视图控制器

从一个视图控制器传递到另一个视图控制器

本文介绍了如何将 NSString 从一个视图控制器传递到另一个视图控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用两个视图控制器制作了一个非常简单的基于故事板的项目.

I made a very simple storyboard based project with two View Controllers.

我只想从 VC2 访问在 VC1 中声明的字符串.然后,第二个 VC 应在按下按钮时在文本字段中显示文本.

I want to simply access a string declared in VC1 from VC2. The second VC should then display the text in a textfield upon the press of a button.

我不想使用委托,一个用于全局数据的单独类全局变量和外部.相反,我读到使用对另一个 VC 的引用来实现变量共享很容易.

I do not want to use delegation, a separate class for global data or global variables and Extern. Instead, I read that it was easy to achieve variable sharing using a reference to one VC in the other.

对于我下面显示的代码,XCode 没有抱怨,但是我的问题是:第二个 VC 中的 NSLog 返回 null.

For my code shown below, XCode didn't complain, however my problem is this: The NSLog in the second VC returns null.

如果有人能告诉我如何修改代码以将字符串传递给第二个 VC/告诉我哪里出错了,我将不胜感激.

If anybody can tell me how to amend the code to pass the string to the second VC/ tell me where I'm going wrong I would appreciate it.

VC1 标头:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property NSString* textToPassToOtherVC;

VC1 实现:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize textToPassToOtherVC = _textToPassToOtherVC;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    _textToPassToOtherVC = @"Here is some text";
    NSLog (@"Text in VC1 is: %@", _textToPassToOtherVC);
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

@end

VC2 标头:

#import <UIKit/UIKit.h>

@class ViewController;

@interface ViewController2 : UIViewController

@property (nonatomic, strong) ViewController *received;

@property (strong, nonatomic) IBOutlet UITextField *textDisplay;

- (IBAction)textButton:(id)sender;

@end

VC2 实现:

#import "ViewController2.h"
#import "ViewController.h"

@interface ViewController2 ()

@end

@implementation ViewController2
@synthesize textDisplay;
@synthesize received;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

}

- (void)viewDidUnload
{
    [self setTextDisplay:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (IBAction)textButton:(id)sender {

    NSLog (@"Text in VC1 from VC2 is: %@", self.received.textToPassToOtherVC);

    textDisplay.text = self.received.textToPassToOtherVC;
}
@end

推荐答案

您的第二个视图控制器需要将其 received 属性设置为代表第一个视图控制器的值.

Your second view controller needs to have its received property set to a value that represents the first view controller.

对于基于故事板的项目,这通常在 prepareForSegue: 中完成.在执行到第二个视图控制器的 segue 之前,将在您的第一个视图控制器中调用此方法.

With storyboard-based projects, this is typically done in prepareForSegue:. This method will be called in your first view controller before the segue to your second view controller is performed.

(在我看来,最好只将字符串传递给第二个视图控制器而不是指向整个视图控制器的指针,因为这会减少依赖性并且是第二个视图控制器真正需要的唯一信息,但是让我们不要把事情复杂化.)

以下是我认为您需要执行此操作的步骤:

Here are the steps I think you need to get this working:

  • 在故事板中为从第一个视图控制器到第二个视图控制器的 segue 命名.对于此示例,我将其命名为 mySegue.
  • 在ViewController.m"中导入ViewController2.h" - 您的第一个视图控制器需要知道第二个视图控制器具有 received 属性.
  • 在你的第一个视图控制器中添加一个 prepareForSegue: 方法:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"mySegue"])
    {
        ViewController2 *targetVC = (ViewController2*)segue.destinationViewController;
        targetVC.received = self;
    }
}

希望这段代码在做什么很清楚.

Hopefully it's pretty clear what this code is doing.

这篇关于如何将 NSString 从一个视图控制器传递到另一个视图控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 00:58