开发者

What to do when UIBarButtonItem are not visible in UINavigationBar?

Hello Everyone I am writing a simple code in ViewDidLoad, and class is child of UITableViewController, like below but buttons are not visible, while title is visible,

Another thing that I am clicking a button which in ViewDidLoad method ViewController.m, and which is call a method, code of that method is as below

//Code of button target method

-(void)statusMethod {
    NSLog(@"statusMethod");
    Status *ob=[[Status alloc]init];
    [self presentModalViewController:ob animated:YES];

}

//Code of ViewDidLoad of Status.m
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, width, 43)];
    navBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    [self.view addSubview:navBar];
    [navBar release];

UIBarButtonItem *home = [[UIBarButtonItem alloc] initWithTitle:@"HOME" style:UIBarButtonItemStylePlain target:self action:@selector(homeMethod)];
self.navigationItem.rightBarButtonItem = home;

UIBarButtonItem *add = [[UIBarButtonItem alloc] initWithTitle:@"Add" style:UIB开发者_运维问答arButtonSystemItemAdd target:self action:@selector(addMethod)]; 
self.navigationItem.leftBarButtonItem = add;


UILabel *label = [[UILabel alloc] initWithFrame:
                  CGRectMake(10,10,width-20,25)];
label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
label.text = @"Status";
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont systemFontOfSize:25];
label.textAlignment = UITextAlignmentCenter;
[navBar addSubview:label];
[label release];

Any Idea, what is problem in code? I you don't understand, then u can ask me again, I will praise, if I will get solution of my problem


It seems like self.navigationItem is only looked at by the UINavigationController (controller, not bar). The bar itself isn’t going to check that.

So you should either

1) Use a real UINavigationController which comes with its own navigationBar and will handle the navigationItems for you. The self.navigationItem code above will work in that case. (you should consider self.navigationItem.title = @"Status";)

If your UITableViewController is going to be pushing stuff on and off the navigation stack, this is the path you should take in any case.

2) Use the UINavigationBar’s own navigation item. Except it doesn’t seem to come with a navigation item, so you have to add your own:

UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:@"Status"];

[navBar setItems:[NSArray arrayWithObject:navItem]];
[navItem release];

and then set the left and right buttons as

[navBar topItem].leftBarButtonItem = add;
[add release];

[navBar topItem].rightBarButtonItem = home;
[home release];

Dealing with UINavigationController/UINavigationBar/UINavigationItem can be confusing, but luckily Apple has a decent explanation of how all these things work together at the top of its UINavigationController documentation.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜