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

时间:2013-10-24 17:09:12

标签: objective-c cocoa model-view-controller

主要观点:

模型:将值从textfield设置为我可以调用/记录的变量。

查看:只有一个NSTextFieldModel班级相关联。

控制器:NSButton连接到ViewController

正如您将注意到的,它记录了NSLog的基本字符串,也是预定义的begin值。但当我要求txtBegin值时,它会返回NULL

我知道TextFieldButton已连接到连接检查器中。

截图:

enter image description here

Downloadable project:

ViewController.h

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

@end

ViewController.m

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

Model.h

#import <Foundation/Foundation.h>

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

//methods
-(void)doSomething;

@end

Model.m

#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

2 个答案:

答案 0 :(得分:1)

您在logTheVariable:中使用的Model类实例正在记录一个空值,因为它是您在ViewController的操作中创建的新实例,而不是Model接口构建器的实例所知道的。

- (IBAction)logTheVariable:(id)sender 
{
    Model *myModel = [[Model alloc]init];
    //This is a new instance. The IBOutlet for txtBegin is null. 
    [myModel doSomething];
}

您实施的并不是MVC的目标。 Apple提供了一个完整的用户界面,框架和编程概念的路线图,您需要知道为OSX开发,这将有助于您了解Apple的方式希望你使用他们的框架。 https://developer.apple.com/library/mac/referencelibrary/GettingStarted/RoadMapOSX/chapters/01_Introduction.html

模型通常不知道关于用户界面的任何。它们只是存储数据并在数据更改时进行通信。

  • 您的Model类应该公开它想要公开的任何数据的属性。
  • 您的Model类不应该对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
}

视图通常不知道关于模型的任何。它只是以用户可以与之交互并可能编辑的方式显示数据。

控制器将模型和视图绑定在一起。它在数据更改时从模型接收通知并更新View。相反,它还会在编辑数据时从View接收通知以更新模型。

  • 您的ViewController类应该引用NSTextField(使用插座)
  • 您的ViewController类应该具有内部创建的Model实例。

现在可以实现logTheVariable以将模型和视图绑定在一起:

- (IBAction)logTheVariable:(id)sender 
{
    //Use ViewController's model instance
    Model *myModel = [self myModel];
    NSString * value = [[self txtBegin] stringValue]; 
    [myModel doSomething:value];
}

答案 1 :(得分:1)

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

Model.h

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

Model.m

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

Viecontroller.m

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