在两个 UIViewController 之间传递变量的时机
不知道如何问这个问题,这样才有意义,但让我试试。我的应用程序打开一个 UIViewController,然后调用另一个。当它打开另一个时,它成功地将一个变量放入文本字段中。当用户关闭第二个 UIViewController 并返回到第一个 UIViewController 时,我传回变量并将其放置在文本字段中。至此就全部成功了。变量来回传递没有问题。
所以这就是我想要做的:返回到第一个 UIViewController 后,我运行一个查询语句,该语句使用文本字段中的变量作为从 SQLite 表中提取记录的键。我使用 NSLog 检查代码,发现文本字段为空,但是当 UIViewController 出现时,变量位于文本字段中。
到目前为止还和我在一起吗?我希望如此……
我正在 ViewDidLoad 中运行查询。我认为 ViewDidLoad 在将变量从第二个复制到文本字段之前正在运行。
我的主要问题是:我应该在 ViewDidLoad 中运行查询语句还是其他一些语句来获取文本字段中的变量。基本上,我想做的就是从另一个 UIViewController 返回时根据文本字段中的值提取一条记录。
谢谢你!
Not sure how to ask this question so it will makes sense, but let me try. My app opens a UIViewController, and then calls another one. When it opens the other one it places a variable in a textfield successfully. When the user closes the 2nd UIViewController and returns to the 1st UIViewController, I pass back the variable and place it in a textfield. This is all successful at this point. The variable is being passed back and forth with no problems.
So here is what I am trying to do: upon returning to the 1st UIViewController I run a query statement which uses the variable in the textfield as a key to pull a record from a SQLite table. I use the NSLog to check the code and I see that the textfield is empty, but when the UIViewController appears the variable is in the textfield.
Are with me so far? I hope so…
I am running the query in the ViewDidLoad. I am thinking that the ViewDidLoad is running before it copies the variable from the 2nd into the textfield.
My main question is: should I be running my query statement in the ViewDidLoad or some please else to get the variable in the textfield. Basically, all I want to do is pull a record based on the value in the textfield upon returning from another UIViewController.
Thank You!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从技术上讲,如果您的原始视图从未被释放,您的
viewDidLoad
将不会被调用。将调用的委托方法是viewWillAppear:
和viewDidAppear:
。您可以使用自定义的委托方法将值从第二个 viewController 传递回第一个 viewController。您应该记住的是,您应该使用通过委托方法传回的
NSString
进行查询,并在viewWillAppear:
期间将其放置在 textField 中。希望这有帮助!
Technically, your
viewDidLoad
won't be called if your original view was never dealloced. The delegate methods that will be called areviewWillAppear:
andviewDidAppear:
.You can pass the value from the second viewController back to the first one with a custom made delegate method. What you should keep in mind is that you should do the query with the
NSString
you pass back with the delegate method, and place it in the textField duringviewWillAppear:
.Hope this helps!
viewDidLoad
仅当视图控制器从笔尖加载时才会被调用。如果您从一个视图控制器切换回另一个视图控制器,则不会再次执行。尝试将代码放入viewWillAppear
中。viewDidLoad
is only called when the view controller is loaded up from a nib. If you are switching back to one view controller from another, it won't be executed again. Try putting the code inviewWillAppear
.