2012-03-10 64 views
0

我有自定義的UIViewController,它看起來像這樣。UIScrolllView和信息按鈕

@implementation UIViewControllerCustom 


- (id)init 
{ 
    self = [super init]; 
    if (self) { 
     NSLog(@"init"); 
    } 

    return self; 
} 



-(void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight]; 
    [infoButton setFrame:CGRectMake(290, 10, 16, 16)]; 
    [infoButton addTarget:self action:@selector(showInfo) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:infoButton]; 
} 

-(void)showInfo 
{ 
    About *about = [[About alloc]init]; 
    NSLog(@"touched"); 
    [about setModalTransitionStyle:UIModalPresentationFullScreen]; 
    [self presentModalViewController:about animated:YES]; 
} 

@end 

我的應用程序中的任何視圖控制器都會繼承此控制器。當信息按鈕被觸摸時,模態窗口出現。問題是我的一個視圖控制器使用UIScrollView。在這個控制器中,信息按鈕是可見的,它會對觸摸做出反應。問題是,當我觸摸按鈕showInfo方法的UIViewControllerCustom不被調用。在其他控制器中我沒有這樣的問題。 這裏有什麼可能是錯誤的?

回答

1

因爲UIScrollView處理正在發送的消息。在你的情況下,觸摸按鈕將由UIScrollView處理。

我建議你繼承UIScrollView並實現:- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view來確定UIScrollView是否應該處理事件。

的實施可能是沿着線的東西:

- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view { 
    if ([view isKindOfClass:[UIButton class]]) { 
     return NO; 
    } 

    return YES; 
} 
+0

當我使用的代碼,你建議我得到控制只的滾動視圖內按鈕。我仍然無法控制信息按鈕。 – OhDoh 2012-03-10 15:48:55

+0

啊,我想你的UIViewController的視圖屬性是UIScrollView。您的UIScrollView是UIViewController中self.view的子視圖嗎?如果是這種情況,那麼不僅僅是子視圖的順序阻止你與UIButton交互?如果你這樣做會發生什麼:[self.view sendSubviewToBack:scrollView];在你的UIViewController? – 2012-03-10 16:31:07