Need simple Help:Changing UIViews

Discussion in 'iOS Development' started by gojohnnyboi, Apr 28, 2008.

  1. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    Ok, so what i need is pretty simple. I need to know the basics to simply change views, which im pretty sure a UITransitionView is for, however i'm not quite sure how to do this:



    Also, when making a second view, do i init it with my original rect?



    Anyone who helps i will appreciate very much.


    Thanks,
    Gojohnnyboi


    PS if theres a different way to just change from view to view, hoepfully with an animation like installer, let me know. Even without an animation. Thanks
  2. optimo

    optimo New Member

    Joined:
    Sep 28, 2007
    Messages:
    535
    Likes Received:
    17
    simple changes can be handled with UIView, which you should already have for most apps. You can add UITransitionView on top of the main UIVIew to enable fancier transition animations and such. You call transition method with the to- and from-view names and the transition type, the views have to be alloc'd already proper

    Code:
    UITransitionView *transitionView [ [ UITransitionView alloc ] initWithFrame: [U]viewRect[/U] ];
    [mainView addSubView: transitionView ];
    
    [ transitionView transition: 0  fromView:oldView  toView:newView ];
    
    transition: can be 0 - 9 for diff trans types (0 is instant). You could do all this from a delegate override method

    I'm sure there is a better example out there
  3. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    UITransitionView is great. Don't forget that you can make them any size (think about the little album previews in iTunes). Fading views in and out can make for a well polished app, so don't be afraid to add UITransitionViews on top of your main UITransitionView.

    The code Optimo posted is absolutely correct. My advice though would be to add that into a separate function, that way you can call it outside of the class it's implemented in.

    Example, much abridged (using Optimo's provided code):
    Code:
    @implementation theAppController
    
        //within its definition:
    
        UITransitionView *transitionView [ [ UITransitionView alloc ] initWithFrame: viewRect ];
        [mainView addSubView: transitionView ];
    
    - (void) useTransition:(int)transitionType fromView:(UIView*)oldView toView(UIView*):newView{
        [transitionView transition:transitionType  fromView:oldView  toView:newView ];
    }
    
    @end
    Within another object, when you need to change the view:
    Code:
        [theAppController useTransition:transitionType fromView:currentView toView:newView];
  4. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    EDIT: ok, with your

    - (void) useTransition

    Please Register or Log in to view images

    int)transitionType fromView

    Please Register or Log in to view images

    UIView*)oldView toView(UIView*):newView{
    [transitionView transition:transitionType fromView

    Please Register or Log in to view images

    ldView toView:newView ];
    }


    i get a compiler error. syntax before (

    so i could use help with that. Also, when i add the transView to my mainView, everything in the view freezes. you see, i ahve a UIPreferencesTable and a UINavigationBar, but... i cant press anything when i add this to the view!

    Help much appreciated. Thanks for the help so far. once i "spread some rep" i will rep u!


    Thanks,
    Gojohnnyboi
  5. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Of course you can't press anything. You're pasting the UITransitionView on top of your UIPreferencesTable instead of the other way around.

    Also, you are defining your UIPreferencesTable in the app controller, which you shouldn't do, it should be in its own set of files. You should then take and either make your mainView the UITransitionView, or paste a UITransitionView over the mainView. Then you can call UITransitionView to transition to the UIPreferncesTable using the function that I outlined in my last post.

    Speaking of which, we can't tell you how to fix the errors if you don't tell us what they are.
  6. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    EDIT: Got the transView and the prefs table worked out now, made a seperate set of files just for the prefstable and imported it.


    NOW, the remaining problem is calling the transitionView on the selection of a cell. already know how to work with the selections that easy, but the problem is that code you gave me. i get a compiler error and therefore cannot call the transition when my cell is selected...


    EDIT 2:

    Sorry, didn't list error again.

    Also, when i set it (to test) to switch views on the app launch, nothing i have added to the new view appears. Dont know why.


    Thanks Skylar!!!!!!!

    Gojohnnyboi
  7. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    EDIT: I'm not going to spoonfeed you. Look at the function name, the problem is obvious. The cause of the problem, I typed too quickly and didn't proofread that line.
  8. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55

    i think i basically fixed it:



    Code:
    - (void)transition:(int)transitionType fromView:(UIView*)oldView toView:(UIView*)newView
    {
    [transitionView useTransition:1 fromView:mainView toView:secondView];
    }
    


    but when i call:

    [transitionView useTransition:1 fromView:mainView toView:secondView];


    from the cell selection, the app crashes! and also my second view is really called secondView, so it shoudl work.


    Anyone?
  9. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Did you already create the view to which you are transitioning?

    EDIT: mainView is wrong. If you set this up as you hinted that you did, mainView is wrong.

    EDIT: You are passing the function a value called newView, but you are not using it, instead, you pull the variable secondView out of thin air. That is also wrong. Change newView to secondView, or change secondView to newView.
  10. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55

    Yes, like so:

    UIView* secondView;
    being already in the header file...

    Code:
    secondView = [[[UIView alloc] initWithFrame: rect] autorelease];

Share This Page