iPhone - backBarButtonItem为零,触摸时不突出显示

时间:2011-03-20 01:41:34

标签: iphone uinavigationcontroller uibarbuttonitem back-button null

我有一个名为的全屏modalView:

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

然后从这个modalView我推出另一个视图:

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

在这个新控制器中,我有这个viewDidLoad:

- (void)viewDidLoad {
    [super viewDidLoad];

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

这样做,backBarButtonItem不活跃,我的意思是触摸它不会突出显示它,也不会回到上一个视图。

实际上,self.navigationController.navigationItem.backBarButtonItem是NIL。
self.navigationController.navigationItem不是NIL
调用者和内部调用视图的self.navigationController具有相同的引用。

有什么问题?

3 个答案:

答案 0 :(得分:1)

确保您 nextWindow UIViewController引用了UINavigationController。

更新#1

我当前的应用程序是一个标签栏应用程序,所以当我需要设置导航控制器参考时,我只是让我新的传入视图控制器从控制标签的基类引用了导航控制器。

对不起之前的回答我完全错了,我希望这会对你有帮助。

更新#2

请在推送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;

更新#3

将definePreferences方法复制并粘贴到您的项目

 -(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];
}

更新#4

  • 我在Prefrences 2 Nib文件中添加了NavigationBar
  •  
  • 然后将它与Prefrences 2 UIViewController中的UINavigationItem类型的IBOutlet连接
  • 在ViewDidLoad中:Prefrences2 UIViewController:添加以下代码以确保添加了一个左栏项目[1]
  • 然后将要处理的函数返回[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];
}

如果您需要我解释更多,请告诉我。

答案 1 :(得分:0)

当您将视图控制器推入导航控制器时,它的backBarButtonItem将被设置为返回上一个视图控制器,而不是leftBarButtonItem。如果设置leftBarButtonItem,则backBarButtonItem将隐藏,并且相同的位置将被leftBarButtonItem替换。

你的代码根本没有设置leftBarButtonItem,所以当然它是零。

编辑:   我的测试代码。我通过以下代码检查了backButtonItem和leftBarButtonItem,它们是零。你是对的。但当我触摸前一个视图的后退按钮时,它的颜色确实发生了变化,因此它被突出显示。它可以很好地回到以前的视图控制器。所以我认为你的代码在其他地方是错误的,它使后退按钮无效。

清单1,RootViewController类

// 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

清单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

清单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

答案 2 :(得分:0)

我有最终的解决方案 事实上,当从相机覆盖使用新控制器来显示新视图时会出现问题 在某些时刻,navigationBar不起作用 要使其正常工作,您必须调用第一个视图,而不是从自身(叠加层)调用,而是从ImagePicker本身调用。因此,您需要在叠加层上保留对选择器的引用:

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];

然后在叠加层中:

- (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];
}

然后没有更多导航问题......