将字符串从 tableviewcontroller 传递到导航堆栈中的 viewcontroller
我应该如何将字符串传递到选择 tableviewcell 时推送的视图控制器。 我应该在视图控制器中创建自定义 init 方法吗?例如 [[myvc alloc]initWithURL:...]
设置属性?例如 [myvc setURL:...]
或 myvc.url = ...
或者只是创建一个自定义方法? [myvc setLoadingURL:...]
How should I pass a string to the view controller that gets pushed when a tableviewcell is selected.
Should I create a custom init method in the view controller? eg [[myvc alloc]initWithURL:...]
Set a property? eg [myvc setURL:...]
or myvc.url = ...
or just create a custom method? [myvc setLoadingURL:...]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,我们已经通过两种方式完成了此操作(使用 init 和属性)。我发现该属性是最好的方法,因为如果 ViewController 是由 InterfaceBuilder 创建的,则可能不会调用自定义 init 方法。对于该属性,如果您想使用它,您总是被迫对其进行设置。
只需 0.02 美元,
-担
We've actually done this both ways (with an init and a property). I found the property the best method because the custom init method may not be called if the ViewController is created by InterfaceBuilder. With the property, you are always forced to set it if you desire to use it.
Just $0.02,
-dan
这些解决方案中的任何一个都是可以接受的。我认为如果你只传递一个字符串,那么一个属性就足够了。我一直在为更复杂的对象保存 init 方法。这是我最近为“高分”表所做的一个,当您点击一行时,会在堆栈上显示配置文件视图:
我可以创建另一个初始化程序,例如
。这看起来更优雅一点,但正如我所说,你的任何方法都可以。
Any of those solutions would be acceptable. I think if you're just passing it one string, that a property would be sufficient. I've been saving init methods for more complex objects. Here's one I did recently for a "high scores" table that when you tapped a row, showed a profile view on the stack:
I could create another initializer like
instead. That is a little bit more elegant looking, but as I said, any of your approaches would be fine.