我真的是Cocoa/Obj-C編程的新手。我有一個相當簡單的應用程序(目標平臺Mac OSX 10.5 & 10.6),我的背景是C/C++系統編程。Cocoa,Objective-C如何正確地將params從一個窗口傳遞到另一個窗口
我有一個文本字段,按鈕,屬性等主窗口我設置網點和行動(希望是正確的)。在另一NIB申報的新窗口,在該按鈕的點擊一個被加載:正在加載
- (IBAction) openSettings: (id) sender
{
ConfigurationWindowController * wc=[[ConfigurationWindowController alloc] initWithWindowNibName:@"Configuration"];
[wc showWindow:self];
}
新窗口,並顯示出來。
我需要做以下事情: 1)將一些字符串參數傳遞給第二個(「子」)窗口 2)當第二個窗口關閉時,將此參數接收回主窗口。
我不確定什麼是在可可中這樣做的正確方法。
@sergio: 非常感謝您的回覆!在我的「孩子」的窗口,我店指針打開子窗口前的主窗口NAD我通過一些PARAMS:
ConfigurationWindowController * wc=[[ConfigurationWindowController alloc] initWithWindowNibName:@"Configuration"];
mConfigWindow = wc;
[mConfigWindow setValuesToURL:@"some string here" storageParam:@"another string" callerWindowPtr:self];
[wc showWindow:self];
此方法成功調用我看到存儲在「孩子」窗口類實例的屬性值。但是,當我嘗試將此值分配給setValuesToURL方法中的textfiled時,GUI元素仍然爲空,我嘗試在awakeFromNib方法中分配存儲的字符串,但這裏的這些屬性爲null! 。自指針的值也不同 - 這意味着對象創建的initWithWindowNibName和具有GUI的實際窗口是不同的。當我嘗試傳回值時,顯然存儲的指向「主」窗口的指針也爲空。我懷疑這個問題是在NIB設置 - 真的讓我感到困惑。我懷疑是代碼/ NIB關係的常見誤解,我嘗試過不同的事情,但仍然無法實現。任何指導都會非常有用。
-(void) setValuesToURL:(NSString*)strServiceURL storageParam:(NSString*) strStorageURL callerWindowPtr:(AppletAppDelegate *)_callerWindow
{
@try {
NSLog(@"setValuesToURL was called with params %@ , %@" , strServiceURL , strStorageURL);
self.strDataStorageURL = strStorageURL;
self.strServerURL = strServiceURL;
self.callerWindow = _callerWindow;
[textServerURL setStringValue:[self strServerURL]];
[textDataStorageURL setStringValue:[self strDataStorageURL] ];
NSLog(@" after assigmnemnt %@ , %@" , [self strDataStorageURL], [self strServerURL]);
}
@catch (NSException * e) {
NSLog(@"exception inf0 %@ " ,[[ e userInfo] descriptionInStringsFileFormat]);
}
@finally {
}
}
好消息 - 現在將字符串值分配給NSTextField對象。我對「孩子」窗口中的按鈕和點擊我想再打父指針窗口methid:
- (IBAction) saveConfigurationSetings: (id) sender
{
NSLog(@"saveConfigurationSetings: (id) sender");
//close window and pass back URL strings
self.strServerURL = [textServerURL stringValue];
self.strDataStorageURL = [textDataStorageURL stringValue];
[self.callerWindow passMeBackData: [textServerURL stringValue] strStorageURLParam: [textDataStorageURL stringValue]];
[self close];
}
這裏callerWindow變量爲空和passMeBackData失敗。以下是聲明在H:
@interface ConfigurationWindowController : NSWindowController {
...
AppletAppDelegate *callerWindow;
...
}
@property (assign) AppletAppDelegate * callerWindow;
在M檔
@synthesize callerWindow;
非常感謝Williham。這個特定的設計(壞事™)是客戶要求的,我既沒有時間也沒有時間去改變這個要求。所以我必須創建一些自定義設置窗口,實際上是2個Windows應用程序... – David 2011-05-16 21:16:06
@David:不需要時間;短語「那不行」。創造奇蹟。 – 2011-05-16 21:21:45
@大衛:但在一個相關的說明;根據需要使用[sender tag],'NSUserDefaults'和通知仍然可以解決您的問題,並且幾乎肯定是您的方法。 – 2011-05-16 21:23:23