【问题标题】:UIButton not calling selectorUIButton 没有调用选择器
【发布时间】:2013-05-28 23:51:33
【问题描述】:

我正在以编程方式创建UIButton,但是当我点击按钮时,它根本不会调用选择器。该按钮是通过以下方法创建的:

- (UIButton*) createNewsButtonFromItem: (MWFeedItem*) item origin: (CGPoint) origin color: (UIColor*) color
{
    UIButton* titleButton = [UIButton buttonWithType: UIButtonTypeCustom];

    titleButton.frame = CGRectMake(origin.x, origin.y, CGRectGetWidth(self.mainScrollView.frame), CGRectGetHeight(self.mainScrollView.frame));

    int size;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        size = IPAD_TEXT_SIZE;
    }
    else
    {
        size = IPHONE_TEXT_SIZE;
    }
    titleButton.titleLabel.font = [UIFont fontWithName: @"STHeitiTC-Medium" size: size];
    titleButton.titleLabel.minimumScaleFactor = .5;
    titleButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    [titleButton setTitleColor: color forState: UIControlStateNormal];
    [titleButton setTitle: item.title forState: UIControlStateNormal];
    [titleButton addTarget: self action: @selector(openInWebView) forControlEvents: UIControlEventTouchUpInside];

    [titleButton.titleLabel setNumberOfLines: 0 ];
    [titleButton.titleLabel sizeToFit];

    titleButton.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    return titleButton;
}

在创建视图的方法中被调用:

- (UIView*) viewForItem: (MWFeedItem*) item
{
    // figure out if it should be veritical
    BOOL heads = arc4random() % 2;
    BOOL tails = arc4random() % 2;
    CGPoint origin = self.mainScrollView.contentOffset;
    CGSize size = self.mainScrollView.bounds.size;

    // define the four possible points
    CGPoint left = CGPointMake(origin.x - size.width, origin.y);
    CGPoint right = CGPointMake(origin.x + size.width, origin.y);
    CGPoint top = CGPointMake(origin.x, origin.y - size.height);
    CGPoint bottom = CGPointMake(origin.x, origin.y + size.height);

    // set up frames 
    BOOL sideways = heads;
    CGPoint point1 = sideways ? (tails ? left : right) : (tails ? top : bottom);
    CGPoint point2 = sideways ? (tails ? right : left) : (tails ? bottom : top);
    UIColor* color = isBlack ? [UIColor blackColor] : [UIColor whiteColor];
    UIColor* otherColor = isBlack ? [UIColor whiteColor] : [UIColor blackColor];

    UIButton* titleButton = [self createNewsButtonFromItem: item origin: point1 color: color];
    UIView* background = [self createBackgroundViewOrigin: point2 color: otherColor];

    UIView* container = [[UIView alloc] initWithFrame: self.mainScrollView.bounds];

    [container addSubview: background];
    [container addSubview: titleButton];

    return container;

}

我正在使用上述方法创建一个视图并将其放置在我的主容器视图中:

UIView* view = [self viewForItem: item];
[self.mainScrollView addSubview: view];    
[self animateIntoPlace: view];

然后将其动画到框架的中心:

- (void) animateIntoPlace: (UIView*) view
{
    // find out frame of current view
    CGPoint origin = self.mainScrollView.contentOffset;
    CGPoint center = CGPointMake(origin.x + CGRectGetWidth(self.mainScrollView.frame) / 2, origin.y + (CGRectGetHeight(self.mainScrollView.frame) / 2));

    for (UIView* subview in view.subviews)
    {
        [UIView animateWithDuration: .3 animations:^{
            subview.center = center;
        }];
    }
}

当按钮被点击时,它应该调用以下方法,但从不点击它:

- (void) openInWebView
{
    UIViewController* container = [[UIViewController alloc] init];

    // create webview
    UIWebView* webView = [[UIWebView alloc] initWithFrame: container.view.frame];
    webView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

    // load webview with URL
    NSString* link = ((MWFeedItem*)[self.mainQueue objectAtIndex: currentIndex]).link;
    NSURL* url = [NSURL URLWithString: link];
    NSURLRequest* request = [[NSURLRequest alloc] initWithURL: url];
    [webView loadRequest: request];

    // close button
    UIButton* closeButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
    [closeButton setTitle: @"X" forState: UIControlStateNormal];
    [closeButton addTarget: self action: @selector(closeWebView) forControlEvents: UIControlEventTouchUpInside];
    closeButton.frame = CGRectMake(5, 5, 30, 30);

    // add views to the container
    [container.view addSubview: webView];
    [container.view addSubview: closeButton];

    [self presentViewController: container animated: YES completion:^{
        shouldCycle = NO;
    }];

}

有人知道这有什么问题吗?我以前做过一百万次,但无法弄清楚为什么它不起作用。按钮不应该花这么长时间来制作 >_

【问题讨论】:

  • 按钮颜色是否会像被按下一样改变。换句话说,您确定没有其他视图挡住它吗?
  • @JustinMeiners 不,不是,好电话。我将在调试器中检查子视图堆栈

标签: objective-c user-interface uiview uibutton


【解决方案1】:

你可能忘记设置视图的userInteractionEnabled = YES 在你的情况下:

UIView* view = [self viewForItem: item];
view.userInteractionEnabled = YES;

【讨论】:

  • 不是这个视图,但我必须在UIScrollView 容器上启用用户交互
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-28
相关资源
最近更新 更多