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

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

问题描述

好了,所以我试图建立一个非常简单的程序为自己在X $ C $角结果
我有一个包含一个NSTextView,和NSButton窗口(在Interface Builder)。里面我的AppController类,我宣布一个的NSMutableString 作为一个属性。我想要做的是字符串设定的初始值,说@首次进入\\ N的话,它绑定到 NSTextView 。当我按下按钮,方法, - (IBAction为)appendToString 称为其追加 @另一行\\ n的NSMutableString 。结果
我想,然后通过绑定和键值观察,而不是通过声明 IBOutlet中 NSTextView 这些变化C>和手动更新的内容。我也想改变我做的TextView的编辑 NSMutable 字符串。

不幸的是,我非常困惑,和一个没有任何运气搞清楚了这一点。

什么是如何去这几个基本步骤?

的*编辑***

好吧,彼得Hosey间接地解决了这个问题。我忘了,我不得不使用setLogString方法,而不是使用[logString appendFormat:]。因此,这里的code我结束了:

在AppController.h:

 #进口<可可/ Cocoa.h>
@interface AppController的:NSObject的{
    IBOutlet中NSButton * appendButton;
    * NSString的logString;
    IBOutlet中NSTextView * thatView;
}
@property(读写,拷贝)的NSString * logString;
- (IBAction为)hitIt:(ID)发送;
@结束

AppController.m:

 #进口AppController.h
@implementation AppController的
- (无效)awakeFromNib
{
}
- (ID)的init
{
    [超级初始化]
    logString = @行头\\ n;
    返回自我;
}
- (IBAction为)hitIt:(ID)发送者
{
    [个体经营setLogString:[logString stringByAppendingString:@嘿,另一行\\ N]];
}
@synthesize logString;
@结束

然后,在InterfaceBuilder下,我绑定的NSTextView到AppController.logString,并设置了连续更新值标志,这样的变化,我对文本字段将字符串进行。

感谢很多的帮助家伙!


解决方案

  

在我的AppController类,我宣布一个的NSMutableString作为属性。


这是正确的,MVC-标准的解决方案。该模型的主要拥有者应该是控制器,而不是意见。所有意见应得到从控制器的型号。

您不应该,但是,公开字符串的可变性。使用该属性,然后可试图直接变异字符串,而不AppController中知道此变化,并能够通知财产的任何观察者任何东西。该物业应被宣布为的NSString,并以提供与用新字符串替换值的访问读写。


  

我想要做的是字符串设定的初始值,说@首次进入\\ n,那么,将其绑定到NSTextView。


设置实例变量后盾属性初始化的值。

绑定NSTextView在IB。然后,将已经当您加载笔尖约束。

请注意您想要前的设置伊娃的加载笔尖。如果你第一次加载笔尖,文本视图不会看到改变,因为直接设置伊娃(你应该在做的init ,以避免招致财产副作用)正在后面的观察者(视图)变回。如果更改已经作出当您加载笔尖(和视图开始观察),那么就没有一种观点错过了。


  

当我按下按钮,方法 - (IBAction为)appendToString ...


在可可每一个动作到底需要一个参数,没有更多的,不下。这说法是,发送消息的控制。因此,正确的签名是:

   - (IBAction为)appendToString:(ID)发送;


  

...被称为该追加@另一行\\ n来了的NSMutableString。


所有你需要做的就是要求物业为您的当前字符串,使一个可变的副本,进行更改,并设置修改后的字符串作为属性的新值。不要忘了释放可变副本。


  

我希望这些改动然后通过绑定和键值观察,而不是通过声明一个IBOutlet和手动更新的内容的方式反映在NSTextView。我也想改变我做的TextView的编辑NSMutable字符串。


这两个发生时免费绑定文本视图的绑定到您的AppController的属性。

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***

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:

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:

#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

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.

Thanks much for the help guys!

解决方案

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

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.

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.

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

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

Bind the NSTextView in IB. Then, it will already be bound when you load the 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.

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

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;

… 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.

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.

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天全站免登陆