2011-11-01 44 views
0

我的iPad應用程序當前是這樣構建的:xml數據從我的網站中提取並在View Controller「FishIDView」中解析。這個視圖控制器有一個UIScrollView「scrollView」,它有一個子視圖「XMLView」,它是一個UIView。 scrollView被設置爲根據XML中包含的項目數來滾動和瀏覽XMLViews。例如12項= 12頁,或XMLView的實例。這些頁面包含單個魚的照片。這些頁面中的每一個都包含一個按鈕,該按鈕創建名爲「flipView」的UIView「XMLView」的子視圖,並通過從左側動畫過渡翻轉顯示子視圖。這個子視圖包含有關圖片魚的信息。 flipView有一個按鈕,將用戶返回(向後翻轉)魚圖片。通過子視圖中包含的IBAction禁用滾動

我的問題是,當我在查看flipView(它是scrollview子視圖的XMLView的子視圖)時,滾動仍處於啓用狀態。如果我在flipView上向左或向右滾動,則會看到下一條魚的XMLView。我想在查看flipView時禁用滾動功能。

對於我能做些什麼來發送像setScrollEnabled:NO命令來從子視圖scrollView(在FishIDView上)的任何建議嗎?

編輯:

所以我想我需要使用的協議和委託。我想我可以弄明白,但是我正在趕上執行。

在我XMLView.h(留出多餘的代碼):

@protocol XMLViewDelegate <NSObject> 

- (void) stopScrolling; 

@end 

@interface XMLView : UIView 
{ 
    ... 
    id      secondDelegate; 
} 
... 
@property (nonatomic, retain) id <XMLViewDelegate> secondDelegate; 
... 

@end 

然後在我的XMLView.m(與IBAction爲鉤到一個UIButton其正常工作):

... 
@synthesize secondDelegate; 
... 
-(IBAction)goToInfo 
{ 
    //[self newPage]; 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:1.0]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self cache:YES]; 
    [self.secondDelegate stopScrolling]; 
    [self addSubview:flipView]; 
    ... 

    NSLog(@"button pressed"); 

} 

在FishIDView.h:

@interface FishIDView : UIViewController <XMLViewDelegate> 

FishIDView.m:

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

    [scrollView setScrollEnabled:YES]; 

    XMLView *subsubView = [[XMLView alloc] init]; 
    subsubView.secondDelegate = self; 
    ... 
} 

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

當我點擊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"); 

} 

然後在FishIDView.h

-(void) stopScrolling; 

FishIDView.m

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

的 「沒有更多的滾動!!!」打印,但scrollView仍然啓用滾動!爲什麼它會運行stopScrolling的NSLog部分,但不是setScrollEnabled:NO?誰能幫我?

回答

2

解決!我結合了通知而不是直接從子視圖調用方法。

我的設置快速概述。 UIViewController FishIDView包含一個名爲scrollView的UIScrollView。 scrollView有一個子視圖XMLView。 XMLView有一個顯示其子視圖flipView的按鈕。我想在flipView可見時在scrollView上禁用滾動。

在XMLView.h(與不相關的代碼省略):

#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 

在XMLView.m:

#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 

確保把#進口 「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 

然後在FishIDView.m

- (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!!"); 
} 

如果任何人有疑問或具有實施這個麻煩,我會很樂意分享更多我的代碼。我只是省略了大部分內容,因爲它並不涉及這個主題。

相關問題