开发者

toolbar button on one view controller but also showing on other

开发者 https://www.devze.com 2023-02-17 11:31 出处:网络
i have toolbar button on specific view controller and need to disappear at other view controllers. h开发者_如何学JAVAow can i do!! here is my code:

i have toolbar button on specific view controller and need to disappear at other view controllers. h开发者_如何学JAVAow can i do!! here is my code:


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //Initialize the toolbar
    toolbar = [[UIToolbar alloc] init];
    toolbar.barStyle = UIBarStyleDefault;
    //Set the toolbar to fit the width of the app.
    [toolbar sizeToFit];
    //Caclulate the height of the toolbar
    CGFloat toolbarHeight = [toolbar frame].size.height;
    //Get the bounds of the parent view
    CGRect rootViewBounds = self.parentViewController.view.bounds;
    //Get the height of the parent view.
    CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);
    //Get the width of the parent view,
    CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);
    //Create a rectangle for the toolbar
    CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight);
    //Reposition and resize the receiver
    [toolbar setFrame:rectArea];
    //Create a button
    UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] 
                                   initWithTitle:@"Info" style:UIBarButtonItemStyleBordered target:self action:@selector(info_clicked:)];
    [toolbar setItems:[NSArray arrayWithObjects:infoButton,nil]];
        //Add the toolbar as a subview to the navigation controller.
    [self.navigationController.view addSubview:toolbar];
    //Reload the table view
    [self.tableView reloadData];

}

- (void) info_clicked:(id)sender {
//Initialize the Info View Controller
    if(ivControllerToolbar == nil)
        ivControllerToolbar = [[InfoViewController alloc] initWithNibName:@"InfoView" bundle:[NSBundle mainBundle]];
    ivControllerToolbar.isViewPushed = NO;
    //Initialize the navigation controller with the info view controller
    if(infoNavController == nil)
        infoNavController = [[UINavigationController alloc] initWithRootViewController:ivControllerToolbar];
    //Present the navigation controller.
    [self.navigationController presentModalViewController:infoNavController animated:YES];
}

can i work with :

- (void)viewWillDisappear:(BOOL)animated {}
- (void)viewDidDisappear:(BOOL)animated {}

 

? and may i know how?


I don't know why you are creating your toolbar in viewWillAppear:. It is good to do your view hierarchy creation in viewDidLoad or loadView(If you are not using xib for view creation)

0

精彩评论

暂无评论...
验证码 换一张
取 消