将 NSString 从一个类传递到另一个类 [英] Pass NSString from one class to another

查看:68
本文介绍了将 NSString 从一个类传递到另一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里遇到了一些奇怪的问题,因为我无法将 NSString 从一个类传递到另一个类.我部署了与其他类相同的方法.

I have some weird issue here as somehow I am unable to pass NSString from one class to another. I deployed the same method that worked on other classes.

我正在尝试将字符串从 secondViewController 传递到我的 firstViewController.这就是我所做的.

I am trying to pass a string from secondViewController to my firstViewController. Here's what I did.

firstViewController.h

firstViewController.h

NSString *pickUpAddressString;
@property (nonatomic, retain) NSString *pickUpAddressString;

firstViewController.m

firstViewController.m

@synthesize pickUpAddressString;
-(void) viewWillAppear:(BOOL)animated {
NSLog(@"pickUpAddressString is %@", pickUpAddressString); // why it's null here?
    PickUpAddress.text = pickUpAddressString; // PickUpAddress is a UITextField
}

secondViewController.m

secondViewController.m

FirstViewController *controller = [[FirstViewController alloc]init];
    controller.pickUpAddressString = selectedAddress; // here, I set the string
    NSLog(@"selected address :%@\npickUpAddressString:%@", selectedAddress, controller.pickUpAddressString); // I had verified that both strings are valid.
    [self.navigationController popViewControllerAnimated:YES]; // pop to firstView

推荐答案

您正在创建 FirstViewController...

FirstViewController *controller = [[FirstViewController alloc]init];

...不同于(我假设)推送 SecondViewController 并且您通过 popViewControllerAnimated: 返回的原始实例.

...different from the original instance that (I'm assuming) pushed the SecondViewController and to which you are returning via popViewControllerAnimated:.

基本上,您需要将数据传回推送 SecondViewController 的控制器,在本例中为 FirstViewController.

Basically, what you need is to pass data back to the controller that pushed the SecondViewController, in this case, the FirstViewController.

也许,实现这一目标的最简单方法就是@Ladislav 在他的评论中所建议的:

Perhaps, the easiest way to achieve this is what @Ladislav suggested in his comment:

NSArray *viewControllers = [self.navigationController viewControllers];
FirstViewController *firstVC = [viewControllers objectAtIndex:[viewControllers count] - 2];

但是,请记住,这会在 SecondViewControllerFirstViewController 之间引入直接依赖关系.或者,换句话说,SecondViewController 现在与 FirstViewController 紧密耦合.

However, keep in mind that this introduces a direct dependency between SecondViewController and FirstViewController. Or, in other words, SecondViewController is now tightly coupled to FirstViewController.

简而言之,在将数据传递回层次结构时,最佳做法是使用松散耦合以避免视图控制器之间的直接依赖(紧耦合).这样做的好处包括代码可重用性和可测试性.为了实现松耦合,您需要为观察者定义一个通用接口(例如委托、通知等).

In a nutshell, when it comes to pass data back up the hierarchy, it is a best practice to use loose coupling to avoid direct dependencies between your view controllers (tight coupling). The benefits of doing so include code reusability and testability. In order to achieve loose coupling you need to define a generic interface for observers (e.g. delegation, notifications, etc).

还值得一提的是将状态信息放入模型对象中的重要性.避免将数据放入控制器中,除非是严格的表示数据.

It is also worth mentioning the importance of putting state information in model objects. Avoid putting data inside the controllers, unless it's strictly presentation data.

有关此主题的更多信息:什么是最佳沟通方式视图控制器之间?

More on this topic: What's the best way to communicate between view controllers?

这篇关于将 NSString 从一个类传递到另一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆