开发者

calling functions in other classes when UIButton is hit

开发者 https://www.devze.com 2022-12-11 20:18 出处:网络
I\'m using a UITabBarController to keep track of different views for an app. In one view, the \"quiz controller\", I have the user select numbers from segmented controls and then I store their answers

I'm using a UITabBarController to keep track of different views for an app. In one view, the "quiz controller", I have the user select numbers from segmented controls and then I store their answers in an integer array. At the bottom is a 'submit' button which, when hit, shou开发者_运维百科ld call a function in another view to display results using the "results controller". My question is, how do I handle class methods to allow the results to to be analyzed and set UILabels in the "results controller" when called by the "quiz controller"?

My code for hitting the submit button looks like this:

[ResultViewController calculateAndDisplayScores];

// swtich to results view
mainDelegate.tabBarController.selectedIndex = 2;

where "ResultViewController" is the name of my "results controller". When I do this, ResultViewController gives me an error which I think says I can't set a UILabel text property in a class method.


You don't really want a class method. You want some way to notify the particular instance of ResultViewController that's living in your tab bar controller to update itself. The compiler is complaining at you because you're calling your method on the class, instead of a particular instance.

The simplest, ugliest way to do that, based on the code that you've posted, would seem to be:

[(ResultViewController*)[mainDelegate.tabBarController.viewControllers objectAtIndex:2] calculateAndDisplayScores];

// switch to results view
mainDelegate.tabBarController.selectedIndex = 2;

But the right way to do this would probably be to have some kind of model object(s) where the information lives. Your data entry UI communicates with the model to get the current data, and push back the user's edits. The model can then notify any interested parties (like your results view controller) that new data is available, and that they should update themselves accordingly. That notification might be accomplished though a delegate relationship, by using the NSNotificationCenter, or some other way you dream up.

(For the sake of argument, assume that this really is a class method. Classes in Objective-C don't really have state, so how would a class method keep the results, or update your UI?)

0

精彩评论

暂无评论...
验证码 换一张
取 消