简体   繁体   中英

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?! 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!

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. I believe tags are int s.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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