简体   繁体   English

从另一个View Controller iOS StoryBoard调用方法

[英]Call Method From Another View Controller iOS StoryBoard

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?! kes,那是什么?! We want to assign the players array to the players property of PlayersViewController so it can use this array for its data source. 我们想将players数组分配给PlayersViewController的players属性,以便它可以将此数组用于其数据源。 But the app delegate doesn't know anything about PlayersViewController yet, so it will have to dig through the storyboard to find it. 但是应用程序委托者对PlayersViewController尚不了解,因此必须在情节提要中进行挖掘才能找到它。 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. 使用Interface Builder,您始终可以在MainWindow.xib中引用App Delegate,并且可以建立从顶级视图控制器到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. 我尝试在表视图单元格中向另一个视图控制器创建IBOutlet,但是由于某些原因,我无法按住Ctrl键单击并拖动到另一个视图控制器。

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 . 我还尝试在其他视图控制器中设置按钮的IBAction,但我需要知道单击了哪一行,并且我可以从-(IBAction) addButtonClicked:(id)seder获得的唯一信息是sender信息,即只是RectButton

Thanks! 谢谢!

As a workaround, you could set the tag property of the sender in InterfaceBuilder to something, and then just compare [sender tag] in the addButtonClicked: method. 解决方法是,可以在InterfaceBuilder中将sendertag属性设置为某种值,然后在addButtonClicked:方法中比较[sender tag] I believe tags are int s. 我相信标签是int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM