gpt4 book ai didi

objective-c - 基本MVC : set variable to NSTextField input in different classes

转载 作者:行者123 更新时间:2023-12-03 17:40:04 25 4
gpt4 key购买 nike

主要思想:

模型:将文本字段中的值设置为我可以调用/记录的变量。

View:只是一个连接到 Model 类的 NSTextField

Controller :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];
}

模型.h

#import <Foundation/Foundation.h>

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

//methods
-(void)doSomething;

@end

模型.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

最佳答案

简单的解决方案只需在 View Controller 中声明文本字段的导出,然后在模型类中修改以下方法并实现它:-

模型.h

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

型号.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];
}

关于objective-c - 基本MVC : set variable to NSTextField input in different classes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19572270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com