views:

37

answers:

1

hi . i was playng around with an idea. in a single lable in a view when the view appears a random number is displayed. presently i have aview, a lable and a button. press button random number is generated and the lables title is replaced with a random number....random number displayed.

what i wish to acheive is that there is no button, just a view and a text lable and when the view appears a random number is displayed also instantaneously with out the need for a button to prompt generation. here isi the code i have so far

import "Mainview.h"

@implementation Mainview - (IBAction)pushstart {

NSString *title = nil;
NSString *path = nil;

int Number = arc4random() % 12;
switch(Number) {
                case 0:
                namel.text = @"1";

                break;
      case 1:
                namel.text = @"2";

        break;

    case 2:
                namel.text = @"3";

                break;

    case 3:
                namel.text = @"4";

                break;

    case 4:
                namel.text = @"5";

                break;
    case 5:
        namel.text = @"6";

        break;

    case 6:
        namel.text = @"7";

        break;

    case 7:
        namel.text = @"8";

        break;

    case 8:
        namel.text = @"9";

        break;



    case 9:
        namel.text = @"10";

        break;

    case 10:
        namel.text = @"11";

        break;

    case 11:
        namel.text = @"12";

        break;


                        break;
                default:
                break;
}

} @end

+1  A: 

Here is the way shorter answer, this method goes in whatever class is controlling the view (same place as the current action method):

- (void) awakeFromNib {
    srandom(time(NULL));
    [namel setIntValue: random %12];
}
theMikeSwan
Or use arc4random(). No need to seed it.
Don