Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

Switching UIViewControllers programmatically ?

peterverpeterver Posts: 25Registered Users
edited February 2012 in iPhone SDK Development
Hey guys, i'm working on an app where on the home screen i got several buttons and a newsfeed.

The newsfeed is a custom horizontal control i wrote which has swipe gestures ( next/previous ) and a tap gesture. This feed only displays one item.

This custom control is built in a UIView class and has been made completely in code ( it's also added to my main Home view through code ). The newsfeed component and the buttons are staged inside of a uiview, which is staged inside a UIViewController.

Now i'm trying to make it so that when i tap one of the news items i go to a different controller ( navigation controller ). However i can't seem to figure out how to do this ? Anyone able to help me on this one ?

Cheers & thanks from a beginning xcoder/objective-c programmer :) !
Post edited by peterver on

Replies

  • fhsjaagshsfhsjaagshs Posts: 93Registered Users
    edited February 2012
    Hello, and welcome to objective-c and iPhone development


    First, you need to make a new UIViewController subclass in Xcode

    Second, you must include the newly created UIViewController's header file in the original (the one with the swipe controls) UIViewController's header or main file.

    Third, you must call this code to present the view controller
    // presenting or "pushing"
    UIViewController *myViewController = [[UIViewController alloc] init];
    [self.navigationController pushViewController:myViewController animated:YES];
    
    // Call this from the pushed view controller to go back to the original view controller
    [self.navigationController popViewControllerAnimated:NO];
    
    
    <b><b>My apps</b></b><br />
    <a href="http://itunes.apple.com/us/app/rest-counter/id464985741?ls=1&mt=8" target="_blank">Rest Counter - Count bars of rest in orchestra with ease!!!</a><br />
    <a href="http://itunes.apple.com/us/app/swiftload/id4697629
  • fiftysixtyfiftysixty Posts: 310Registered Users
    edited February 2012
    peterver wrote: »
    Hey guys, i'm working on an app where on the home screen i got several buttons and a newsfeed.

    The newsfeed is a custom horizontal control i wrote which has swipe gestures ( next/previous ) and a tap gesture. This feed only displays one item.

    This custom control is built in a UIView class and has been made completely in code ( it's also added to my main Home view through code ). The newsfeed component and the buttons are staged inside of a uiview, which is staged inside a UIViewController.

    Now i'm trying to make it so that when i tap one of the news items i go to a different controller ( navigation controller ). However i can't seem to figure out how to do this ? Anyone able to help me on this one ?

    Cheers & thanks from a beginning xcoder/objective-c programmer :) !

    It seems you have a slight misunderstanding: UINavigationController is a so called container view controller, meaning that it manages other view controllers. It does not have "a view of its own" so to speak, meaning that UINavigationController only displays the navigation bar, the rest of the view is filled with the view controller currently displayed by the navigation controller. This all may sound a bit complicated, but it really isn't that difficult once you get the hang of it.

    Now, onto your question. The UINavigationController handles the kind of tree-like navigation structure that you have for example in the mail app: list of accounts -> an account -> list of emails -> an email. If that is the navigation structure you want for your app, then you need a UINavigationController as the root view controller of your app. After that, you push and pop your view controllers into the navigation controller.

    However, if you don't want that kind of navigation structure, you have other options. If you just need to open a new view in response to a user selection, you can use a modal view. First, implement a new view controller with the stuff you want to display, and then call
    - (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion
    

    and pass your view controller there. You can control the way the new view is animated using the modalTransitionStyle -property.

    Working with view controllers is such as basic element of iOS apps that I really urge everybody to get intimate with the iOS View Controller Programming Guide. Doing so will pay off really handsomely. There are really so many ways to implement things that you need to have a good grasp of the foundations before going on and starting writing your custom stuff.
    <a href="http://www.fiftysixtysoftware.com" target="_blank">Fifty Sixty Software</a><br />
    <a href="http://www.fiftysixtysoftware.com/blog" target="_blank">iPhone development tips and tutorials</a><br />
    <br />
    <b>Apps in store:</b><br />
    <a href="htt
  • peterverpeterver Posts: 25Registered Users
    edited February 2012
    coming from a flex background this is all new to me, so thanks guys ! this really helped me out, i know that i'm but a beginning objective-C coder but i find myself learning more and more every day :D !

    In a couple of weeks i'll be giving back to the community in the same way that you are doing for me :)
  • fiftysixtyfiftysixty Posts: 310Registered Users
    edited February 2012
    peterver wrote: »
    coming from a flex background this is all new to me, so thanks guys ! this really helped me out, i know that i'm but a beginning objective-C coder but i find myself learning more and more every day :D !

    In a couple of weeks i'll be giving back to the community in the same way that you are doing for me :)

    There is so much to learn, and Apple keeps improving the frameworks and adding totally new ones, not to mention improving and adding stuff to the toolchain and language (Objective-C, that is). You'll never run out of things to learn :)
    <a href="http://www.fiftysixtysoftware.com" target="_blank">Fifty Sixty Software</a><br />
    <a href="http://www.fiftysixtysoftware.com/blog" target="_blank">iPhone development tips and tutorials</a><br />
    <br />
    <b>Apps in store:</b><br />
    <a href="htt
Sign In or Register to comment.