1

我想在我的应用程序中使用NavigationController的工具栏。这个工具栏的toolbarItems假设根据呈现的视图控制器而改变。这是非常基本的。uinavigationController与自定义项目的工具栏

我想要做的是使用UIBarButtonItem的“initWithCustomView:”方法将自定义按钮添加到工具栏。但是,该按钮不会显示在工具栏上。但是,如果我使用“initWithTitle:”或“initWithBarButtonSystemItem:”方法创建UIBarButtonItem,则会显示按钮。例如,请看下面的代码:

UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemBookmarks target:self action:nil]; 
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"edit" style:UIBarButtonItemStylePlain target:self action:nil]; 

NSArray *array = [[NSArray alloc] initWithObjects:item1, item2, nil]; 
[self setToolbarItems:array]; 

如果这样做,按钮显示在工具栏上。但是,如果我要执行以下操作:

UIButton* replyBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
[replyBtn setImage:[UIImage imageNamed:@"Reply_Message.png"] forState:UIControlStateNormal]; 
[replyBtn addTarget:self action:@selector(replyButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; 
replyBtn.frame = CGRectMake(10, 0, 40, 40); 
UIBarButtonItem *replyButton = [[UIBarButtonItem alloc] initWithCustomView:replyBtn]; 

UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemBookmarks target:self action:nil]; 
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"edit" style:UIBarButtonItemStylePlain target:self action:nil]; 

NSArray *array = [[NSArray alloc] initWithObjects:item1, replyButton, item2, nil]; 
[self setToolbarItems:array]; 

在此代码中,只有item1和item2显示在工具栏上。 replyButton不显示在工具栏上。假设按钮所在的地方有空白处。

如果在我创建的常规工具栏上使用相同的代码而不是NavigationController的工具栏,该按钮将显示出来。我正试图在整个应用程序中使用一个工具栏,以便与Apple的Mail应用程序具有相同的感觉。我需要使用“initWithCustomView:”方法的原因是因为其中一个图标是彩色的,这是它在常规工具栏上显示的唯一方式。现在,我浏览了苹果文档,并没有提到为什么不能调用“initWithCustomView:”方法(或者我找不到它)。

可以请某人在这个话题上闪耀一些光,帮助我指出正确的方向。在此先感谢你们。

+0

有相同的问题。我不知道为什么它不工作......我可能还需要添加一个不同的工具栏“添加子视图”。 – 2011-03-30 07:25:22

回答

1

我不能看到你尝试过什么不同,但它最终为我工作,与该代码:

////////////////////////////////////////////////////////////////////////////////////////////// 
/////>>>> Adding buttons for browsing in the toolbar of the popover's navigation controller/// 
////////////////////////////////////////////////////////////////////////////////////////////// 

//>>>>Create a goBack button  
goBack = [UIButton buttonWithType:UIButtonTypeCustom]; 
UIImage *goBackImage = [UIImage imageNamed:@"back1.png"]; 
[goBack setImage:goBackImage forState:UIControlStateNormal]; 
[goBack addTarget:self action:@selector(goBackClicked:) forControlEvents:UIControlEventTouchUpInside]; 
goBack.frame = CGRectMake(0,0,goBackImage.size.width,goBackImage.size.height); 
//Create a Bar button to hold this button 
UIBarButtonItem *goBackBarButton = [[[UIBarButtonItem alloc] initWithCustomView:goBack] autorelease]; 

UIBarButtonItem *flex1 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease]; 
UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRewind target:nil action:nil] autorelease]; 
NSArray *arrayOfButtons = [[[NSArray alloc] initWithObjects:goBackBarButton, flex1, addButton, nil] autorelease]; 
[self setToolbarItems:arrayOfButtons]; 

注意从你的一些差异(也许这也正是抓我?不敢肯定): 1.我的按钮没有在本地方法分配,但在类(你知道,房地产,综合等) 2.你

[replyBtn setImage:[UIImage imageNamed:@"Reply_Message.png"] forState:UIControlStateNormal]; 

其中m INE看起来有点不同

[goBack setImage:goBackImage forState:UIControlStateNormal]; 

尝试这些微小的变化,也许它会工作:)

0

我敢打赌,该项目没有显示,因为它的视图没有任何内容。你确定在你的项目中有一个叫做Reply_Message.png的图片吗? [UIImage imageNamed:@"Reply_Message.png"]可能是nil

+0

我对此很乐观。如果我采用相同的确切代码并将这些项目设置为我创建的工具栏,则会显示该按钮。这确实不是问题。我甚至试过一个UIButton只有一个标题,而不是图像,添加作为一个子视图到的UIBarButtonItem,甚至不会出现。 – Bittu 2010-07-28 03:29:09

0

您可以在整个应用程序中使用一个工具栏,而无需使用导航控制器工具栏。由于您的代码适用于非导航控制器工具栏,因此这可能是实现您想要的最简单的方法。在你的应用程序委托中,尝试添加一个工具栏到窗口,就像这样。这样它就贯穿整个应用程序。确保你考虑一个方案,让你访问工具栏添加/删除按钮,或隐藏/显示它从不同的视图控制器。

- (BOOL)application:(UIApplication *)application 
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

    /////// frame sizes //////// 
    // compensate for the status bar 
    int statusBarHeight = 20; 

    // toolbar 
    int toolbarWidth = window.frame.size.width; 
    int toolbarHeight = 30; // I think standard size is 30 
    int toolbarxOffset = 0; 
    int toolbaryOffset = window.frame.size.height - tHeight; 
    CGRect toolbarFrame = CGRectMake(toolbarxOffset, 
            toolbaryOffset, 
            toolbarWidth, 
            toolbarHeight); 

    /////// toolbar ////////////////////// 
    self.myPersistentToolbar = [[UIToolbar alloc] initWithFrame:toolbarFrame]; 
    [window addSubview:self.myPersistentToolbar]; 
} 
+2

谢谢你的回复德鲁。我早就想到了这个想法。如果我有一个使用navigationController作为它的rootViewController的应用程序,这可能已经奏效。相反,我使用这个Three20库的启动器作为根视图,每个“子应用程序”包含它自己的设置。有些在其中有tabbars,而另一些则有navigationController或splitviewController。由于这个原因,我试图避免向窗口添加任何东西。我想出的解决方案是将我自己的工具栏添加为navigationController视图的子视图。 – Bittu 2010-08-20 02:47:45

1

我有同样的问题。事实证明,UINavigationController的工具栏在每次将新视图压入堆栈时重置它的项目。我试图设置applicationDidFinish函数中的工具栏项目,并且它不工作。它工作了一次,我把工具栏设置在被推入导航堆栈的viewController的 - (void)viewDidAppear函数中。

所以,好像如果你想要导航控制器保持相同的工具栏项目在整个应用程序,你必须设置在你推到导航控制器出现视图后,每个视图工具栏项目。

我希望有帮助!