简体   繁体   中英

How do I set the stringValue of NSTextView if I only have the containing view?

Here is the question, hopefully easy to answer.

I'm making a class in which you give it an array of views and it saves all the elements inside it and is able to read them back. I got the saving working by using NSArchiver class, but not the reading part.

@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

The idea is I want an easy way to save preferences and recall them later. I'm aware of NS_UserDefaults, and doing it manually by creating connections from interface builder to my code, but I have literally thousands of preferences that I would have to make connections for and save.

So my goal is to create a class where I say something like...

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

My plan was to set the identifier attribute in interface builder for every element. Then to get a value out of it I would have a function cycle through every subview in the views until it found one who's identifier matched what I requested and I would inspect it's classtype to create a temporary variable of that and use that temporary variable of the class tpye to get the value out of it.

For example, lets say the object inside of my NSView was an NSTextView with the stringValue of "hello world"

Using the NSViews method subViews I return an array of all it's subviews, which would include the NSTextView mentioned above.

I can use the function from NSObject className to return the classname as @"NSTextView".

Then I should be able to say... one of two things

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

or...

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

That compiles but I get a warning (Incompatible pointer types initializing 'NSTextView *__strong' with an expression of type 'NSView *const __strong')

When I run the program and attempt to run my routine it crashes and says -[NSTextField setString:]:unrecognized selector sent to an instance 0X101a0cfe0

I have no idea what to do, help would be appreciated!

Hi there guys. Here is the question, hopefully easy to answer.

I'm making a class in which you give it an array of views and it saves all the elements inside it and is able to read them back. I got the saving working by using NSArchiver class, but not the reading part.

@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

The idea is I want an easy way to save preferences and recall them later. I'm aware of NS_UserDefaults, and doing it manually by creating connections from interface builder to my code, but I have literally thousands of preferences that I would have to make connections for and save.

So my goal is to create a class where I say something like...

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

My plan was to set the identifier attribute in interface builder for every element. Then to get a value out of it I would have a function cycle through every subview in the views until it found one who's identifier matched what I requested and I would inspect it's classtype to create a temporary variable of that and use that temporary variable of the class tpye to get the value out of it.

For example, lets say the object inside of my NSView was an NSTextView with the stringValue of "hello world"

Using the NSViews method subViews I return an array of all it's subviews, which would include the NSTextView mentioned above.

I can use the function from NSObject className to return the classname as @"NSTextView".

Then I should be able to say... one of two things

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

or...

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

That compiles but I get a warning (Incompatible pointer types initializing 'NSTextView *__strong' with an expression of type 'NSView *const __strong')

When I run the program and attempt to run my routine it crashes and says -[NSTextField setString:]:unrecognized selector sent to an instance 0X101a0cfe0

I have no idea what to do, help would be appreciated!

My implementation of the interface is below

@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

It seems like your views array contains text fields, not text views, maybe you made a binding error in IB.

A text field responds to setStringValue: , not to setString: .

Some tips

  • You can test the class belonging of an object;
  • Provided that you are sure that a pointer points to an object of a certain class, you can cast the pointer. In your case you would do a downcasting (from base class to subclass).

So you would do something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM