将 NSString 从一个类传递到另一个类

发布于 2024-12-01 22:39:22 字数 988 浏览 0 评论 0原文

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

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

第一视图控制器.h

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

第一视图控制器.m

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

第二视图控制器.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

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.

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

firstViewController.h

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小矜持 2024-12-08 22:39:22

您正在创建 FirstViewController 的新实例...

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

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


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

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

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

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

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

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

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

You are creating a new instance of FirstViewController...

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

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


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

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

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文