2012-04-20 53 views

回答

16

正位按鈕您需要更改按鈕的原點x和y。看看我在下面寫的代碼和評論。

你可以這樣說:

-(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中。

相關問題