从另一个视图控制器调用方法 iOS StoryBoard
我有一个带有按钮的表视图单元格,按下该按钮时需要调用不同视图控制器的方法。我已经遵循了这个教程,其中显示了在故事板中获取另一个视图控制器的一种方法,但它涉及对视图控制器的位置进行硬编码。每当我更改视图控制器的顺序时,我都需要更新我的代码,我知道这会搞砸。
这是他们的方法:
UITabBarController *tabBarController = (UITabBarController *)
self.window.rootViewController;
UINavigationController *navigationController =
[[tabBarController viewControllers] objectAtIndex:0];
PlayersViewController *playersViewController =
[[navigationController viewControllers] objectAtIndex:0];
playersViewController.players = players;
哎呀,那是什么?!我们想要将players数组分配给 PlayersViewController 的 Players 属性,因此它可以使用此数组 它的数据源。但应用程序委托对此一无所知 PlayersViewController 还没有,所以它必须挖掘 故事板找到它。这是故事板的局限性之一 我觉得很烦人。使用 Interface Builder,您始终可以 引用 MainWindow.xib 中的 App Delegate,您可以 建立从顶级视图控制器到插座的连接 应用程序委托。目前,故事板无法做到这一点。你 无法从顶级视图引用应用程序委托 控制器。这很不幸,但我们总能得到那些 以编程方式引用。
有人知道更清洁的方法吗?
我尝试在表视图单元格中创建一个 IBOutlet 到另一个视图控制器,但由于某种原因,我无法按住 Ctrl 键单击并拖动到另一个视图控制器。
我还尝试在其他视图控制器中设置按钮的 IBAction,但我需要知道单击了哪一行,以及我可以从 -(IBAction) addButtonClicked:(id)seder
获得的唯一信息是sender
信息,就是RectButton
。
谢谢!
I have a table view cell with a button that, when pressed, needs to call the method of a different view controller. I''ve followed this tutorial, which shows one way to get at another view controller in story boards, but it involves hardcoding the position of the view controllers. Anytime I'd change the order of my view controllers, I'd need to update my code, which I know I will mess up.
Here is their method:
UITabBarController *tabBarController = (UITabBarController *)
self.window.rootViewController;
UINavigationController *navigationController =
[[tabBarController viewControllers] objectAtIndex:0];
PlayersViewController *playersViewController =
[[navigationController viewControllers] objectAtIndex:0];
playersViewController.players = players;
Yikes, what is that?! We want to assign the players array to the
players property of PlayersViewController so it can use this array for
its data source. But the app delegate doesn’t know anything about
PlayersViewController yet, so it will have to dig through the
storyboard to find it. This is one of the limitations of storyboards
that I find annoying. With Interface Builder you always had a
reference to the App Delegate in your MainWindow.xib and you could
make connections from your top-level view controllers to outlets on
the App Delegate. That is currently not possible with storyboards. You
cannot make references to the app delegate from your top-level view
controllers. That’s unfortunate, but we can always get those
references programmatically.
Does anybody know a cleaner way?
I tried creating an IBOutlet in my table view cell to the other view controller, but I can't ctrl-click and drag to the other view controller for some reason.
I also tried setting the IBAction of my button in my other view controller, but I need to know what row was clicked, and the only information i can get from -(IBAction) addButtonClicked:(id)seder
is the sender
information, which is just RectButton
.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作为解决方法,您可以将 InterfaceBuilder 中
sender
的tag
属性设置为某个内容,然后只需比较[sender tag]
中的[sender tag]
代码>addButtonClicked:方法。我相信标签是int
。As a workaround, you could set the
tag
property of thesender
in InterfaceBuilder to something, and then just compare[sender tag]
in theaddButtonClicked:
method. I believe tags areint
s.