我不能以编程方式制作UIButton

时间:2014-02-19 05:17:41

标签: ios uibutton

我搜索了所有的互联网,似乎每个关于这个话题的帖子都遗漏了我需要的一条重要信息。无论人们告诉我做什么,我都无法摆脱关于我的按钮操作方法的所有/任何事情的“未声明的标识符”错误。我已经选择了这么多的代码,以至于它完全被顶起来了。任何人都可以告诉我我需要做什么才能做出正确的互动按钮?用于viewcontroller的.m文件和.h文件中的所有内容?
请不要认为我已经有了一些重要的代码,因为我可能没有。我是Objective-c的新手。
谢谢!!!!!!!! (顺便说一下,这是ipad,我正避免使用故事板)谢谢!!我在这一点上非常沮丧。

这是我到目前为止的代码

    UIButton *add = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[add addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchDown];
[add setTitle:@"add new" forState:UIControlStateNormal];
add.frame = CGRectMake(100, 100, 100, 100);
[objects addSubview:add];
- (void)aMethod:(UIButton*)button
{
    NSLog(@"Button  clicked.");
}

问题不在于它运行然后崩溃,问题是我无法运行它,只要我有这个错误“使用未声明的标识符'aMethod'”

4 个答案:

答案 0 :(得分:5)

请尝试此代码

  UIButton *submitButton=[[UIButton alloc]initWithFrame:CGRectMake(120, 300, 70, 50)];
[submitButton.layer setCornerRadius:10.0]; 
[submitButton setTitle:@"Submit" forState:UIControlStateNormal];
[submitButton addTarget:self action:@selector(submitAction:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:submitButton];

答案 1 :(得分:5)

 .what is object in this line     [object addSubview:add];
    try this

    UIButton *add = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [add addTarget:self action:@selector(aMethod) forControlEvents:UIControlEventTouchDown];
    [add setTitle:@"add new" forState:UIControlStateNormal];
    add.frame = CGRectMake(100, 100, 100, 100);
     [object addSubview:add];


    - (void)aMethod
    {
        NSLog(@"Button  clicked.");
    }

答案 2 :(得分:2)

试试这个。

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];

答案 3 :(得分:0)

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];

按下按钮后,它将自动查找buttonClicked:动作事件

-(void) buttonClicked:(UIButton *)clicked{ //Your action goes here...}