首页 文章

在可可中使用绑定时,更改未反映在视图中

提问于
浏览
1

我正在创建一些示例应用程序来理解可可中的视图导航,绑定等概念 . 这是一个场景:我有一个窗口,在MainMenu.Xib中有一个标签视图(2个标签) . 我在第一个标签中有一个文本字段,在第二个标签中有标签 . 我希望它们都反映相同的值,我想使用绑定来做到这一点 . 另外,我不想使用提供给我的视图以及选项卡视图 .

这些是我所做的步骤 .

使用以下代码在applicationDidFinishLaunching:方法中单独设置每个选项卡视图项的视图:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application

    //initialize view controllers 
    view1=[[ViewTab1 alloc] initWithNibName:@"ViewTab1" bundle:nil];
    view2=[[ViewTab2 alloc] initWithNibName:@"ViewTab2" bundle:nil];


   //set views
    [[[myTabView tabViewItems] objectAtIndex:0]setView:view1.view];
    [[[myTabView tabViewItems] objectAtIndex:1]setView:view2.view];    


}
  • myTabView是AppDelegate中MainMenu.xib的选项卡视图的出口参考 .

  • ViewTab1是第一个视图控制器(和xib)的名称 .

  • ViewTab2是第二个视图控制器(和xib)的名称 .

ViewTab1有一个单独的文本字段(和一个关联的标签) . 我已经将它绑定到AppDelegate中声明的变量(名称) . ViewTab2有一个标签 . 我也将它绑定到AppDelegate中的同一个变量 .

变量“name”在AppDelegate的init方法中初始化 .

AppDelegate.h

....
NSString *name;
....
@property(strong) ViewTab1 *view1;
@property(strong) ViewTab2 *view2;
@property (assign) IBOutlet NSTabView *myTabView;

@property (strong) NSString *name;
....

AppDelegate.m

....
@synthesize myTabView;
@synthesize view1,view2;
@synthesize name;
....
- (id)init {
self = [super init];
if (self) {
    name=@"dummy";
}
return self;
....

除此之外,我没有在我的程序中进行任何编码 .

在ViewTab1.xib中,我得到了一个对象并使其成为AppDelegate的一个实例,然后将Application对象(NSApplication)的委托引用连接到同一个对象 . (我希望这是获取AppDelegate对象的正确方法 . )

我在ViewTab2.xib中做了同样的事情

然后我将ViewTab1中的文本字段绑定到ViewTab2中并将其绑定到AppDelegate中的此变量 .

当我运行程序时,文本字段和标签都显示“虚拟” . 但是当我更改文本字段中的值时,它不会反映在第二个选项卡中的标签中(即ViewTab2) .

请告诉我我做错了什么 .

3 回答

  • 0

    你在 NSTextField 控件中打开了'Continuously Updates Value'吗?

    this example .

  • 0

    我认为问题是您设置为app delegate类的xib中的对象会创建2个不同的app委托实例,因此更改text字段的值会更改一个实例中name的值,但不会更改另一个实例中的name值 . 不幸的是,这就是你做错了,我现在想不出解决办法 .

  • 2

    How to establish binding to the same App delegate object from any loaded Nib?

    是的,我知道这个令人沮丧的情况如上所述...经过数周和数百页的KVO文档 - 通知 - 绑定我认为有一个非常简单的解决方案 .

    正如我们在一些信息源中所发现的那样,nib加载过程会产生新的成员实例......我们需要使用旧的绑定连接 .

    请注意,在加载nib后,InterfaceBuilder中的绑定会自动重定向到这些新实例

    Why not redirect the pointer of App delegate to the old instance?

    在加载nib的方法中,您可以在nib加载之前和之后测试哪个对象是app委托 . 如果新的与前一个不同,您可以根据需要重定向 .

    这个简单的例子适用于Xcode3 10.5.8以下,目标是OSX10.5 / i386:

    // ***** SOMEWHERE IN DEFAULT APP-DELEGATE.m IMPLEMENTATION
    
    - (IBAction) createOtherWindowFromNib: (id)sender
    {
        // ensure that app delegate is set as you want...
        [NSApp setDelegate:self];
        NSLog(@"APP-DELEGAT **** CREATE-TEST-WINDOW ***** WHO IS APP-DELEGATE BEFORE NIB LOAD: %@ ", [[NSApp delegate] description]);
    
    
        // we can bind members of the nib to this controller over proxy object named "File’s Owner"
        NSWindowController *otherWinCapo = [[NSWindowController alloc] initWithWindowNibName: @"OtherTestWindow"];
    
        NSLog(@"APP-DELEGAT **** CREATE-TEST-WINDOW ***** WHO IS APP-DELEGATE AFTER NIB LOAD: %@ ", [[NSApp delegate] description]);
    
        // make some test for delegates before/after here if you need ...
        // usually your bindings made inside "OtherTestWindow.xib" by IB doesn’t works in this moment
    
        // ... and some redirection if needed
        [NSApp setDelegate:self];
    
        // afer that the bind made in IB inside "OtherTestWindow.xib" 
        // referred to (proxy object) "Application.delegate.myBOOL" (Bind to:Application, Model Key Path:delegate.myBOOL)
        // react to changes of myBOOL placed in default app delegate object as expected 
        // simultaneously in every open instance of "OtherTestWindow.xib"
    
        [otherWinCapo showWindow: otherWinCapo.window]; // we need populate the window instance on screen to see it
    }
    

相关问题