更改 NSTextField 的值不会更新标签?

编程入门 行业动态 更新时间:2024-10-26 22:22:19
本文介绍了更改 NSTextField 的值不会更新标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试从 AppDelegate 更改标签.我可以使用在具有标签的类的实现中运行 changeLabel 的 IBAction 更改标签,但是如果我尝试运行 changeLabel> 从 AppDelegate 它改变值(我有一个 NSLog),但不更新标签.

I'm trying to change a label from the AppDelegate. I can change the label with an IBAction that runs a changeLabel in the implementation of the class that has the label, but if I try to run changeLabel from the AppDelegate it changes the value (I have an NSLog), but doesn't update label.

代码如下:

#import <Foundation/Foundation.h> @interface testLabelThingy : NSObject @property (strong) IBOutlet NSTextField *daLabel; - (id) init; - (void)changeLabel; - (IBAction)daButton:(id)sender; @end

和:

#import "testLabelThingy.h" @implementation testLabelThingy @synthesize daLabel; - (id) init{ self.daLabel = [[NSTextField alloc] init]; return self; } - (IBAction)daButton:(id)sender{ [self changeLabel]; } - (void)changeLabel{ NSLog(@"Change Label Function. Current value is: %@", [self.daLabel stringValue]); if([[self.daLabel stringValue] isEqualToString:@"Bloog"]){ [self.daLabel setStringValue:@"Blarg"]; }else{ [self.daLabel setStringValue:@"Bloog"]; } } @end

推荐答案

为此,您必须使用 NSNotificationCenter.

在 Appdelegate 中使用以下代码.

in Appdelegate use following code.

[[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeThelabel" object:nil];

在具有标签的类的实现的init方法中使用以下代码.

use the below code in the init method of the implementation of the class that has the label.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ChangelabelText:) name:@"ChangeThelabel" object:nil];

并在同一个类中使用以下函数.

And in the same class use the following function.

- (void)ChangelabelText:(NSNotification *)notification { // Change the text here. }

更多推荐

更改 NSTextField 的值不会更新标签?

本文发布于:2023-10-25 22:28:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1528294.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标签   NSTextField

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!