Interface Builder 窗口操作

打开关闭

关闭窗口:[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();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值