将字符串从tableviewcontroller传递给导航堆栈中的viewcontroller
问题描述:
我应该如何将一个字符串传递给在选中tableviewcell时被推入的视图控制器。 我应该在视图控制器中创建一个自定义的init方法吗?例如[[myvc alloc]initWithURL:...]
设置属性?例如[myvc setURL:...]
或myvc.url = ...
或者只是创建一个自定义的方法? [myvc setLoadingURL:...]
将字符串从tableviewcontroller传递给导航堆栈中的viewcontroller
答
我们实际上已经完成了这两种方式(使用init和property)。我发现该属性是最好的方法,因为如果ViewController是由InterfaceBuilder创建的,则可能不会调用自定义init方法。随着财产,你总是被迫设置它,如果你想使用它。
只需$ 0.02,
-dan
答
任何这些解决方案都是可以接受的。我想如果你只是传递一个字符串,那么一个属性就足够了。我一直在为更复杂的对象保存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];
代替。这看起来更优雅一些,但正如我所说,你的任何方法都可以。