无法从另一个类设置NSString的值

时间:2014-10-10 19:35:18

标签: ios objective-c

我已经在谷歌搜索过,并且堆叠溢出以寻求解决方案,但我无法找到解决问题的答案。很抱歉这篇长篇文章,因为我试图提供尽可能多的信息。我是iOS和Objective-c的新手,但由于被我的公司要求从Android切换而不是一般的编程,所以任何帮助都表示赞赏。

我试图从另一个类的TextField为一个类中的NSString赋值,但是我收到错误:
 **-[ViewController name:]: unrecognized selector sent to instance 0x78712fe0**
当我在模拟器中运行应用程序时。

相关代码:

//UserInfo.h  

    #import <Foundation/Foundation.h>

    @interface UserInfo : NSObject <NSCoding>

    @property (nonatomic, strong) NSString *name;
    @property (nonatomic, strong) NSString *age;
    @property (nonatomic, strong) NSString *address;

    @end

//UserInfo.m  


    #import "UserInfo.h"

    static NSString *nameKey = @"userName";
    static NSString *ageKey = @"userAge";
    static NSString *addressKey = @"userAddress";
    static NSString *userInfoKey = @"userInfoKey";

    @implementation UserInfo
    @synthesize name;
    @synthesize age;
    @synthesize address;

    - (id) initWithCoder:(NSCoder *)coder
    {
        self = [super init];

        self.name = [coder decodeObjectForKey:nameKey];
        self.age = [coder decodeObjectForKey:ageKey];
        self.address = [coder decodeObjectForKey:addressKey];

        return self;
    }

    - (void)encodeWithCoder:(NSCoder *)coder
    {
        [coder encodeObject:self.name forKey:nameKey];
        [coder encodeObject:self.age forKey:ageKey];
        [coder encodeObject:self.address forKey:addressKey];
    }

    @end

//ViewController.h


    #import <UIKit/UIKit.h>
    #import "UserInfo.h"

    @interface ViewController : UIViewController <UITextFieldDelegate, UITextViewDelegate, NSCoding>


    @property (nonatomic, strong) UserInfo *userInfoObject;
    @property (nonatomic, weak) IBOutlet UILabel *titleLabel;
    @property (nonatomic, weak) IBOutlet UILabel *nameLabel;
    @property (nonatomic, weak) IBOutlet UILabel *ageLabel;
    @property (nonatomic, weak) IBOutlet UILabel *addressLabel;
    @property (nonatomic, weak) IBOutlet UITextField *nameText;
    @property (nonatomic, weak) IBOutlet UITextField *ageText;
    @property (nonatomic, weak) IBOutlet UITextField *addressText;
    @property (nonatomic, weak) IBOutlet UIButton *saveBtn;

    - (IBAction)saveBtnTouched:(id)sender;

    - (void) saveUserInfo;
    - (void) loadUserInfo;
    - (void) setUserInterfaceValues;
    - (IBAction)nameText:(id)sender;
    - (IBAction)ageText:(id)sender;
    - (IBAction)addressText:(id)sender;

    @end

//ViewController.m


    #import "ViewController.h"
    #import "UserInfo.h"

    @interface ViewController ()

    @end

    @implementation ViewController

    @synthesize titleLabel;
    @synthesize nameLabel;
    @synthesize ageLabel;
    @synthesize addressLabel;
    @synthesize nameText;
    @synthesize ageText;
    @synthesize addressText;
    @synthesize saveBtn;


    static NSString *userInfoKey = @"userInfoKey";



    - (void)viewDidLoad {
        [super viewDidLoad];
        [self loadUserInfo];

        if(!self.userInfoObject)
        {
            self.userInfoObject = [[UserInfo alloc] init];
        }

        [self setUserInterfaceValues];
    }

    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
    }

    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
    {
        self.saveBtn.enabled = YES;
        return YES;
    }

    - (BOOL)textFieldShouldReturn:(UITextField*)textField
    {
        return YES;
    }

    - (BOOL) textFieldShouldEndEditing:(UITextField *)textField
    {

        [self.nameText resignFirstResponder];
        [self.ageText resignFirstResponder];
        [self.addressText resignFirstResponder];

        return YES;
    }

    - (IBAction)saveBtnTouched:(id)sender
    {
        NSLog(@"%@ was entered into the name field", self.nameText.text);
        NSLog(@"%@ was entered into the age field", self.ageText.text);
        NSLog(@"%@ was entered into the address field", self.addressText.text);



        [self textFieldShouldEndEditing:self.nameText];
        [self textFieldShouldEndEditing:self.ageText];
        [self textFieldShouldEndEditing:self.addressText];

        self.userInfoObject.name = self.nameText.text;
        self.userInfoObject.age = self.ageText.text;
        self.userInfoObject.address = self.addressText.text;



        [self saveUserInfo];

        self.saveBtn.enabled = NO;
    }

    - (void)saveUserInfo
    {
        NSData *userInfoData = [NSKeyedArchiver archivedDataWithRootObject:self.userInfoObject];
        [[NSUserDefaults standardUserDefaults] setObject:userInfoData forKey:userInfoKey];
    }

    - (void)loadUserInfo
    {
        NSData *userInfoData = [[NSUserDefaults standardUserDefaults] objectForKey:userInfoKey];
        if(userInfoData)
        {
        self.userInfoObject = [NSKeyedUnarchiver unarchiveObjectWithData:userInfoData];
        }
    }

    - (void) setUserInterfaceValues
    {
        self.nameText.text = self.userInfoObject.name;
        self.ageText.text = self.userInfoObject.age;
        self.addressText.text = self.userInfoObject.address;
    }

    - (IBAction)nameText:(id)sender {
    }
    - (IBAction)ageText:(id)sender {
    }
    - (IBAction)addressText:(id)sender {
    }


    @end


//AppDelegate.h  


    #import <UIKit/UIKit.h>
    #import "ViewController.h"
    #import "UserInfo.h"

    @interface AppDelegate : UIResponder <UIApplicationDelegate>

    @property (strong, nonatomic) UIWindow *window;
    @property (strong, nonatomic) ViewController *viewController;


    @end  


//AppDelegate.m  


        #import "AppDelegate.h"
        #import "ViewController.h"
        #import "UserInfo.h"

        @interface AppDelegate ()

        @end

        @implementation AppDelegate


        - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
            // Override point for customization after application launch.
            self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
            self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
            self.window.rootViewController = self.viewController;
            [self.window makeKeyAndVisible];
            return YES;
        }

    //all the other generated methods. Taken out due to space.
    @end

设置三个断点,这些断点是问题的根源:

从ViewController.m,- (void)setUserInterfaceValues

self.nameText.text = self.userInfoObject.name;
  (我认为这也适用于它下面的另外两行)

同样来自ViewController.m,在- (void)viewDidLoad[self setUserInterfaceValues];

最后来自AppDelegate.m,在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

self.window.rootViewController = self.viewController;

根据我的理解,并从搜索此问题中学到,应用程序正在尝试将数据发送到不存在的内容,NSString名称是罪魁祸首。其他人建议确保我的.xib文件连接到ViewController,我已经验证它是。

作为另一点信息,我没有为这个应用程序使用故事板,而是使用界面构建器。我知道故事板有优点,我想使用它们,但我的公司使用界面构建器并以编程方式执行很多操作,因此我学会了没有开发。

[编辑]:伊恩已经解决了问题。

0 个答案:

没有答案