简体   繁体   中英

Do something when a view loads

I am making a quiz app and want it to have a 'witty remark' when it loads based on their score. For example...

Score 50 points : Good Job! Score 100 points : You Rock! Score 150 points : You're So Money!

And stuff like that. Any help is appreciated. Thanks!

Note: The last question of the quiz is a different .xib than the .xib with the witty remark. The witty remark will be displayed with a UILabel. And each time the user answers a question, it adds to their score. I would also appreciate help for this part as I am not quite sure that the way I am using is the best way to do it. Heres my code:

-(IBAction)a {

switch(questionNumber)
{
    case 0:
        question.text = @"This is question one";
        questionNumber = 1;
        myScore = myScore + 1;
        break;

    case 1:
        question.text = @"This is question two";
        answerA.text = @"Yes";
        answerB.text = @"No";
        [answerC setHidden:YES];
        [answerD setHidden:YES];
        [answerButton3 setHidden:YES];
        [answerButton4 setHidden:YES];
        questionNumber = 2;
        myScore = myScore = 2;
        break;

and so on for the other questions.

I don't think you need to extend a UIView to do this. You should be able to implement a UIViewController and use the viewDidLoad method to set the text into the UILabels at that time.

The steps would be: 1) Make a view-based XIB with the UILabel in it 2) Make a class that extends UIViewController with an IBOutlet for the UILabel 3) Add properties in the UIViewController for the score 4) Make the owning class of the XIB the extended UIViewController 5) Wire the UILabel to the UILabel outlet 6) Uncomment the viewDidLoad method in the extended UIViewController 7) Do your tests there 8) In the class that is loading the XIB and pushing it onto the view stack, set the score property in the new UIViewController (just after creation of the controller and before pushing it on the stack 9) Magical witty comments appear!

i think the best approch would be that u make a subclass of UIView which has one UILabel (displaying the Q) and four buttons for four options and two mthods and to dispaly the Q in right way (as I can guess that some of ur Q has 2 option and some have 4), and second method checks that the tapped answer is correct or not.

And make another subClass of NSObject which has few properties like

NSString* question;

NSArray* options;//[option count] = total button to dispaly

NSUInteger* correctOption;//index of correct answer

Now the first method in ur UIView subClass takes an object of this second class as an argument and display it accordingly;

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