首页 > 代码库 > NSWindowController 多个窗口之间传递值

NSWindowController 多个窗口之间传递值

NSWindowController 多个窗口之间传递值

主界面一个按钮点击,出现另一个窗口,然后输入123,点OK,在主界面的编辑框内可以得到123这个值,


思路:

由于要给主界面的控件赋值,所以在第二个窗口里面设定一个同类型的变量。

然后主界面的控件 = 第二个窗口的同类型变量。

例如:主界面是

NSTextField 控件1,

那么就在子窗口里面设定这样一个变量(NSTextField a1),给值。

然后在主界面调用时,让主界面的NSTextField 控件1 =a1

这样主界面就可以得到子界面的值了。


子界面代码如下

H文件

//
//  Form1.h
//  test_multi_window
//
//  Created by EDU on 3/6/14.
//  Copyright (c) 2014 EDU. All rights reserved.
//
#import <Cocoa/Cocoa.h>


@interface Form1 : NSWindowController

@property (assign) IBOutlet NSWindow *window_form1;

@property (assign) NSInteger *button_ok_flag;
@property (assign) NSString *button_string;
@property (assign) IBOutlet NSTextField *m_Edit1;
@property (assign) IBOutlet NSTextField *form_main;
@property (assign) IBOutlet NSTextField *form_main_m_ET1;
@property bool form_ok_cancel_result;
-(void)get_value;
@end

M文件

//
//  Form1.m
//  test_multi_window
//
//  Created by  on 3/6/14.
//  Copyright (c) 2014 EDU. All rights reserved.
//

#import "Form1.h"
#import "EDUAppDelegate.h"
@interface Form1 ()

@end

@implementation Form1
@synthesize form_main;
@synthesize form_main_m_ET1;
@synthesize window_form1;
@synthesize button_ok_flag;
@synthesize button_string;
@synthesize m_Edit1;
@synthesize form_ok_cancel_result;

- (id)initWithWindow:(NSWindow *)window
{
    NSLog (@"init()");
    
    self = [super initWithWindow:window];

    if (self)
    {
       
    }    
    return self;
}

- (void)windowDidLoad
{
    [super windowDidLoad];    
}

- (IBAction)OnBT_OK:(id)sender
{
    form_main.stringValue = http://www.mamicode.com/@"OK";>

主窗口代码如下

H文件

//
//  EDUAppDelegate.h
//  test_multi_window
//
//  Created by  on 3/6/14.
//  Copyright (c) 2014 EDU. All rights reserved.
//

#import <Cocoa/Cocoa.h>
#import "Form1.h"

@interface EDUAppDelegate : NSObject <NSApplicationDelegate>

@property (assign) Form1 *m_form1;

@property (assign) IBOutlet NSWindow *window;

@property (assign) IBOutlet NSTextField *m_label1;

@property (assign) IBOutlet NSTextField *m_ET1;

@end


M文件

//
//  EDUAppDelegate.m
//  test_multi_window
//
//  Created by EDU on 3/6/14.
//  Copyright (c) 2014 EDU. All rights reserved.
//

#import "EDUAppDelegate.h"

@implementation EDUAppDelegate

@synthesize m_form1;
@synthesize m_label1;
@synthesize window;
@synthesize m_ET1;

- (void)dealloc
{
    [super dealloc];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
}

- (IBAction)OnBT_Form1:(id)sender
{
    if(!m_form1)
    {
        m_form1 = [[Form1 alloc] initWithWindowNibName:@"Form1"];
    }
    m_form1.window.title = @"Hello,this is a test";
    m_form1.form_main = m_label1;
    m_form1.form_main_m_ET1=m_ET1;
    [m_form1 showWindow: sender];
}
@end




运行编译OK

如图:


完成!