【问题标题】:How can I send Button Event - UIControlEventTouchUpInside如何发送按钮事件 - UIControlEventTouchUpInside
【发布时间】:2011-09-19 13:05:00
【问题描述】:

在我的简单程序中,按钮有问题。按钮是视图的子视图,视图是 myViewController.view 的子视图。

此按钮是动态创建的,但它的操作不起作用。如何发送事件?

这是我的代码。

//视图控制器

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{

    bookCount = 0;

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        UIView *subview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
        [self.view addSubview:subview];
        UIButton  *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [btn setFrame: subview.bounds];
        [btn addTarget:self action:@selector(onBtnchild:) forControlEvents:UIControlEventTouchUpInside];
        [subview addSubview:btn];
        .....
    }
    return self;

}
-(void) onBtnchild:(id) sender{
    NSLog(@"HI");
}

我可以看到我的按钮,但我无法点击。你有什么想法请教我。

【问题讨论】:

    标签: objective-c ios uibutton subview viewcontroller


    【解决方案1】:

    你的代码应该是这样的:

     UIView *subview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
    
     UIButton  *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
     [btn setFrame: subview.bounds];
    
     [btn addTarget:self action:@selector(onBtnchild:) 
             forControlEvents:UIControlEventTouchUpInside];
    
     [subview addSubview:btn];
    
     [self.view addSubview:subview];
    

    【讨论】:

    • 我一直在处理你的代码。但我找不到区别。怎么了?
    • 谢谢。它工作正常。但我还有另一个问题。当我将 imageView 添加到超级按钮时。它不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多