Objective c 来自其他窗口的OS X委托集标签(Xcode)
我对Mac编程(而不是Objective C)相当陌生 我正在开发一个小应用程序,它显示一些数据并在按下按钮时打开第二个窗口。 第二个窗口中有一个文本字段和一个提交按钮。如果按下提交按钮,窗口应关闭+文本字段的值需要传递到第一个窗口 我认为最好的方法是简单的委托。我试过了,但是我无法使用第二个窗口更改第一个窗口中的标签。。 然而,委托似乎可以工作,因为我可以从另一个类调用方法并向其发送数据。它只是不会改变标签 因为这是我第一次尝试代理,我很确定我在这里做了一些愚蠢的事情^^ 还是有更好的解决方案?从第二个窗口更改标签不会太复杂。。对吧? ViewController.h(FirstController) secondController.hObjective c 来自其他窗口的OS X委托集标签(Xcode),objective-c,xcode,macos,delegates,Objective C,Xcode,Macos,Delegates,我对Mac编程(而不是Objective C)相当陌生 我正在开发一个小应用程序,它显示一些数据并在按下按钮时打开第二个窗口。 第二个窗口中有一个文本字段和一个提交按钮。如果按下提交按钮,窗口应关闭+文本字段的值需要传递到第一个窗口 我认为最好的方法是简单的委托。我试过了,但是我无法使用第二个窗口更改第一个窗口中的标签。。 然而,委托似乎可以工作,因为我可以从另一个类调用方法并向其发送数据。它只是不会改变标签 因为这是我第一次尝试代理,我很确定我在这里做了一些愚蠢的事情^^ 还是有更好的解决方案
#import <Cocoa/Cocoa.h>
#import "ViewController.h"
@interface secondController : NSViewController <ViewControllerDelegate>
{
IBOutlet NSTextField *txtfield;
}
-(IBAction)submit:(id)sender;
@end
日志输出:
#import "firstController.h"
@implementation secondController
-(void)viewDidLoad
{
[super viewDidLoad];
ViewController *custom = [[ViewController alloc] init];
// assign delegate
custom.delegate = self;
[custom helloDelegate];
}
-(void)sayHello:(ViewController *)ViewController
{
NSLog(@"Hiya!");
}
-(IBAction)submit:(id)sender
{
NSString *txtval= txtfield.stringValue;
NSLog(@"submit: %@", txtval);
ViewController *custom = [[ViewController alloc] init];
// assign delegate
custom.delegate = self;
[custom reciveVar:txtval];
}
@end
[...]
- (void)viewDidLoad
{
[super viewDidLoad];
txtlabel.stringValue=@"TEST";
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleUpdatedData:)
name:@"DataUpdated"
object:nil];
}
-(void)handleUpdatedData:(NSNotification *)notification
{
NSLog(@"recieved %@", notification);
txtlabel.stringValue=[notification object];
}
-(IBAction)submit:(id)sender
{
NSString *txtval= txtfield.stringValue;
NSLog(@"submit: %@", txtval);
[[NSNotificationCenter defaultCenter] postNotificationName:@"DataUpdated"
object:txtval];
}
secondController.m
#import "firstController.h"
@implementation secondController
-(void)viewDidLoad
{
[super viewDidLoad];
ViewController *custom = [[ViewController alloc] init];
// assign delegate
custom.delegate = self;
[custom helloDelegate];
}
-(void)sayHello:(ViewController *)ViewController
{
NSLog(@"Hiya!");
}
-(IBAction)submit:(id)sender
{
NSString *txtval= txtfield.stringValue;
NSLog(@"submit: %@", txtval);
ViewController *custom = [[ViewController alloc] init];
// assign delegate
custom.delegate = self;
[custom reciveVar:txtval];
}
@end
[...]
- (void)viewDidLoad
{
[super viewDidLoad];
txtlabel.stringValue=@"TEST";
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleUpdatedData:)
name:@"DataUpdated"
object:nil];
}
-(void)handleUpdatedData:(NSNotification *)notification
{
NSLog(@"recieved %@", notification);
txtlabel.stringValue=[notification object];
}
-(IBAction)submit:(id)sender
{
NSString *txtval= txtfield.stringValue;
NSLog(@"submit: %@", txtval);
[[NSNotificationCenter defaultCenter] postNotificationName:@"DataUpdated"
object:txtval];
}
解决了。(感谢)
在这种情况下,NSNotification比委托更简单、更高效
ViewController.m
#import "firstController.h"
@implementation secondController
-(void)viewDidLoad
{
[super viewDidLoad];
ViewController *custom = [[ViewController alloc] init];
// assign delegate
custom.delegate = self;
[custom helloDelegate];
}
-(void)sayHello:(ViewController *)ViewController
{
NSLog(@"Hiya!");
}
-(IBAction)submit:(id)sender
{
NSString *txtval= txtfield.stringValue;
NSLog(@"submit: %@", txtval);
ViewController *custom = [[ViewController alloc] init];
// assign delegate
custom.delegate = self;
[custom reciveVar:txtval];
}
@end
[...]
- (void)viewDidLoad
{
[super viewDidLoad];
txtlabel.stringValue=@"TEST";
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleUpdatedData:)
name:@"DataUpdated"
object:nil];
}
-(void)handleUpdatedData:(NSNotification *)notification
{
NSLog(@"recieved %@", notification);
txtlabel.stringValue=[notification object];
}
-(IBAction)submit:(id)sender
{
NSString *txtval= txtfield.stringValue;
NSLog(@"submit: %@", txtval);
[[NSNotificationCenter defaultCenter] postNotificationName:@"DataUpdated"
object:txtval];
}
secondController.m
#import "firstController.h"
@implementation secondController
-(void)viewDidLoad
{
[super viewDidLoad];
ViewController *custom = [[ViewController alloc] init];
// assign delegate
custom.delegate = self;
[custom helloDelegate];
}
-(void)sayHello:(ViewController *)ViewController
{
NSLog(@"Hiya!");
}
-(IBAction)submit:(id)sender
{
NSString *txtval= txtfield.stringValue;
NSLog(@"submit: %@", txtval);
ViewController *custom = [[ViewController alloc] init];
// assign delegate
custom.delegate = self;
[custom reciveVar:txtval];
}
@end
[...]
- (void)viewDidLoad
{
[super viewDidLoad];
txtlabel.stringValue=@"TEST";
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleUpdatedData:)
name:@"DataUpdated"
object:nil];
}
-(void)handleUpdatedData:(NSNotification *)notification
{
NSLog(@"recieved %@", notification);
txtlabel.stringValue=[notification object];
}
-(IBAction)submit:(id)sender
{
NSString *txtval= txtfield.stringValue;
NSLog(@"submit: %@", txtval);
[[NSNotificationCenter defaultCenter] postNotificationName:@"DataUpdated"
object:txtval];
}
两件事。。。。你的授权似乎在倒退。我想您希望
ViewController
在secondController
请求时采取行动。然而,更重要的是ViewController*custom=[[viewcontrolleralloc]init]代码>创建一个新对象。您对它所做的操作对已经存在的ViewController
没有影响。有意义。。你知道如何访问现有的ViewController吗?你说第二个窗口会在按下按钮时打开。如果由ViewController
或具有对ViewController
的引用的某个对象处理,则它可以传递self
或类似于controller2.delegate=代码>。否则,这实际上取决于应用程序的结构。如果共享引用不可用,您可以始终切换到NSNotification
,而不是将委托作为策略。发送通知时,您可以将信息作为通知对象或在userinfo字典中附加到通知。所以你可以给监听器(第一个控制器)任何你喜欢的东西。。。。你的授权似乎在倒退。我想您希望ViewController
在secondController
请求时采取行动。然而,更重要的是ViewController*custom=[[viewcontrolleralloc]init]代码>创建一个新对象。您对它所做的操作对已经存在的ViewController
没有影响。有意义。。你知道如何访问现有的ViewController吗?你说第二个窗口会在按下按钮时打开。如果由ViewController
或具有对ViewController
的引用的某个对象处理,则它可以传递self
或类似于controller2.delegate=代码>。否则,这实际上取决于应用程序的结构。如果共享引用不可用,您可以始终切换到NSNotification
,而不是将委托作为策略。发送通知时,您可以将信息作为通知对象或在userinfo字典中附加到通知。所以你可以给监听器(第一个控制器)任何你喜欢的东西。。。。你的授权似乎在倒退。我想您希望ViewController
在secondController
请求时采取行动。然而,更重要的是ViewController*custom=[[viewcontrolleralloc]init]代码>创建一个新对象。您对它所做的操作对已经存在的ViewController
没有影响。有意义。。你知道如何访问现有的ViewController吗?你说第二个窗口会在按下按钮时打开。如果由ViewController
或具有对ViewController
的引用的某个对象处理,则它可以传递self
或类似于controller2.delegate=代码>。否则,这实际上取决于应用程序的结构。如果共享引用不可用,您可以始终切换到NSNotification
,而不是将委托作为策略。发送通知时,您可以将信息作为通知对象或在userinfo字典中附加到通知。因此,可以给监听器(第一个控制器)任何您喜欢的东西。