-->

如何传递一个NSString从一个视图控制器到另一个?(How can I pass an NSSt

2019-09-20 19:25发布

我做了一个很简单的故事板基础的项目有两个视图控制器。

我想简单地访问从VC2 VC1中声明的字符串。 然后,第二VC应该显示在一个按钮的按压在一个TextField的文本。

我不想使用委派全局数据全局变量和extern一个单独的类 。 相反,我读了很容易使用对方一个VC的引用,实现变共享。

对于下面显示我的代码时,Xcode没有抱怨,但我的问题是这样在NSLog的第二VC返回null。

如果有人能告诉我如何修改代码来把这个字符串传给第二VC /告诉我,我要去哪里错了,我将不胜感激。

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

Answer 1:

您的第二视图控制器需要将其received属性设置为表示所述第一视图控制器的值。

基于故事板的项目,这一般是在做prepareForSegue: 此方法将在第一视图控制器执行SEGUE到第二个视图控制器之前被调用。

(在我看来,你会过得更好传递只是字符串你的第二个视图控制器,而不是一个指向整个视图控制器,因为这减少了依赖,是你的第二个视图控制器真正需要的唯一信息,但我们不要复杂的事情。)

下面是我想你需要得到这个工作的步骤:

  • 从你的第一个视图控制器在故事板你的第二个名字给SEGUE。 在这个例子中,我把它叫做mySegue
  • 导入“ViewController2.h”在“ViewController.m” -您的第一个视图控制器需要知道的是,第二个视图控制器具有received性能。
  • 添加prepareForSegue:像这样的方法在你的第一个视图控制器:

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

希望这是很清楚这是什么代码正在做什么。



Answer 2:

你的错误是基于性能的根本误解。 属性是用于处理getter和setter的样板和实施细则只是语法糖。 虽然设置_textToPassToOtherVC确实使物业回报值,它没有“通知”对方,因为它是参照“接收”的默认设置为零,也不会被你设置。

如果你真正把这个共享文本的价值之前的任何地方,你有几行:

ViewController *myVC1; 
ViewController2 *myVC2;

// ...some initialization.

myVC2.received = myVC1;
// Now the IBAction will display the correct text.

一切都将正常工作。



Answer 3:

尝试使用NSNotification。 当您需要发送的字符串,后与对象NSNotification!

当把要发送的字符串(你给的字符串,当然值后):

    yourString = @"Hello";

    [[NSNotificationCenter defaultCenter] postNotificationName:@"sendString" object:yourString];

在你的第二个视图控制器:

-(void)viewDidLoad:
{
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ThingYouWantToDoWithString:) name:@"sendString" object:nil];
}

- (void)ThingYouWantToDoWithString:(NSNotification *)notification{
        // Make sure you have an string set up in your header file or just do NSString *theString = [notification object] if your using ARC, if not allocate and initialize it and then do what I just said
        theString = [notification object];
        NSLog("%@", theString);
        [self performSelector:@selector(OtherThings:) object:theString];
}

希望这可以帮助!



Answer 4:

你检查你的self.received不为空/无?

在代码中设置断点,看是否self.received实际上是初始化(如果不是的话,那么这就是为什么你看不到任何东西):

- (IBAction)textButton:(id)sender {

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

    textDisplay.text = self.received.textToPassToOtherVC;
}


文章来源: How can I pass an NSString from one View Controller to another?