click event on navigation bar ?

Discussion in 'iOS Programming' started by chhoda, Jun 4, 2009.

  1. chhoda macrumors 6502

    Joined:
    Oct 25, 2008
    #1
    Hi All,

    I have a navigation based app with a view and a table view on top of that. I need to come to table's first row on tapping the navigation bar. How can I achieve that ?

    --CH
     
  2. johnnyjibbs macrumors 68030

    johnnyjibbs

    Joined:
    Sep 18, 2003
    Location:
    London, UK
    #2
    All UITableViews inherit UIScrollView's scrollsToTop property. Set this to yes and the table will automatically scroll to the top when the user taps the status bar.

    If you want to extend that functionailty to the navigation bar as well, you could intercept the touch events from UIResponder, which the navigation bar (as a view) inherits.

    Use the touchesBegan method from UIResponder to get the touch event, then scroll the table to the top using the method
    Code:
    [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] withRowAnimation:UITableViewRowAnimationTop];
    or something like that.

    Try reading the Apple documentation on UIResponder, UIEvent and UITouch for more information on handling touch events and gestures. The touchesBegan etc methods give you the UIEvent, which you can use to get the UITouch object pointer. They have methods for deciding which view needs to respond, etc.
     
  3. chhoda thread starter macrumors 6502

    Joined:
    Oct 25, 2008
    #3
    scrollstotop did not help

    Thanks,
    scrollstotop did not work

    The problem is, I created the app from a navigation based app template. So the top bar is a vavigation controller. Its view is probably the table view. Or default navigation controllers' view is something else ?

    How can I know what view corresponds to that navigation controller ? [top 20 - 30 height pixels !]

    --CH
     
  4. johnnyjibbs macrumors 68030

    johnnyjibbs

    Joined:
    Sep 18, 2003
    Location:
    London, UK
    #4
    The navigation controller manages the navigation hierarchy. In a normal navigation-based app it will have a series of view controllers which will pop onto the stack as and when you navigate through them. As you progress forward through the hierarchy, view controllers will be added and initialised, and dealloc'd when you go back through the navigation hierarchy to the root view controller. However, view controllers that you have navigated from will not be dealloc'd, but instead stacked up in terms of the number of views deep.

    I think you may have to link up the navigation bar to the navigationItem property of the view controller using Interface Builder (control click the file owner in IB and then drag to the mouse to the navigation bar and select navigationItem).

    This will ensure that the current view controller's navigation item property corresponds to your navigation bar. Then you can do anything navigation bar-specific from within each view controller, such as add bar buttons (e.g. save, done or cancel buttons) or track touches.

    I think the navigation bar is 320x44 pixels in size in portrait mode.

    Also, check out the navigation controller object itself, which I think keeps a reference to the navigation bar item itself (the actual bar view). Apple's documentation is a good place to read more about this.
     
  5. chhoda thread starter macrumors 6502

    Joined:
    Oct 25, 2008
    #5
    again

    Thanks a lot for simple and detail description, I hope apple documentation was that simple !

    The way I had been capturing touchend events is create my own class inherited from the class intended and use it instead of IB. The downside is that I will hv to program each and everything on that approach. And I override touchend method in my class and handle things from there.

    but here, navigationcontroller is set up for me. [I would also like to know create a nav based app from scratch without using nav based application template, but that's later]. How can I get a touch event for navigationcontroller.navigationBar ?

    http://www.iphonedevsdk.com/forum/i...der-uicontrol-hijacking-touches-question.html

    is this link helpful ? i am still reading !

    --ch
     
  6. chhoda thread starter macrumors 6502

    Joined:
    Oct 25, 2008
    #6
    anyone ?

    Is there a way I can register my navigation bar for getting notification to a function of my choice when it is clicked ?

    --CH
     
  7. johnnyjibbs macrumors 68030

    johnnyjibbs

    Joined:
    Sep 18, 2003
    Location:
    London, UK
    #7
    You could just stick an invisible button over the top of it. But you should be able to intercept the touches from it using the touchesBegan, etc methods of UIResponder. I think there's a touchInView method somewhere which allows you to specify which view is responding so that you could have a condition that means it only performs some code when the nav bar is touched.

    Remember the responder chain goes through all the views and subviews touched, the view controllers and finally the application so you don't need to subclass everything neccessarily.

    Sorry not to be more specific but I'm not at my Mac and don't have access to any code at the moment. But the above should work. And an invisible UIButton is a simple hack.
     

Share This Page