Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 从自定义类向ViewController添加UIButton_Objective C_Uibutton - Fatal编程技术网

Objective c 从自定义类向ViewController添加UIButton

Objective c 从自定义类向ViewController添加UIButton,objective-c,uibutton,Objective C,Uibutton,我有一个名为“菜单”的类创建UIButton: .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 pos

我有一个名为“菜单”的类创建UIButton:

.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视图中自动显示创建的按钮。有人能帮我吗

我认为你应该阅读一些手册和教程

只需在init方法中将此按钮添加到viewcontroller的视图中:

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

我认为只要在视图中添加一个按钮就可以了, 电话:


初始化按钮后,您仍然需要使用addSubview将按钮“添加”到视图中,否则您的按钮将不会在屏幕上绘制。

并将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];

我试过了,但不起作用。我试过:UIViewController*newview=[[UIViewController alloc]init];[newview.view addSubview:self.button];在我的视图中,didload方法e:Menu*play=[[Menu alloc]initWithClef:1 hauteur:100 largeur:300 posY:100 posX:50 alpha:1 imageButtonName:@“play.png”按钮:[[UIButton alloc]init];如果我添加[self.view addSubview:button];在viewDidLoad中,它可以工作。但是我真的想在init方法中自动显示我的按钮。感谢您取代[[UIButton alloc]init]];至[[UIButton Button WithType:UIButtonTypeCustom]retain];是,因为如果我在ViewDidLoad中调用[self.view addSubview:button],我可以显示我的按钮在init中检查参数。这些宽度和高度是否有效?是:菜单*播放=[[Menu alloc]initWithClef:1高分:100大分:300高分:100高分:50阿尔法:1图像按钮名称:@“play.png”按钮:[[UIButton alloc]init];非常感谢。它起作用了。。但现在我想从第三节课开始讲。所以onView不能是“self.view”。你知道吗?
 Menu * menu2=[[Menu alloc] initWithClef:1 hauteur:30 largeur:30 posY:30 posX:30 alpha:1 imageButtonName:@"arti.png" button:b onView:self.view];