How can I get a UINavigationBar to work?

Discussion in 'iOS Development' started by ubunix, Sep 6, 2008.

  1. ubunix

    ubunix Member

    Joined:
    Aug 6, 2008
    Messages:
    637
    Likes Received:
    1
    Device:
    iPhone 5 (Black)
    Say I want to add a UINavigationBar to gojonnyboi's SimpleWindowApp. How would I go about doing that? Everytime I try to compile it I get like 20 errors?
  2. Gitykins

    Gitykins Banned

    Joined:
    May 16, 2008
    Messages:
    3,760
    Likes Received:
    1
    Is it not just the default UINavigationBarDefaultBackground.png in the other.artwork?
  3. ubunix

    ubunix Member

    Joined:
    Aug 6, 2008
    Messages:
    637
    Likes Received:
    1
    Device:
    iPhone 5 (Black)
    what?
  4. Gitykins

    Gitykins Banned

    Joined:
    May 16, 2008
    Messages:
    3,760
    Likes Received:
    1
    sh*t lol, I clicked on this from the front page and thought you were asking how to change the navigation bar.

    ^.^
  5. Nickll9009

    Nickll9009 New Member

    Joined:
    Sep 19, 2007
    Messages:
    1,367
    Likes Received:
    8
    Device:
    iPod touch
    In you objective-c source code (.m) file -


    navBar = [ [ UINavigationBar alloc ] initWithFrame: CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, 45.0f)];
    [ navBar setDelegate: self ];
    [ navBar pushNavigationItem: [ [ UINavigationItem alloc ] initWithTitle

    Please Register or Log in to view images

    "AppName" ] ];
    [ mainView addSubview: navBar ];

    In your header (.h) file -

    UINavigation* navBar;
  6. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    create an instance of a UINavigationBar in your application, and add it to your view you wish to see it on. say you want a navigation bar in your UIView "mainView":

    Code:
    UINavigationBar* theBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 48.0f)];
    [theBar pushNavigationItem:[[UINavigationItem alloc] initWithTitle:@"This Title"]];
    [mainView addSubview: theBar];
  7. ubunix

    ubunix Member

    Joined:
    Aug 6, 2008
    Messages:
    637
    Likes Received:
    1
    Device:
    iPhone 5 (Black)
    Do I add the UINavigationItem to my header file also?
  8. Nickll9009

    Nickll9009 New Member

    Joined:
    Sep 19, 2007
    Messages:
    1,367
    Likes Received:
    8
    Device:
    iPod touch
    Read my post 2 up

    also add

    Code:
    #import <UIKit/UINavigationBar.h>
  9. ubunix

    ubunix Member

    Joined:
    Aug 6, 2008
    Messages:
    637
    Likes Received:
    1
    Device:
    iPhone 5 (Black)
    Thank you, I got it to work!

    Another question why wont this work

    Code:
    {
    	if(button == 0)
    		[self terminate];
    	}
  10. Nickll9009

    Nickll9009 New Member

    Joined:
    Sep 19, 2007
    Messages:
    1,367
    Likes Received:
    8
    Device:
    iPod touch
    im me - nickroxursox88
    ------------------double post merged------------------
    Nevermind, don't IM me i'll help everyone here -

    main.m-
    Code:
    #import <UIKit/UIKit.h>
    #import "NavBarApp.h"
    
    int main(int argc, char *argv[])
    {
    	NSAutoreleasePool* pool = [ [ NSAutoreleasePool alloc ] init ];
    	int AppReturn;
    	AppReturn = UIApplicationMain( argc, argv, @"NavBarApp", @"NavBarApp" );
    	return AppReturn;
    	[ pool release ];
    }
    NavBarApp.h-
    Code:
    #import <Foundation/Foundation.h>
    #import <CoreFoundation/CoreFoundation.h>
    #import <UIKit/UIKit.h>
    #import <UIKit/UIHardware.h>
    #import <UIKit/UIWindow.h>
    #import <UIKit/UIView.h>
    #import <UIKit/UINavigationBar.h>
    
    @interface NavBarApp : UIApplication
    {
    	UIWindow* window;
    	UIView* mainView;
    	CGRect rect;
            UINavigationBar* navBar;
    }
    
    - (void)applicationDidFinishLaunching:(id)unused;
    - (void)dealloc;
    
    @end
    NavBarApp.m-
    Code:
    #import "NavBarApp.h"
    
    @implementation NavBarApp
    
    - (void)applicationDidFinishLaunching:(id)unused
    {
    	
    	rect = [ UIHardware fullScreenApplicationContentRect ];
    	rect.origin.x = 0.0f;
    	rect.origin.y = 0.0f;
    	
    	window = [ [ UIWindow alloc ] initWithContentRect: rect ]; 
    	[ window makeKeyAndVisible ]; 
    	
    	mainView = [ [ UIView alloc ] initWithFrame: rect ];
    	
    	[ window setContentView: mainView ];
    
            navBar = [ [ UINavigationBar alloc ] initWithFrame: CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, 45.0f)];
           [ navBar setDelegate: self ];
           [ navBar pushNavigationItem: [ [ UINavigationItem alloc ] initWithTitle:@"NavBar Example" ] ];
           [ navBar showLeftButton:nil withStyle:nil rightButton:@"Respring" withStyle: 0 ];
           [ mainView addSubview: navBar ];
    	
    }
    
    - (void)dealloc
    {
         [ window release ];
         [ mainView release ];
         [ navBar release ];
         [ super dealloc ];
    }
    
    - (void)navigationBar:(UINavigationBar*)bar buttonClicked:(int)button
    {
               system("killall SpringBoard");
    }
    
    
    
    @end
    Makefile-
    Code:
    CC=arm-apple-darwin9-gcc
    LD=$(CC)
    LDFLAGS=	-lobjc \
    				-framework UIKit \
    				-framework Foundation \
    				-framework CoreFoundation \
    				-framework CoreGraphics \
    				-L"/usr/lib" \
    				-F"/System/Library/Frameworks" \
    				-F"/System/Library/PrivateFrameworks" \
    				-bind_at_load
    CFLAGS=-I/var/include
    App=NavBarApp
    
    all:	$(App)
    
    $(App):	main.o NavBarApp.o
    	$(LD) $(LDFLAGS) -o $@ $^
    		ldid -S $(App)
    
    
    %.o:	%.m
    		$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
    
    clean:
    		rm -f *.o $(App)
    There ya go, plain and simple.

    Here's a screenie of the app:

    Please Register or Log in to view images



    Also i've uploaded the source code in a .zip as well as the .app.

    Attached Files:

Share This Page