如果我只有包含视图,如何设置NSTextView的stringValue?

时间:2013-01-14 02:19:58

标签: objective-c macos cocoa

这是一个问题,希望很容易回答。

我正在创建一个类,在其中为它提供一个视图数组,它将所有元素保存在其中并能够读回它们。我通过使用NSArchiver类获得了保存工作,但没有阅读部分。

@interface preferences : NSObject

- (void)savePreferences;
- (void)readPreferences;

@property (strong) NSMutableArray *theViews;

@end
 #import "preferences.h"

@implementation preferences
- (id)init {
    self = [super init];
    if (self) {
        _theViews = [[NSMutableArray alloc]init];
    }
    return self;
}

- (void)savePreferences{
    NSLog(@"Saving Preferences...");
    [NSKeyedArchiver archiveRootObject:_theViews toFile:@"preferences.xml"];
}

- (void)readPreferences{
    NSLog(@"Reading Preferences...");
    _theViews = [NSKeyedUnarchiver unarchiveObjectWithFile:@"preferences.xml"];
}

@end

我的想法是想要一种简单的方法来保存偏好并在以后调用它们。我知道NS_UserDefaults,并通过创建从接口构建器到我的代码的连接手动完成它,但我有几千个首选项,我必须连接和保存。

所以我的目标是创建一个类,我说的是......

[preferences savePreferencesForViews:ArrayOfViews];//For Saving
[preferences readPreferencesForViews:ArrayOfViews];//For Reading

我的计划是在界面构建器中为每个元素设置标识符属性。然后为了从中获取一个值,我将通过视图中的每个子视图进行一个函数循环,直到找到一个标识符与我请求的匹配,然后我会检查它的classtype来创建一个临时变量并使用该临时变量class tpye从中获取价值。

例如,假设我的NSView内部的对象是一个NSTextView,其stringValue为“hello world”

使用NSViews方法子视图我返回一个包含所有子视图的数组,其中包括上面提到的NSTextView。

我可以使用NSObject className中的函数将类名返回为@“NSTextView”。

然后我应该可以说......两件事之一

   [subView setString:@"hey"];//compile error

...或

 NSTextView *temp = subView;
    [temp setString:@"test"];

编译但我收到警告(不兼容的指针类型初始化'NSTextView * __ strong',表达式为'NSView * const __strong')

当我运行程序并尝试运行我的例程时,它会崩溃并说 - [NSTextField setString:]:无法识别的选择器发送到实例0X101a0cfe0

我不知道该怎么做,帮助将不胜感激!

大家好。这是一个问题,希望很容易回答。

我正在创建一个类,在其中为它提供一个视图数组,它将所有元素保存在其中并能够读回它们。我通过使用NSArchiver类获得了保存工作,但没有阅读部分。

@interface preferences : NSObject

- (void)savePreferences;
- (void)readPreferences;

@property (strong) NSMutableArray *theViews;

@end
 #import "preferences.h"

@implementation preferences
- (id)init {
    self = [super init];
    if (self) {
        _theViews = [[NSMutableArray alloc]init];
    }
    return self;
}

- (void)savePreferences{
    NSLog(@"Saving Preferences...");
    [NSKeyedArchiver archiveRootObject:_theViews toFile:@"preferences.xml"];
}

- (void)readPreferences{
    NSLog(@"Reading Preferences...");
    _theViews = [NSKeyedUnarchiver unarchiveObjectWithFile:@"preferences.xml"];
}

@end

我的想法是想要一种简单的方法来保存偏好并在以后调用它们。我知道NS_UserDefaults,并通过创建从接口构建器到我的代码的连接手动完成它,但我有几千个首选项,我必须连接和保存。

所以我的目标是创建一个类,我说的是......

[preferences savePreferencesForViews:ArrayOfViews];//For Saving
[preferences readPreferencesForViews:ArrayOfViews];//For Reading

我的计划是在界面构建器中为每个元素设置标识符属性。然后为了从中获取一个值,我将通过视图中的每个子视图进行一个函数循环,直到找到一个标识符与我请求的匹配,然后我会检查它的classtype来创建一个临时变量并使用该临时变量class tpye从中获取价值。

例如,假设我的NSView内部的对象是一个NSTextView,其stringValue为“hello world”

使用NSViews方法子视图我返回一个包含所有子视图的数组,其中包括上面提到的NSTextView。

我可以使用NSObject className中的函数将类名返回为@“NSTextView”。

然后我应该可以说......两件事之一

   [subView setString:@"hey"];//compile error

...或

 NSTextView *temp = subView;
    [temp setString:@"test"];

编译但我收到警告(不兼容的指针类型初始化'NSTextView * __ strong',表达式为'NSView * const __strong')

当我运行程序并尝试运行我的例程时,它会崩溃并说 - [NSTextField setString:]:无法识别的选择器发送到实例0X101a0cfe0

我不知道该怎么做,帮助将不胜感激!

我的界面实现在

之下
@implementation appController

- (IBAction)savePreferences:(id)sender {
    NSLog(@"Save Button Pressed");

    //Create an instance of the preference class.
    preferences *PreferencesObject = [[preferences alloc]init];

    //Add the views we want to sve to the PreferenceObject.
    [[PreferencesObject theViews]addObject:_preferenceView];

    //Save the actual preferences now.
    [PreferencesObject savePreferences];

}
- (IBAction)loadPreferences:(id)sender {

    //Create an instance of the preference class.
    preferences __strong *PreferencesObject = [[preferences alloc]init];

    //Read the preferences into it's internal array.
    [PreferencesObject readPreferences];

    for(NSView __strong *view in [PreferencesObject theViews]){
        if([[view identifier]isEqualTo:[_preferenceView identifier]]){
            NSLog(@"View Match Found For PreferenceView, Setting...");


            for(NSView *subView in [_preferenceView subviews]){
                if([[subView identifier]isEqualTo:@"name"]){
                    NSLog(@"Attempting to set value of name control");
                    NSTextView *temp = subView;
                    [temp setString:@"test"];
                }
            }


        }
    }


}
@end

1 个答案:

答案 0 :(得分:1)

您的视图数组似乎包含文本字段,而不是文本视图,也许您在IB中发生了绑定错误。

文本字段响应setStringValue :,而不是setString :.

一些提示

  • 您可以测试对象的类;
  • 如果您确定指针指向某个类的对象,则可以转换指针。在你的情况下,你会做一个向下转换(从基类到子类)。

所以你会做类似的事情:

if ([ subview isKindOfClass: [NSTextView class]]) // until you fix that binding error or whatever, it will return NO
{
    NSTextView* temp= (NSTextView*) subview;
    [ temp setString: @"temp" ];
}