0

我正在以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;
    }];

}

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

4

1 回答 1

3

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

UIView* view = [self viewForItem: item];
view.userInteractionEnabled = YES;
于 2013-05-28T15:51:33.527 回答