Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
417 views
in Technique[技术] by (71.8m points)

ios - iPhone toolbar for entire app

I have my app base on navigationController. So i set the toolbar visible for some views and for others i didnt calling setToolbarHidden:NO or YES. first question, this goes in viewWillAppear method ?

Then in my appDelegate, I put one item on the toolbar but is not being show. can someone show me how can I use delegate protocol here so each view know what to do when a item is pressed??

my code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    //create itemViewcontroller
    EventosViewController *itemsViewController = [[EventosViewController alloc] init];

    //create UINavigationcontroller, stack only contains itemviewcontroller
    navController=[[UINavigationController alloc] initWithRootViewController:itemsViewController];
    //navController will retain itemviewcontroller, we can release it
    [itemsViewController release];

    UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] 
                                    initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                    target:self
                                    action:@selector(pressButton1:)];
    //Use this to put space in between your toolbox buttons
    UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] 
                                 initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                 target:nil
                                 action:nil];
    //Add buttons to the array
    NSArray *items = [NSArray arrayWithObjects: systemItem1, flexItem,nil];

    //release buttons
    [systemItem1 release];
    [flexItem release];

    //add array of buttons to toolbar
    [navController.toolbar setItems:items animated:NO];

    //set navController's view in window hierarchy
    [[self window] setRootViewController:navController];
    [navController release];

    // Override point for customization after application launch.
    [self.window makeKeyAndVisible];

    return YES;
}

thx in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The toolbar us hidden by default. The toolbarItems should be stored in the respective view controller, not the navigation controller.

From the documentation:

Displaying a Toolbar
In iOS 3.0 and later, navigation controller objects make it easy to provide a custom toolbar for each screen of a navigation interface. The navigation controller object now manages an optional toolbar in its view hierarchy. When displayed, this toolbar obtains its current set of items from the toolbarItems property of the active view controller. When the active view controller changes, the navigation controller updates the toolbar items to match the new view controller, animating the new items into position when appropriate.

The navigation toolbar is hidden by default but you can show it for your navigation interface by calling the setToolbarHidden:animated: method of your navigation controller object. If not all of your view controllers support toolbar items, your delegate object can call this method to toggle the visibility of the toolbar during subsequent push and pop operations.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...