将 NSString 从一个类传递到另一个类
我在这里遇到一些奇怪的问题,因为我无法将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在创建
FirstViewController
的新实例......与(我假设)推送
SecondViewController
的原始实例不同,并且您通过popViewControllerAnimated:
。基本上,您需要的是将数据传递回推送
SecondViewController
的控制器,在本例中为FirstViewController
。也许,实现这一点的最简单方法是 @Ladislav 在他的评论中建议的:
但是,请记住,这会在
SecondViewController
和FirstViewController
之间引入直接依赖关系。或者,换句话说,SecondViewController
现在与FirstViewController
紧密耦合。简而言之,当涉及到将数据传递到层次结构时,最佳实践是使用松耦合来避免视图控制器之间的直接依赖关系(紧密耦合)。这样做的好处包括代码可重用性和可测试性。为了实现松耦合,您需要为观察者定义一个通用接口(例如委托、通知等)。
还值得一提的是在模型对象中放入状态信息的重要性。避免将数据放入控制器中,除非它是严格的演示数据。
有关此主题的更多信息: 视图控制器之间通信的最佳方式是什么?
You are creating a new instance of
FirstViewController
......different from the original instance that (I'm assuming) pushed the
SecondViewController
and to which you are returning viapopViewControllerAnimated:
.Basically, what you need is to pass data back to the controller that pushed the
SecondViewController
, in this case, theFirstViewController
.Perhaps, the easiest way to achieve this is what @Ladislav suggested in his comment:
However, keep in mind that this introduces a direct dependency between
SecondViewController
andFirstViewController
. Or, in other words,SecondViewController
is now tightly coupled toFirstViewController
.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?