Interface Builder 窗口操作

来源:互联网 发布:网络麻将赌博平台 编辑:程序博客网 时间:2024/05/17 09:02

打开关闭

关闭窗口:[theWindow close];

关闭窗口并释放内存:

[theWindow setReleasedWhenClosed:YES]

[theWindow close];

新建窗口:theWindow=[[NSWindow alloc] init];

显示窗口:[theWindow makeKeyAndOrderFront:self];

隐藏显示

隐藏窗口:

-(IBAction) hideWindow:(id)sender{

[theWindow orderOut:sender];

}

显示窗口:[theWindow orderFront:sender];

查看窗口状态:[theWindow isVisible]

设置窗口作为主窗口:在awakeFromNib方法(当应用程序启动,窗口资源从NIB文件中加载时开始执行)中实现

-(void)awakeFromNib{

[theWindow makeKeyAndOrderFront:nil];

}

定位

-(void)frame 方法以NSRect结构返回窗体当前位置信息

typeof struct _NSRect{

NSPoint origin;

NSSize size;

}NSRect;

-(void)setFrameOrigin:(NSPoint)origin 方法设置顶点位置

-(void)setFrame:(NSRect)frame 方法设置窗口

NSRect theFrame=[theWindow frame];

theFrame.origin.x=theFrame.origin.x/2;

theFrame.size.width=theFrame.size.with/2;

[theWindow setFrame:theFrame display:YES];

使窗体位于屏幕中心 :[theWindow center];

修改窗口显示状态(正常<->全屏):[theWindow zoom];

跟踪

如果窗口具体:有标题栏,窗口大小可变,可以作为主窗口,这三个属性,则创建时Cocoa自动把它加到窗口列表中,不满足其中一项就会从列表中移出,都满足时也可在窗口加载时通过下面方法移出

[theWindow setExcludedFromWindowsMenu:nil];

显示

窗体风格要跟窗体的作用一致

窗口标题:[theWindow setTitle:@"MyWindow"];

标题显示文件名,点击文件可打开文件:[theWindow setTitleWithRepresentedFilename:theFileName];

设置窗体透明度:[theWindow setAlphaValue:num];(num:0->1:透明->实体)

在一个窗体中新建另一个窗体

从Library中拉一个Panel到XIB 项目中

分别向Panel和主窗体中加入一个按钮用来显示和关闭窗体

从Library中拉一个Object到XIB项目中,重新命名为MyObject,并添加两个Class Outlets(theWindow,theSheet),和两个Class Actions(openSheet,closeSheet),并为此创建类文件MySheet

绑定:Panel<>theSheet,Window(主窗体)<>theWindow,以及openSheet跟closeSheet分别绑定到主窗体跟Panel中的Button上

定义类文件MySheet:

#import <Cocoa/Cocoa.h>

@interface MySheet : NSObject {

IBOutlet id theWindow;

IBOutlet id theSheet;

}

- (IBAction) openSheet:(id)sender;

- (IBAction) closeSheet:(id)sender;

@end

 

 

#import "MySheet.h"

@implementation MySheet

- (IBAction) openSheet:(id)sender{

[NSApp beginSheet: theSheet

modalForWindow:theWindow

modalDelegate:self

didEndSelector:NULL

  contextInfo:nil];

}

- (IBAction) closeSheet:(id)sender{

[theSheet orderOut:nil];

[NSApp endSheet: theSheet];

}

@end

 

响应窗口事件

通过代理实现

在MainMenu.xib 中Control + 拖动Window窗体到MyObject(从Library拉入XIB 项目中的Object),选中Delegate

在Xcode界面下MyObject对应类的implementation文件中输入系统已定义的事件代理的实现:最小化时发出一个系统提供提声音(windowDidMiniaturize为系统已定义的事件代理

-(void)windowDidMiniaturize:(NSNotification *)notification{

NSBeep();

}

原创粉丝点击