からラベルを変更しようとしていAppDelegate
ます。私はラベルを変更することができますIBAction
実行されるchangeLabel
ラベルを持つクラスの実装では、私が実行しようとするchangeLabel
からAppDelegate
、それが値を変更する(私はのNSLogを持っている)が、ラベルを更新しません。
コードは次のとおりです。
#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
次のコードを使用しています。
[[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeThelabel" object:nil];
init
ラベルを持つクラスの実装メソッドで以下のコードを使用します。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ChangelabelText:) name:@"ChangeThelabel" object:nil];
また、同じクラスで次の関数を使用します。
- (void)ChangelabelText:(NSNotification *)notification
{
// Change the text here.
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加