如何(重新)在多个视图控制器中使用相同的笔尖

时间:2010-01-08 17:32:36

标签: iphone cocoa-touch uiviewcontroller nib

好的,我只是输入了整个问题,然后设法将其删除。叹。无论如何,标准的披露,我到处搜索,并在键盘上敲我的头,无法弄明白。

我正在构建一个基于实用程序应用程序模板的基本应用程序(使用MainViewController和FlipsideViewController)。旁注:我仍然不明白MainView和FlipsideView在这个方案中的用途,所以如果有人可以解释它不会太糟糕:D

无论如何,在这两个视图的底部我想要一个工具栏。使用IB将它添加到给定视图很容易,但我希望工具栏有自己的控制器和模型,因为我希望在两个视图中保持其状态一致。因此,我想从笔尖加载该视图,但似乎我做错了。我遵循了这里的建议:NSViewController and multiple subviews from a Nib但显然已经把它搞砸了,所以更多的见解会受到赞赏。

我做了以下事情: 我创建了ToolBarViewController,它基本上是空的,但是ToolBar.xib的文件所有者。在ToolBar.xib中我有一个视图,其框架大小与工具栏相同,并且在工具栏内部。在MainView.xib中,我有一个相同大小的视图元素,它连接到下面代码中的toolBarView ...

在MainViewController.h中:

#import "ToolBarViewController.h"

@interface MainViewController : UIViewController <FlipsideViewControllerDelegate, MKMapViewDelegate> {
 ...
 ToolBarViewController *toolBarViewController;
 IBOutlet UIView *toolBarView;
}

...
@property(nonatomic, retain) ToolBarViewController *toolBarViewController;
@property(nonatomic, retain) IBOutlet UIView *toolBarView;

在MainViewController.m中:

@synthesize toolBarViewController;
@synthesize toolBarView;

- (void)loadView
{
    [super loadView];
    toolBarViewController = [[ToolBarViewController alloc] initWithNibName:@"ToolBar" bundle:nil];
    [[toolBarViewController view] setFrame:[toolBarView frame]];
    [[self view] replaceSubview:toolBarView with:[toolBarViewController view]];
}

当我构建并运行时,我得到上面最后一行的警告'UIView'可能无法响应'replaceSubview:with'并且在运行它时抛出以下异常: *** - [MainView replaceSubview:with:]:无法识别的选择器发送到实例0x450c540

有谁能解释我做错了什么?谢谢!A

2 个答案:

答案 0 :(得分:0)

那是因为没有这样的UIView的replaceSubview:with :(虽然在Mac上有一种类似于NSView的方法)。

请记住,Objective-C警告通常是错误;)一般来说,我倾向于永远不会留下他们没有治疗,我甚至将它们变成错误: http://akosma.com/2009/07/16/objective-c-compiler-warnings/

答案 1 :(得分:0)

没有[UIView replaceSubview:with:]这样的方法。这是一个NSView方法。你编译时应该收到警告。

要做同样的事情,您需要使用[[self view] insertSubview:aboveSubview:]然后使用[toolbarView removeFromSuperview]

那就是说,我不确定你是不是想要弄乱工具栏。我可能会尝试更多的东西:

self.toolbarItems = [toolbarViewController toolbarItems];
相关问题