Nswindowcontroller passing values between multiple windows
Main interface One button click, another window appears, and then enter 123, click OK, in the main screen edit box can get 123 this value,
Ideas:
To assign a value to the control of the main interface, set a variable of the same type in the second window.
Then the control of the main interface = the same type variable of the second window.
For example: The main interface is
Nstextfield Control 1,
Then set such a variable (Nstextfield A1) in the Subwindow to give the value.
Then, when the main interface is called, let the Nstextfield control of the main interface 1 =A1
So the main interface can get the value of the sub-interface.
The sub-interface code is as follows
H file
form1.h// test_multi_window//// Created by edu on 3/6/14.// Copyright (c) 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 file
form1.m//test_multi_window////Created by on 3/6/14.//Copyright (c) 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 to self;} -(void) windowdidload{[Super Windowdidload]; }-(ibaction) ONBT_OK: (id) sender{Form_main.stringvalue = @ "OK"; Form_ok_cancel_result = true; [Self get_value]; [Super close];} -(Ibaction) Onbt_cancel: (id) sender{Form_main.stringvalue = @ "Cancel"; Form_ok_cancel_result = false; [Self get_value]; [Super close];} -(void) get_value{form_main_m_et1.stringvalue = M_edit1.stringvalue;} @end
The main window code is as follows
H file
eduappdelegate.h// test_multi_window//// Created by on 3/6/14.// Copyright (c) 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 file
eduappdelegate.m// test_multi_window//// Created by edu on 3/6/14.// Copyright (c) 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
Run compile OK
Complete!