2013-03-21 75 views
1

我有一類名爲 「菜單」 創建的UIButton添加的UIButton到的ViewController:從自定義類

.h文件中:

@interface Menu : NSObject 

@property (strong) Forme *forme; 
@property (strong) NSString *imageButtonName; 
@property (strong) UIButton *button; 

- (id) initWithClef:(int)clef hauteur:(int)hauteur largeur:(int)largeur posY:(int)posY posX:(int)posX alpha:(float)alpha imageButtonName:(NSString*)imageButtonName button:(UIButton*)button; 

@end 

.m文件:

#import "Menu.h" 

@implementation Menu 

@synthesize forme = _forme; 
@synthesize button = _button; 
@synthesize imageButtonName = _imageButtonName; 

- (id) initWithClef:(int)clef hauteur:(int)hauteur largeur:(int)largeur posY:(int)posY posX:(int)posX alpha:(float)alpha imageButtonName:(NSString *)imageButtonName button:(UIButton *)button 
{ 
    if ((self = [super init])) 
     { 
      NSLog (@"test2"); 
      self.forme =[[Forme alloc]initWithClef:clef hauteur:hauteur largeur:largeur posY:posY posX:posX alpha:alpha]; 
      self.button = button; 
      [button setFrame:CGRectMake(largeur, hauteur, posX, posY)]; 
      [button setBackgroundImage:[UIImage imageNamed:imageButtonName] forState:UIControlStateNormal]; 
     } 
    return self; 
} 

我想自動將創建的按鈕顯示到ViewController視圖。有人可以幫助我嗎?

回答

0

以及OnView參數添加到您的initClef ....

- (id) initWithClef:(int)clef hauteur:(int)hauteur largeur:(int)largeur posY:(int)posY posX:(int)posX alpha:(float)alpha imageButtonName:(NSString *)imageButtonName button:(UIButton *)button onView:(UIView *)view 
{ 
    if ((self = [super init])) 
    { 
     NSLog (@"test2"); 
//  self.forme =[[Forme alloc]initWithClef:clef hauteur:hauteur largeur:largeur posY:posY posX:posX alpha:alpha]; 
     self.button = button; 
     [button setFrame:CGRectMake(largeur, hauteur, posX, posY)]; 
     [button setBackgroundImage:[UIImage imageNamed:imageButtonName] forState:UIControlStateNormal]; 
     [view addSubview:button]; 

    } 
    return self; 
} 

調用它的viewController

Menu * menu2=[[Menu alloc] initWithClef:1 hauteur:30 largeur:30 posY:30 posX:30 alpha:1 imageButtonName:@"arti.png" button:b onView:self.view]; 
+0

謝謝!它的工作原理..但是現在我想從第三課開始。所以onView不能是「self.view」。你有什麼主意嗎? – alex777771 2013-03-22 09:57:20

+0

我找到了解決方案,謝謝! – alex777771 2013-03-22 10:10:26

0

我想你應該閱讀一些手冊和教程

只需添加此按鈕的ViewController的看法在init方法:

[button setBackgroundImage:[UIImage imageNamed:imageButtonName] forState:UIControlStateNormal]; 
[self.view addSubview:button]; 
+0

我都嘗試過,但它不工作。我還試過:UIViewController * newview = [[UIViewController alloc] init]; [newview.view addSubview:self.button]; – alex777771 2013-03-21 10:05:12

+0

分享到更多的代碼來幫助 – 2013-03-21 10:06:25

+0

在我的viewDidLoad方法:Menu * play = [[Menu alloc] initWithClef:1 hauteur:100 largeur:300 posY:100 posX:50 alpha:1 imageButtonName:@「play.png」button: [[UIButton alloc] init]];比如果我添加[self.view addSubview:按鈕];在viewDidLoad它的作品。但我真的想在init方法中自動顯示我的按鈕。謝謝 – alex777771 2013-03-21 10:16:07

0

我認爲,簡單地增加一個按鈕到視圖應該做的的伎倆, 電話:

[self.view addSubview:button] 

你的按鈕,你S中的初始化之後直到需要使用addSubview將該按鈕添加到視圖,否則您的按鈕不會在屏幕上繪製。

+0

謝謝,但它不起作用.. – alex777771 2013-03-21 10:06:36

+0

檢查你的參數在你的初始化。那些有效的寬度和高度? – 2013-03-21 10:10:25

+0

Yes:Menu * play = [[Menu alloc] initWithClef:1 hauteur:100 largeur:300 posY:100 posX:50 alpha:1 imageButtonName:@「play.png」button:[[UIButton alloc] init]]; – alex777771 2013-03-21 10:18:24