将字符串从 tableviewcontroller 传递到导航堆栈中的 viewcontroller

发布于 2024-09-03 10:29:33 字数 239 浏览 1 评论 0原文

我应该如何将字符串传递到选择 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 技术交流群。

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

发布评论

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

评论(2

三生池水覆流年 2024-09-10 10:29:33

实际上,我们已经通过两种方式完成了此操作(使用 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

甜心 2024-09-10 10:29:33

这些解决方案中的任何一个都是可以接受的。我认为如果你只传递一个字符串,那么一个属性就足够了。我一直在为更复杂的对象保存 init 方法。这是我最近为“高分”表所做的一个,当您点击一行时,会在堆栈上显示配置文件视图:

ProfileController *profileController = [[ProfileController alloc] initWithNibName:@"ProfileController" bundle:nil];
// pass the controller the player object
[profileController showProfile:player];
// show it by pusing it on the stack
[self pushViewController:profileController animated:YES];
[profileController release];

我可以创建另一个初始化程序,例如

ProfileController *profileController = [[ProfileController alloc] initWithPlayer:player];

。这看起来更优雅一点,但正如我所说,你的任何方法都可以。

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:

ProfileController *profileController = [[ProfileController alloc] initWithNibName:@"ProfileController" bundle:nil];
// pass the controller the player object
[profileController showProfile:player];
// show it by pusing it on the stack
[self pushViewController:profileController animated:YES];
[profileController release];

I could create another initializer like

ProfileController *profileController = [[ProfileController alloc] initWithPlayer:player];

instead. That is a little bit more elegant looking, but as I said, any of your approaches would be fine.

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