如何将NSMutableString绑定到NSTextView的值? [英] How do I bind an NSMutableString to the value of an NSTextView?

查看:177
本文介绍了如何将NSMutableString绑定到NSTextView的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我试图在Xcode中为自己设置一个非常简单的程序。

我有一个窗口(在界面构建器中),它包含一个NSTextView和一个NSButton。在我的AppController类中,我已经声明了一个 NSMutableString 作为属性。我想做的是为字符串设置一个初始值,例如@First Entry\\\
,然后将其绑定到 NSTextView 。当我按下按钮,调用一个方法, - (IBAction)appendToString 被追加 @另一个Line\\\
NSMutableString

我想要这些更改反映在 NSTextView 通过绑定和键值观察,而不是通过声明 IBOutlet 并手动更新内容。我也想在文本视图中修改,以编辑 NSMutable 字符串。

Ok, so I'm trying to set up a very simple program for myself in Xcode.
I have a window (in Interface builder) which contains an NSTextView, and an NSButton. Inside my AppController class, I have declared an NSMutableString as a property. What I want to do is to set an initial value for the string, say @"First Entry\n", then, bind it to the NSTextView. When I push the button, a method, -(IBAction)appendToString is called which appends @"Another Line\n" to the NSMutableString.
I want these changes to then be reflected in the NSTextView by means of bindings and key value observing, rather than by declaring an IBOutlet and manually updating the contents. I'd also like changes I make in the textview to edit the NSMutable string.

非常困惑,没有任何运气想出这一点。

Unfortunately, I'm terribly confused, and an not having any luck figuring this out.

如何去做这些的基本步骤是什么?

What are some basic steps for how to go about this?

*****编辑*****

*****EDIT*****

好的,Peter Hosey间接解决了这个问题。我忘了我不得不使用setLogString方法,而不是使用[logString appendFormat:]。所以下面是我最终得到的代码:

Ok, Peter Hosey indirectly solved the problem. I was forgetting that I had to use the setLogString method, rather than using [logString appendFormat:]. So here's the code I ended up with:

在AppController.h中:

in AppController.h:

#import <Cocoa/Cocoa.h>
@interface AppController : NSObject {
    IBOutlet NSButton *appendButton;
    NSString *logString;
    IBOutlet NSTextView *thatView;
}
@property (readwrite, copy) NSString *logString;
-(IBAction)hitIt:(id)sender;
@end

AppController.m:

AppController.m:

#import "AppController.h"
@implementation AppController
-(void)awakeFromNib
{
}
-(id)init
{
    [super init];   
    logString = @"First Line\n";
    return self;
}
-(IBAction)hitIt:(id)sender
{
    [self setLogString:[logString stringByAppendingString:@"Hey, Another Line!\n"]];
}
@synthesize logString;
@end

然后,在InterfaceBuilder中,我将NSTextView绑定到AppController.logString, HAD设置连续更新值标志,以便我对文本字段进行的更改将作为字符串。

Then, in InterfaceBuilder, I bound the NSTextView to AppController.logString, and HAD to set the "Continuously Updates Value" flag so that changes that I make to the text field will be made to the string.

非常感谢帮助家伙! / p>

Thanks much for the help guys!

推荐答案


我在AppController类中声明了一个NSMutableString作为属性。

Inside my AppController class, I have declared an NSMutableString as a property.

这是正确的符合MVC的解决方案。模型的主要所有者应该是控制器,而不是视图。所有视图都应该从控制器获取模型。

This is the correct, MVC-compliant solution. The primary owner of the model should be the controller, not views. All views should obtain the model from the controller.

但是,你不应该暴露字符串的可变性。使用该属性的任何东西都可以尝试直接改变字符串,而AppController不知道这个改变,并且能够通知属性的任何观察者。该属性应该声明为NSString,并作为readwrite,以提供一个访问器,用一个新的字符串替换该值。

You should not, however, expose the string's mutability. Anything that uses the property may then attempt to mutate the string directly, without the AppController knowing about the change and being able to notify any observers of the property. The property should be declared as NSString, and as readwrite in order to provide an accessor with which to replace the value with a new string.


我想做的是为字符串设置一个初始值,例如@First Entry\\\
,然后将其绑定到NSTextView。

What I want to do is to set an initial value for the string, say @"First Entry\n", then, bind it to the NSTextView.

设置支持 init 中的属性的实例变量的值。

Set the value of the instance variable backing the property in init.

绑定NSTextView在IB。

Bind the NSTextView in IB. Then, it will already be bound when you load the nib.

请注意,您要在加载nib之前设置ivar 。如果您首先加载nib,文本视图将不会看到更改,因为直接设置ivar(如在 init 中应该做的,以避免产生属性副作用)正在使观察者(视图)背后的变化。如果在加载nib时已经进行了更改(并且视图开始观察),则视图不会有任何错过。

Note that you want to set the ivar before loading the nib. If you load the nib first, the text view will not see the change, because setting the ivar directly (as you should do in init, to avoid incurring property side-effects) is making the change behind the observer's (view's) back. If the change is already made when you load the nib (and the view starts observing), then there is nothing for the view to miss.


当我按下按钮时,一个方法 - (IBAction)appendToString ...

When I push the button, a method, -(IBAction)appendToString …

Cocoa中的每个动作都只有一个参数 - ,不少。该参数是发送消息的控件。因此,正确的签名是:

Every action in Cocoa takes exactly one argument—no more, no fewer. That argument is the control that sent the message. Thus, the correct signature is:

- (IBAction) appendToString:(id)sender;




...会调用@Another Line \\\
NSMutableString。

… is called which appends @"Another Line\n" to the NSMutableString.

所有你需要做的是询问你的当前字符串的属性,做一个可变的副本,将修改的字符串设置为属性的新值。不要忘记释放那个可变的副本。

All you need to do is ask the property for your current string, make a mutable copy, make the change, and set the modified string as the new value of the property. Don't forget to release that mutable copy.


我想要这些更改,然后通过绑定和键值反映在NSTextView观察,而不是通过声明IBOutlet并手动更新内容。我也想在文本视图中更改我编辑NSMutable字符串。

I want these changes to then be reflected in the NSTextView by means of bindings and key value observing, rather than by declaring an IBOutlet and manually updating the contents. I'd also like changes I make in the textview to edit the NSMutable string.

这两个都是免费的,当你绑定文本视图的 value 绑定到您的AppController的属性。

Both of these happen for free when you bind the text view's value binding to your AppController's property.

这篇关于如何将NSMutableString绑定到NSTextView的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆