Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 无法从另一个类设置NSString的值_Ios_Objective C - Fatal编程技术网

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

Ios 无法从另一个类设置NSString的值,ios,objective-c,Ios,Objective C,我在google上搜索了所有的地方,并在stack overflow中寻找解决方案,但我没能找到解决我问题的答案。很抱歉发了这么长的帖子,因为我正在尽可能多地提供信息。我不熟悉iOS和Objective-c,但由于我的公司要求我从Android转换过来,所以我一般不会编程,所以非常感谢您的帮助 我试图从另一个类中的文本字段为一个类中的NSString赋值,但出现错误: **-[ViewController名称:]:发送到实例0x78712fe0的选择器无法识别** 当我在模拟器中运行应用程序时

我在google上搜索了所有的地方,并在stack overflow中寻找解决方案,但我没能找到解决我问题的答案。很抱歉发了这么长的帖子,因为我正在尽可能多地提供信息。我不熟悉iOS和Objective-c,但由于我的公司要求我从Android转换过来,所以我一般不会编程,所以非常感谢您的帮助

我试图从另一个类中的文本字段为一个类中的NSString赋值,但出现错误: **-[ViewController名称:]:发送到实例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,in-voidsetUserInterfaceValues

self.nameText.text=self.userInfoObject.name; 我假设这也适用于它下面的另外两行

也可以从ViewController.m中的-voidviewDidLoad [自我设置用户接口值]

最后,从AppDelegate.m,in-BOOLapplication:UIApplication*application didnfinishlaunchingwithoptions:NSDictionary*launchOptions

self.window.rootViewController=self.viewController

据我所知,并从搜索这个问题中了解到,该应用程序试图将数据发送到不存在的东西,NSString名称是罪魁祸首。其他人建议确保我的.xib文件已连接到ViewController,我已验证它是否已连接

作为另一点信息,我没有为这个应用程序使用故事板,而是使用interface builder。我知道故事板有很多优点,我很想使用它们,但我的公司使用interface builder,并且以编程方式做很多事情,所以我正在学习不用脚本进行开发


[编辑]:多亏了Ian,问题才得以解决。

看起来您可能在某个时候将NSUserDefaults设置为包含错误的对象,然后自修复错误以来,您一直在错误地加载它。谢谢,Ian。你能不能再详细一点?我是一个绝对的初学者。如果您以前在NSUserDefaults中为存储的值分配了不正确的指针,那么在检索该对象时,该对象将再次加载。尝试在不加载数据的情况下运行它,然后查看结果。此外,你不需要像Android强迫你使用Activitys那样进行数据共享;研究使用委托和数据源协议来替换此行为。您使用了多少视图控制器?您使用的是导航视图控制器吗?Ian,这就解决了问题。谢谢你的回复和解释。