在OS X应用程序中编程创建和定位NSButton? [英] Programatically create and position an NSButton in an OS X app?

查看:203
本文介绍了在OS X应用程序中编程创建和定位NSButton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Mac OS X Cocoa应用程序中以编程方式创建和定位按钮。任何帮助将不胜感激...

I need to programatically create and position a button in a Mac OS X Cocoa application. Any help would be appreciated...

推荐答案

到possits按钮你需要改变按钮的起源x和y。看下面我写的示例代码和注释。

To possition button You need to change the button's origins x and y. Look at the sample code which I wrote below and comments.

你可以这样做:

-(void)awakeFromNib {

    //Start from bottom left corner

    int x = 100; //possition x
    int y = 100; //possition y

    int width = 130;
    int height = 40; 

    NSButton *myButton = [[[NSButton alloc] initWithFrame:NSMakeRect(x, y, width, height)] autorelease];
    [[windowOutlet contentView] addSubview: myButton];
    [myButton setTitle: @"Button title!"];
    [myButton setButtonType:NSMomentaryLightButton]; //Set what type button You want
    [myButton setBezelStyle:NSRoundedBezelStyle]; //Set what style You want

    [myButton setTarget:self];
    [myButton setAction:@selector(buttonPressed)];
}

-(void)buttonPressed {
    NSLog(@"Button pressed!"); 

    //Do what You want here...  
}

** WindowOutlet 是窗口,因此不要忘记使用IBOutlet。

** WindowOutlet is window so don't forget to IBOutlet it.

这篇关于在OS X应用程序中编程创建和定位NSButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆