Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.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
Objective c 基本MVC:将变量设置为不同类中的NSTextField输入_Objective C_Cocoa_Model View Controller - Fatal编程技术网

Objective c 基本MVC:将变量设置为不同类中的NSTextField输入

Objective c 基本MVC:将变量设置为不同类中的NSTextField输入,objective-c,cocoa,model-view-controller,Objective C,Cocoa,Model View Controller,主要思想是: Model:将文本字段中的值设置为我可以调用/log的变量 视图:只是一个连接到模型类的NSTextField 控制器:NSButton连接到ViewController 您会注意到,它记录NSLog中的基本字符串,以及预定义的begin值。但是当我请求txtbeagin值时,它返回NULL 我知道文本字段和按钮已连接到connections inspector中 屏幕截图: ViewController.h #import <Cocoa/Cocoa.h> #imp

主要思想是:

Model:将
文本字段中的值设置为我可以调用/log的变量

视图:只是一个连接到
模型
类的
NSTextField

控制器:
NSButton
连接到
ViewController

您会注意到,它记录NSLog中的基本字符串,以及预定义的
begin
值。但是当我请求
txtbeagin
值时,它返回
NULL

我知道
文本字段
按钮
已连接到connections inspector中

屏幕截图:

ViewController.h

#import <Cocoa/Cocoa.h>
#import "Model.h"
@interface ViewController : NSView
- (IBAction)logTheVariable:(id)sender;

@end
#import <Foundation/Foundation.h>

@interface Model : NSObject{
//iVars
    int begin;
}
//properties
@property (weak) IBOutlet NSTextField *txtBegin;

//methods
-(void)doSomething;

@end
型号.h

#import <Cocoa/Cocoa.h>
#import "Model.h"
@interface ViewController : NSView
- (IBAction)logTheVariable:(id)sender;

@end
#import <Foundation/Foundation.h>

@interface Model : NSObject{
//iVars
    int begin;
}
//properties
@property (weak) IBOutlet NSTextField *txtBegin;

//methods
-(void)doSomething;

@end
#导入
@接口模型:NSObject{
//伊瓦尔
int开始;
}
//性质
@属性(弱)IBOutlet NSTextField*txtbegen;
//方法
-(无效)剂量测定;
@结束
Model.m

    - (IBAction)logTheVariable:(id)sender 
{
        Model *myModel = [[Model alloc]init];
        [myModel doSomething];
}
#import "Model.h"

@implementation Model

    -(void)doSomething{
        NSLog(@"I'm in the Model Class"); //logs like a charm!
        begin = 5; //just a test to see if it logs anything (which works)
        NSLog(@"%d",begin);// logs like a charm!
        //->Problem is here <-
        NSLog(@"%@",_txtBegin.stringValue); //returns a "NULL" value.
        //->Problem is here <-
    }
    @end
#导入“Model.h”
@实现模型
-(无效)剂量{
NSLog(@“我在模型课上”);//日志就像一个符咒!
begin=5;//只是一个测试,看看它是否记录了任何东西(这是有效的)
NSLog(@“%d”,begin);//日志就像一个符咒!

//->问题在这里问题在这里您在
日志中使用的模型类实例变量:
记录的是空值,因为它是您在ViewController操作中创建的新实例,而不是Model interface builder所知道的实例

- (IBAction)logTheVariable:(id)sender 
{
    Model *myModel = [[Model alloc]init];
    //This is a new instance. The IBOutlet for txtBegin is null. 
    [myModel doSomething];
}
您所实现的并不是MVC的意图。苹果提供了为OSX开发所需的用户界面、框架和编程概念的完整路线图,帮助您了解苹果希望您如何使用其框架

模型通常对用户界面一无所知。它们只存储数据并在数据更改时进行通信

  • 您的模型类应该为它想要公开的任何数据公开一个属性
  • 您的模型类不应该有任何对NSTextField的引用
因此,现在在您的模型中,您可以在更改属性时记录日志

-(void)doSomething:(NSString *)value //method name should be setBegin assuming you name your property 'begin'
{
    NSLog(@"I'm in the Model Class"); //logs like a charm!
    begin = 5; //just a test to see if it logs anything (which works)
    NSLog(@"%d",begin);// logs like a charm!
    //->Problem is here <-
    NSLog(@"%@",value); //will log like a charm
}

简单的解决方案是在viewcontroller中声明textfield的出口,然后在模型类中修改以下方法并实现它:-

模型h

 -(void)doSomething: (NSString*)yourstringvalue;
m型

 -(void)doSomething: (NSString*)yourstringvalue
 {
    NSLog(@"%@",yourstringvalue);
 }
Vie.m

- (IBAction)logTheVariable:(id)sender 
  {
     Model *myModel = [[Model alloc]init];
     NSString * str=self.begintext.stringValue;
    [myModel doSomething:str];
  }

因此,如果我理解正确,我的ViewController应该具有nstextfield的@属性。该模型不允许有一个属性。但是,如何才能将nstextfield值发送到模型,以便它可以使用其值并将其返回到视图?(例如,将值设置为标签)感谢您的响应!不起作用,无论如何,谢谢!