2

在完成与 UINavigationController 项目相关的问题后,我来发布我的确切问题.. 首先,我没有在MainWindow.xib我创建的文件中添加 UINavigationController 并在AppDelegate文件中添加 UINavigationController 之后在UIViewController(不是rootViewController)类中我必须添加Done类型的 rightbarbutton我在 searchbarDelegate 方法中添加它,显示为粗体:-

- (void) searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar {

//This method is called again when the user clicks back from teh detail view.
//So the overlay is displayed on the results, which is something we do not want to happen.

if(exhibDataObj.searching)
    return;

//Add the overlay view.
if(ovController == nil)
    ovController = [[OverlayViewController alloc] initWithNibName:@"OverlayView" bundle:[NSBundle mainBundle]];

CGFloat yaxis = self.navigationController.navigationBar.frame.size.height;
CGFloat width = self.view.frame.size.width;
CGFloat height = self.view.frame.size.height;

//Parameters x = origion on x-axis, y = origon on y-axis.
CGRect frame = CGRectMake(0, yaxis, width, height);
ovController.view.frame = frame;    
ovController.view.backgroundColor = [UIColor grayColor];
ovController.view.alpha = 0.5;

ovController.rvController = self;

[self.theTableView insertSubview:ovController.view aboveSubview:self.parentViewController.view];

exhibDataObj.searching = true;
letUserSelectRow = NO;
self.theTableView.scrollEnabled = NO;

//Add the done button.
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] 
                                          initWithBarButtonSystemItem:UIBarButtonSystemItemDone 
                                          target:self action:@selector(doneSearching_Clicked:)];
 }

但这永远行不通。

任何帮助都会更加明显。

4

1 回答 1

4

我已经快速测试了你的代码,它似乎对我来说工作正常,但是,我曾经遇到过同样的问题,后来发现我在 viewDidLoad 方法中将 rightBarButtonItem 设置为 nil 在将其设置为按钮后. 你应该确保你不会以某种方式犯同样的错误。

但是,如果这对您不起作用,您可以随时尝试为其设置自定义按钮:

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 5, 30, 30)];

button.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"done.png"]];

[button addTarget:self action:@selector(doneButtonClicked) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:button];

self.navigationController.visibleViewController.navigationItem.rightBarButtonItem = customItem;
于 2012-08-31T07:33:18.813 回答