Objective c 按钮单击不注册

Objective c 按钮单击不注册,objective-c,uibutton,xcode5,Objective C,Uibutton,Xcode5,我有一个Xcode 5按钮问题 我在UIViewController上以编程方式创建了许多按钮,然后想注册对这些按钮的单击。我使用NSLog跟踪点击,但似乎没有在日志中注册点击 有人能帮忙吗 最终目的是在每次点击按钮时切换到下一个UIViewController,但只是测试是否首先注册了按钮点击 下面是我如何在viewDidLoad中创建按钮(仅显示1个)。 注意:if语句仅用于检查在上一个视图中按下了哪些按钮 - (void)viewDidLoad { [super viewDidLo

我有一个Xcode 5按钮问题

我在
UIViewController
上以编程方式创建了许多按钮,然后想注册对这些按钮的单击。我使用
NSLog
跟踪点击,但似乎没有在日志中注册点击

有人能帮忙吗

最终目的是在每次点击按钮时切换到下一个
UIViewController
,但只是测试是否首先注册了按钮点击

下面是我如何在
viewDidLoad
中创建按钮(仅显示1个)。 注意:
if语句
仅用于检查在上一个视图中按下了哪些按钮

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view.

    NSString *myString = [NSString stringWithFormat:@"%d", self.tagNumber ];

    if (self.tagNumber == 1)
    {
        hotelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        hotelButton.tag = 1;
        hotelButton.frame = CGRectMake(60, 60, 300.f, 100.f);

        UIImage *buttonImage1 = [UIImage imageNamed:@"buttonImage1.png"];
        [hotelButton setBackgroundImage:buttonImage1 forState:UIControlStateNormal];
        [self.view addSubview:hotelButton];

        // Add targets and actions
        [hotelButton addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpOutside];
    }
}

下面是我检查按钮点击的方法

- (void) buttonClicked
{
    NSLog(@"Button %i clicked", hotelButton.tag);
}
注意:我还尝试了以下方法:

- (void) buttonClicked:(UIButton *) button
{
    NSLog(@"Button %ld clicked", (long int)[button tag]);
}
但在我的
viewdiload
中,出现了一个错误“未声明的选择器
按钮已单击”action@selector):

使用以下命令:

[hotelButton addTarget:self action:@selector(buttonClicked) 
    forControlEvents:UIControlEventTouchUpInside];

来自苹果文档:

UIControlEventTouchUpInside
A touch-up event in the control where the finger is inside the bounds of the control.

UIControlEventTouchUpOutside
A touch-up event in the control where the finger is outside the bounds of the control
并保留此方法:

- (void) buttonClicked
{
    NSLog(@"Button %i clicked", hotelButton.tag);
}
使用以下命令:

[hotelButton addTarget:self action:@selector(buttonClicked) 
    forControlEvents:UIControlEventTouchUpInside];

来自苹果文档:

UIControlEventTouchUpInside
A touch-up event in the control where the finger is inside the bounds of the control.

UIControlEventTouchUpOutside
A touch-up event in the control where the finger is outside the bounds of the control
并保留此方法:

- (void) buttonClicked
{
    NSLog(@"Button %i clicked", hotelButton.tag);
}
试试这个

[hotelButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
然后递给发送者

- (void) buttonClicked:(UIButton*)sender
{
    NSLog(@"Button %i clicked", [sender tag]);
}
试试这个

[hotelButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
然后递给发送者

- (void) buttonClicked:(UIButton*)sender
{
    NSLog(@"Button %i clicked", [sender tag]);
}

试着从外面换到里面

[hotelButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

试着从外面换到里面

[hotelButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

已修改您的if条件。请注意:-

if (self.tagNumber == 1)
    {
        hotelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        hotelButton.tag = 1;
        hotelButton.frame = CGRectMake(60, 60, 300.f, 100.f);

        UIImage *buttonImage1 = [UIImage imageNamed:@"buttonImage1.png"];
        [hotelButton setBackgroundImage:buttonImage1 forState:UIControlStateNormal];
hotelButton.showsTouchWhenHighlighted = YES;
hotelButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
        [hotelButton addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:hotelButton];

    }

已修改您的if条件。请注意:-

if (self.tagNumber == 1)
    {
        hotelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        hotelButton.tag = 1;
        hotelButton.frame = CGRectMake(60, 60, 300.f, 100.f);

        UIImage *buttonImage1 = [UIImage imageNamed:@"buttonImage1.png"];
        [hotelButton setBackgroundImage:buttonImage1 forState:UIControlStateNormal];
hotelButton.showsTouchWhenHighlighted = YES;
hotelButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
        [hotelButton addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:hotelButton];

    }

很好的发现,完全错过了。一定是按错了回车键。谢谢。很好,完全错过了。一定是按错了回车键。谢谢