UIButton没有调用选择器

时间:2013-05-28 15:47:18

标签: objective-c user-interface uiview uibutton

我正在以编程方式创建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;
    }];

}

有人看到这有什么问题吗?我之前已经完成了一百万次,无法找出为什么它不起作用。一个按钮不应该花这么长时间来制作> _<

1 个答案:

答案 0 :(得分:3)

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

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