Niccolò Canepa
Niccolò Canepa

Reputation: 1

random UIView switcher

I have three different views in my iPhone app, and when the user presses a button, I need the view to change to either of the three views randomly. I've found this code, but i don't know how to load images..sorry but i'm newbie....!!

- (IBAction) yourBtnPressed : (id) sender
{
    int i = arc4random() % 3;

    if(i == 1)
    {
        //load first view
    }
    else if(i == 2)
    {
        //load second view
    }
    else
    {
        //load third view
    }
}

Upvotes: 0

Views: 58

Answers (2)

Giuseppe Garassino
Giuseppe Garassino

Reputation: 2292

If your views are in a .xib file you can use this code:

[self.view addSubview:aView];

If you think to create your views programmatically, then:

UIView *aView = [[UIView alloc] initWithFrame:aFrame];
// add contents to your aView here
[self.view addSubview:aView];
[aView release]; // if you don't need it anymore

Upvotes: 0

user679424
user679424

Reputation:

if view are all in the same viewcontroller you can simply write

self.view = firstView;

or for more cool transition you can use these method + transitionWithView:duration:options:animations:completion: + transitionFromView:toView:duration:options:completion:

see apple documentation: http://developer.apple.com/library/IOs/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html

Upvotes: 1

Related Questions