Xcode 通过子视图中包含的iAction禁用滚动

Xcode 通过子视图中包含的iAction禁用滚动,xcode,uiview,delegates,uiscrollview,ibaction,Xcode,Uiview,Delegates,Uiscrollview,Ibaction,我的iPad应用程序目前是这样构建的:xml数据从我的网站中提取,并在视图控制器“FishIDView”中解析。此视图控制器有一个UIScrollView“scrollView”,它有一个子视图“XMLView”,它是一个UIView。scrollView被设置为根据XML中包含的项目数在XMLView中滚动和分页。例如,12项=12页,或XMLView实例。这些页面包含个别鱼类的照片。每个页面都包含一个按钮,用于创建UIView“XMLView”的子视图,称为“flipView”,并通过从左翻

我的iPad应用程序目前是这样构建的:xml数据从我的网站中提取,并在视图控制器“FishIDView”中解析。此视图控制器有一个UIScrollView“scrollView”,它有一个子视图“XMLView”,它是一个UIView。scrollView被设置为根据XML中包含的项目数在XMLView中滚动和分页。例如,12项=12页,或XMLView实例。这些页面包含个别鱼类的照片。每个页面都包含一个按钮,用于创建UIView“XMLView”的子视图,称为“flipView”,并通过从左翻转动画过渡显示子视图。此子视图包含有关图中鱼的信息。flipView有一个按钮,用于将用户返回(翻转回)到fish图片

我的问题是,当我查看flipView(它是XMLView的子视图,XMLView是scrollView的子视图)时,滚动仍然处于启用状态。如果在flipView上向左或向右滚动,我会看到下一条鱼的XMLView。我希望在查看flipView时禁用滚动

关于如何从子视图发送setScrolEnabled:NO命令到scrollView(在FishIDView上)之类的内容,有什么建议吗

编辑:

所以我假设我需要使用协议和委托。我原以为我能弄明白,但我正在抓紧实施

在我的XMLView.h中(省略无关代码):

在FishIDView.h中:

@interface FishIDView : UIViewController <XMLViewDelegate>
当我在XMLView中单击触发“goToInfo”的按钮时,其余的操作都会发生,但“不再滚动!!”日志永远不会显示

我走错方向了吗?我仍在试图找出代表,因此我可能完全错误地使用了这种方法

编辑2:

我现在试着摆脱委托,走一条听起来更简单的路线,但它仍然不能正常工作

在XMLView.m中:

-(IBAction)goToInfo
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self cache:YES];

    //The next 2 lines are what I added
    ViewController *mainView = [[ViewController alloc] init];
    [mainView stopScrolling];

    [self addSubview:flipView];
    [infoButton setEnabled:FALSE];
    [infoButton setHidden:YES];
    [title setHidden:YES];
    [UIView commitAnimations];

    NSLog(@"button pressed");

}
#import "XMLView.h"

@implementation XMLView

@synthesize infoButton, closeButton;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

//Allows button to be pressed while contained in subview of scrollview
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    CGPoint hitPoint = [infoButton convertPoint:point fromView:self];
    CGPoint closePoint = [closeButton convertPoint:point fromView:flipView];
    if ([infoButton pointInside:hitPoint withEvent:event]) return infoButton;
    if ([closeButton pointInside:closePoint withEvent:event]) return closeButton;
    return [super hitTest:point withEvent:event];  
}

-(IBAction)goToInfo
{
    //post notification for FishIDView to listen to to trigger method that disables scrolling
    [[NSNotificationCenter defaultCenter] postNotificationName:kApplicationDidStopScrollingNotification object:nil];

    [self addSubview:flipView];

    //custom transition between views. A cross-fade effect
    flipView.alpha = 0.0f;
    [UIView beginAnimations:@"fadeInSecondView" context:NULL];
    [UIView setAnimationDuration:0.5];
    flipView.alpha = 1.0f;

    [infoButton setEnabled:FALSE];

    [UIView commitAnimations];   
}

- (IBAction)closeSubView
{
    //post notification to resume scrolling
    [[NSNotificationCenter defaultCenter] postNotificationName:kApplicationDidResumeScrollingNotification object:nil];

    flipView.alpha = 1.0f;
    [UIView beginAnimations:@"fadeInSecondView" context:NULL];
    [UIView setAnimationDuration:0.5];
    flipView.alpha = 0.0f;

    [infoButton setEnabled:TRUE];

    [UIView commitAnimations];
    [flipView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1.2];

}

@end
然后在FishIDView.h中

-(void) stopScrolling;
FishIDView.m

-(void) stopScrolling
{
    [scrollView setScrollEnabled:NO];
    NSLog(@"NO more scrolling!!!");
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [scrollView setScrollEnabled:YES];

    //Listen for the notification to stop scrolling
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopScrolling) name:kApplicationDidStopScrollingNotification object:nil];

    //Listen for the notification to resume scrolling
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resumeScrolling) name:kApplicationDidResumeScrollingNotification object:nil];
    [self layoutSubview];
}

-(void) stopScrolling
{    
    [scrollView setScrollEnabled:NO];
    NSLog(@"NO more scrolling!!!");
}

-(void) resumeScrolling
{    
    [scrollView setScrollEnabled:YES];
    NSLog(@"begin scrolling again!!");
}

“不再滚动!!!”将打印,但scrollView仍启用滚动功能!为什么它会运行stopScrolling的NSLog部分而不是setScrolEnabled:否?有人能帮我吗

解决了!我合并了通知,而不是试图直接从子视图调用方法

快速概述我的设置。UIViewController FishIDView包含一个名为scrollView的UIScrollView。scrollView有一个子视图XMLView。XMLView有一个显示其子视图flipView的按钮。当flipView可见时,我希望在scrollView上禁用滚动

在XMLView.h中(省略不相关的代码):

确保将#import“XMLView.h”放在FishIDView.h文件的顶部,并在底部创建-(void)方法

#import <UIKit/UIKit.h>

#import "XMLView.h"

@class AppDelegate;

@interface FishIDView : UIViewController <UIScrollViewDelegate>
{
...
}

@property ...

//These are the 2 methods that turn scrolling on and off
-(void) stopScrolling;
-(void) resumeScrolling;

@end
如果有人对此有疑问或在实现上有困难,我很乐意分享更多我的代码。我只是省略了大部分内容,因为它实际上与这个主题无关

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

#define kApplicationDidStopScrollingNotification @"StopScroll"
#define kApplicationDidResumeScrollingNotification @"ResumeScroll"

#import "FishIDView.h"

@interface XMLView : UIView 
{
    IBOutlet UIButton       *infoButton;
    IBOutlet UIButton       *closeButton;
    IBOutlet UIView         *flipView; 
}

@property (nonatomic, retain) IBOutlet UIButton       *infoButton;
@property (nonatomic, retain) IBOutlet UIButton       *closeButton;

- (IBAction)goToInfo;

- (IBAction)closeSubView;

@end
#import "XMLView.h"

@implementation XMLView

@synthesize infoButton, closeButton;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

//Allows button to be pressed while contained in subview of scrollview
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    CGPoint hitPoint = [infoButton convertPoint:point fromView:self];
    CGPoint closePoint = [closeButton convertPoint:point fromView:flipView];
    if ([infoButton pointInside:hitPoint withEvent:event]) return infoButton;
    if ([closeButton pointInside:closePoint withEvent:event]) return closeButton;
    return [super hitTest:point withEvent:event];  
}

-(IBAction)goToInfo
{
    //post notification for FishIDView to listen to to trigger method that disables scrolling
    [[NSNotificationCenter defaultCenter] postNotificationName:kApplicationDidStopScrollingNotification object:nil];

    [self addSubview:flipView];

    //custom transition between views. A cross-fade effect
    flipView.alpha = 0.0f;
    [UIView beginAnimations:@"fadeInSecondView" context:NULL];
    [UIView setAnimationDuration:0.5];
    flipView.alpha = 1.0f;

    [infoButton setEnabled:FALSE];

    [UIView commitAnimations];   
}

- (IBAction)closeSubView
{
    //post notification to resume scrolling
    [[NSNotificationCenter defaultCenter] postNotificationName:kApplicationDidResumeScrollingNotification object:nil];

    flipView.alpha = 1.0f;
    [UIView beginAnimations:@"fadeInSecondView" context:NULL];
    [UIView setAnimationDuration:0.5];
    flipView.alpha = 0.0f;

    [infoButton setEnabled:TRUE];

    [UIView commitAnimations];
    [flipView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1.2];

}

@end
#import <UIKit/UIKit.h>

#import "XMLView.h"

@class AppDelegate;

@interface FishIDView : UIViewController <UIScrollViewDelegate>
{
...
}

@property ...

//These are the 2 methods that turn scrolling on and off
-(void) stopScrolling;
-(void) resumeScrolling;

@end
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [scrollView setScrollEnabled:YES];

    //Listen for the notification to stop scrolling
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopScrolling) name:kApplicationDidStopScrollingNotification object:nil];

    //Listen for the notification to resume scrolling
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resumeScrolling) name:kApplicationDidResumeScrollingNotification object:nil];
    [self layoutSubview];
}

-(void) stopScrolling
{    
    [scrollView setScrollEnabled:NO];
    NSLog(@"NO more scrolling!!!");
}

-(void) resumeScrolling
{    
    [scrollView setScrollEnabled:YES];
    NSLog(@"begin scrolling again!!");
}