我有两个类,firstView& secondView。 在firstView.h中
#import "SecondView.h" #import "FirstView" @interface FirstView : NSObject { IBOutlet NSButton *test; secondView *mySecond; } @property (nonatomic, retain) IBOutlet NSButton *test; -(IBAction) buttonClicked:(id)sender; @end
在firstView.m
中#import "FirstView.h" @implementation FirstView @synthesize test; -(IBAction) buttonClicked:(id)sender{ NSLog(@"HELLO!!!"); if (!mySecond) { // If the second view controller doesn't exist yet, make it! mySecond = [[secondView alloc] init]; } [mySecond displayWindow]; } @end
在secondView.h中
#import @interface SecondView : NSObject { IBOutlet NSWindow *progressWindow; IBOutlet NSButton *testNew; } @property (nonatomic, retain) IBOutlet NSButton *testNew; - (void)displayWindow; -(IBAction) buttonClickedNew:(id)sender; @end
在secondView.m
中#import "SecondView.h" @implementation SecondView @synthesize testNew; - (id)init { if (self = [super init]) { NSLog(@"HAI!!"); [NSBundle loadNibNamed:@"Next" owner:self]; } return self; } - (void)displayWindow { if (![progressWindow isVisible]) { NSLog(@"ON SECOND!!!"); [progressWindow setIsVisible:YES]; [progressWindow orderFront:nil]; } } -(IBAction) buttonClickedNew:(id)sender { NSLog(@"GOOD!!!"); } @end
但我得到了输出......
Running… 2011-04-05 16:12:30.400 toDoListMac[6558:a0f] HELLO!!! 2011-04-05 16:12:30.402 toDoListMac[6558:a0f] HAI!! 2011-04-05 16:12:30.404 toDoListMac[6558:a0f] HAI!! 2011-04-05 16:12:30.406 toDoListMac[6558:a0f] HAI!! 2011-04-05 16:12:30.408 toDoListMac[6558:a0f] HAI!! 2011-04-05 16:12:30.409 toDoListMac[6558:a0f] HAI!! 2011-04-05 16:12:30.410 toDoListMac[6558:a0f] HAI!! 2011-04-05 16:12:30.411 toDoListMac[6558:a0f] HAI!! 2011-04-05 16:12:30.413 toDoListMac[6558:a0f] HAI!! 2011-04-05 16:12:30.414 toDoListMac[6558:a0f] HAI!! 2011-04-05 16:12:30.415 toDoListMac[6558:a0f] HAI!!
secondView'init'无限次调用! 请...请帮助我......
我想要做的是......我想在特定动作上打开另一个xib /窗口,按下按钮点击....请帮助我......
答案 0 :(得分:0)
在Cocoa中有一个控制窗口的类:NSWindowController你为什么不使用它?子类化NSWindowController为您提供所有的细节和螺栓,然后您只需要添加特定于您的用例的方法。简单得多。
此外,目前尚不清楚这些是单独的窗口还是单独的视图。如果是后者,您需要调查NSViewController