2013-05-28 1 views
0

프로그래밍 방식으로 UIButton을 만들고 있지만 버튼을 누르면 선택기가 전혀 호출되지 않습니다. 이 버튼은 다음과 같은 방법으로 생성됩니다UIButton 호출 선택기가 없습니다.

- (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; 

} 

내가보기를 만들고에 배치하기 위해 위의 방법을 사용하고 있습니다 : 방법에 호출되고

- (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; 
} 

보기를 만들 내 주요 컨테이너보기 :

- (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; 
     }]; 
    } 
} 
: 다음

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

그리고 프레임의 중심으로 애니메이션

이 버튼은 다음 메소드를 호출하기로되어 있지만, 결코 그것을 명중하지 있어요 탭 경우 :

- (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; 
    }]; 

} 

사람이 뭐가 잘못 볼 수 있습니까? 나는이 일을 전에도 한 번 해봤지만 왜 효과가 없는지 골라 낼 수 없습니다. <

+0

버튼의 색상 변경을 수행합니다. 다른 말로하면 다른보기가 그것을 차단하지 않는다는 것입니까? –

+0

@JustinMeiners 아니, 잘 했어. 디버거에서 서브 뷰 스택을 검사합니다. – Chris

답변

3

당신은 아마 설정하는 것을 잊었다 버튼이 _> 만들기 위해이 오래 걸리지 않을 겁니다 뷰의 사용자의 경우 userInteractionEnabled = YES :이 눌러 진 것처럼

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

이 뷰가 아니었지만 UIScrollView 컨테이너에서 사용자 상호 작용을 활성화해야했습니다. – Chris

관련 문제