导航栏上的按钮无法加载

时间:2011-06-24 07:55:27

标签: iphone ios4 uibutton uinavigationbar uibarbuttonitem

我想在导航栏上添加按钮但是没有在栏上加载。请帮助。以下是我的代码。请告诉我我哪里出错了。我使用的是景观

- (void)viewDidLoad {
    [super viewDidLoad];    
    CGRect rect = CGRectMake(0.0, 0.0, 480.0, 32.0);
    navBar = [[UINavigationBar alloc] initWithFrame:rect];
    navBar.items = self.navigationController.navigationBar.items;
    navBar.delegate = self;
    //[navBar release];
    navBar.tintColor =[UIColor blackColor]      

    UIBarButtonItem * rightButton = [[UIBarButtonItem alloc]initWithTitle:@"Photo"       style:UIBarButtonItemStyleBordered target:self action:@selector(takePhoto)];

    UIBarButtonItem * backButton = [[UIBarButtonItem alloc]initWithTitle:@"Camera List" style:UIBarButtonItemStyleBordered target:self action:@selector(cancel:)];

    self.navigationItem.rightBarButtonItem = rightButton;   
    self.navigationItem.leftBarButtonItem = backButton;

    //navBar.topItem.rightBarButtonItem  = rightButton; 
//  navBar.topItem.leftBarButtonItem = backButton;

    [self.view addSubview:navBar];

    [backButton release];
}

1 个答案:

答案 0 :(得分:1)

试试这个

(void)viewDidLoad { 
    [super viewDidLoad];
    CGRect rect = CGRectMake(0.0, 0.0, 480.0, 32.0); 
    navBar = [[UINavigationBar alloc] initWithFrame:rect]; 
    navBar.items =   self.navigationController.navigationBar.items; 
    navBar.delegate = self; 

    UINavigationItem *navigationItem = [UINavigationItem alloc];
    [navBar pushNavigationItem:navigationItem animated:NO];
    [navigationItem release];

    UIBarButtonItem * rightButton = [[UIBarButtonItem alloc]initWithTitle:@"Photo" style:UIBarButtonItemStyleBordered target:self action:@selector(takePhoto)];

    UIBarButtonItem * backButton = [[UIBarButtonItem alloc]initWithTitle:@"Camera List" style:UIBarButtonItemStyleBordered target:self action:@selector(cancel:)];

    self.navigationItem.rightBarButtonItem = rightButton;
    self.navigationItem.leftBarButtonItem = backButton;

    [self.view addSubview:navBar];

    [backButton release]; 
    [rightButton release];
}