0

How should I pass a string to the view controller that gets pushed when a tableviewcell is selected. 我应该在视图控制器中创建一个自定义的 init 方法吗?例如[[myvc alloc]initWithURL:...] 设置属性?例如[myvc setURL:...],或者myvc.url = ... 只是创建一个自定义方法?[myvc setLoadingURL:...]

4

2 回答 2

0

这些解决方案中的任何一个都是可以接受的。我认为如果你只是传递一个字符串,那么一个属性就足够了。我一直在为更复杂的对象保存 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];

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

于 2010-05-24T17:27:45.000 回答
0

我们实际上已经以两种方式完成了(使用 init 和属性)。我发现该属性是最好的方法,因为如果 ViewController 是由 InterfaceBuilder 创建的,则可能不会调用自定义的 init 方法。使用该属性,如果您想使用它,您总是被迫设置它。

只需 0.02 美元,-

于 2010-05-24T17:38:26.480 回答