开发者

iPhone - backBarButtonItem is nil and does not highlight when touched

I have a fullscreen modalView called with :

PreferencesController *nextWindow = [[[PreferencesController alloc] initWithNibName:@"Preferences" bundle:nil] autorelease];
UINavigationController* navController = [[[UINavigationController alloc] initWithRootViewController:nextWindow] autorelease];
[self presentModalViewControl开发者_运维问答ler:navController animated:YES];

Then from this modalView I push another view :

MyController *nextWindow = [[[MyController alloc] initWithNibName:@"tmp" bundle:nil] autorelease];
[self.navigationController pushViewController:nextWindow animated:YES];

In this new controller, I have this viewDidLoad :

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"Borders";
    self.navigationController.navigationBarHidden = NO;
}

Doing like this, the backBarButtonItem is not active, I mean touching it does not highlight it nor goes back to the previous view.

In fact, self.navigationController.navigationItem.backBarButtonItem is NIL.

self.navigationController.navigationItem is not NIL

self.navigationController for caller and inside called view have the same reference.

What is the problem ?


Make sure that you're nextWindow UIViewController has reference to the UINavigationController.

Update #1

My current Application is a Tab Bar Application, so when i need to set the navigation controller reference i just let me new incoming View Controller referenced the navigation controller from the base class that controls the tabs.

i am sorry the previous answer i was totally wrong, i hope this would help you.

Update #2

Please try this before pushing the UIViewController:

UIButton* backButton = [UIButton buttonWithType:101]; 
[backButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; [backButton setTitle:Title forState:UIControlStateNormal]; 
UIBarButtonItem *iButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];     
self.navigationController.navigationItem.leftBarButtonItem = iButton;

Update #3

copy and paste definePreferences method to your project

 -(IBAction) definePreferences:(id)sender {

 PreferencesController *nextWindow = [[PreferencesController alloc] initWithNibName:@"Preferences" bundle:nil] ;
 nextWindow.caller = self;
 UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:nextWindow];
 [self presentModalViewController:navController animated:YES];
}

Update #4

  • I Added a NavigationBar in the Prefrences 2 Nib file
  • Then connected it with IBOutlet of type UINavigationItem in Prefrences 2 UIViewController
  • In ViewDidLoad: of Prefrences2 UIViewController: add the following code to ensure there is a left bar item is being added [1]
  • Then the function that is going to handle going back[2]

[1]

 -(void)viewDidLoad {
[super viewDidLoad];

UIButton* backButton = [UIButton buttonWithType:101];
[backButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

[backButton setTitle:@"Helo" forState:UIControlStateNormal];
UIBarButtonItem* iButton = [[UIBarButtonItem alloc] initWithCustomView:backButton]; 

self.oUINavigationItem.leftBarButtonItem = iButton;}

[2]

-(IBAction)buttonPressed:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

please let me know if you need me to explain more.


When you push view controller into navigation controller, it's backBarButtonItem will be set up for back to previous view controller, not leftBarButtonItem. If you set up leftBarButtonItem, the backBarButtonItem will be hide and the same place will be replaced by the leftBarButtonItem.

And your code doesn't set leftBarButtonItem at all, so of course it is nil.

EDIT: My test codes. I checked the backButtonItem and leftBarButtonItem by following codes, and they are nil. You are right. But when I touched the back button to previous view, its color did change, so it was highlighted. And it worked well back to previous view controller. So I think your code is wrong at some else place, it makes the back button inactive.

Listing 1, RootViewController class

// RootViewController.h 
#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController {
}
@end

// RootViewController.m
#import "RootViewController.h"
#import "MyViewController.h"

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];

self.title = @"RootView";

MyViewController *childView = [[[MyViewController alloc] init] autorelease];
UINavigationController *navCtrl = [[[UINavigationController alloc] initWithRootViewController:childView] autorelease];

[self.navigationController presentModalViewController:navCtrl animated:YES];
}
@end

Listing 2, MyViewController

// MyViewController.h
    #import <UIKit/UIKit.h>
@interface MyViewController : UIViewController {
}
@end

// MyViewController.m
#import "MyViewController.h"
#import "MyAnotherViewController.h"

@implementation MyViewController

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"MyView";

    NSLog(@"%s backBarButtonItem:%@ leftBarButtonItem:%@",__FUNCTION__ ,self.navigationItem.backBarButtonItem, self.navigationItem.leftBarButtonItem);
    MyAnotherViewController *childView = [[[MyAnotherViewController alloc] init] autorelease];
    [self.navigationController pushViewController:childView animated:YES];

}

@end

Listing 3, MyAnotherViewController

// MyAnotherViewController.h
#import <UIKit/UIKit.h>

@interface MyAnotherViewController : UIViewController {
}

@end

// MyAnotherViewController.m
#import "MyAnotherViewController.h"
@implementation MyAnotherViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"Another View";
}

@end


I have the final solution.
In fact, there is a problem when a new controller is used from a camera overlay to display a new view.
At some moment, the navigationBar does not work.
To make it work fine, you must call the first view not from self (the overlay) but from the ImagePicker itself. So you need to keep a reference to the picker on the overlay :

self.picker = [[UIImagePickerController alloc] init];

[some initialisation of the picker]

// Insert the overlay
self.overlay = [[OverlayViewController alloc] initWithNibName:@"Overlay" bundle:nil];
-- HERE --self.overlay.pickerRef = self.picker;
self.picker.cameraOverlayView = self.overlay.view;

[self presentModalViewController:self.picker animated:NO];

Then in the overlay :

- (IBAction) click:(id)sender {

    PreferencesController *nextWindow = [[[PreferencesController alloc] initWithNibName:@"Preferences" bundle:nil] autorelease];
    UINavigationController* navController = [[[UINavigationController alloc] initWithRootViewController:nextWindow] autorelease];

    -- DO --      [self.pickerRef presentModalViewController:navController animated:YES];
    -- DONT DO -- [self presentModalViewController:navController animated:YES];
}

Then no more navigation problem...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜