0

2 つのサブビューを含むビューがあります。一度に1つのサブビューのみが表示され、各サブビューにはボタンがあり、ボタンをクリックするとサブビューが反転して次のサブビューが表示されるように機能しました。問題は、ビュー全体が反転しているように見えることでした。問題を解決する方法についてこのサイトを読んだ後、サブビューをコンテナーに追加して、代わりにそれを反転しようとしました。ただし、ボタンを押すと最初のサブビューが表示されますが、フリップしなくなりました。それは何もしません。サブビューを反転するメソッドにログステートメントとブレークポイントを入れましたが、私が知る限り、それはもはや呼び出されません。私は xcode と目的の c とデリゲートに非常に慣れていないため、どのように進めればよいかわかりません。どんな助けでも大歓迎です。ありがとう。

関連するコードをここに含めました: ViewController のヘッダー

@interface ExerciseViewController : UIViewController<ExerciseSubViewDelegate>

//stuff for subviews
@property (nonatomic, strong) ExerciseSubViewImage *subViewImage;
@property (nonatomic, strong) ExerciseSubViewText *subViewText;
@property UIView *panel;

@end

これは、ViewController のコードです。

@interface ExerciseViewController ()

@end

@implementation ExerciseViewController

@synthesize subViewImage, subViewText;

- (void)viewDidLoad
{

    self.subViewImage.delegate = self;

    _panel = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.view.bounds.size.width,  self.view.bounds.size.height/2)];
    _panel.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_panel];
   [_panel addSubview:subViewImage];
}
-(ExerciseSubViewImage *)subViewImage
{
   if (!subViewImage)
   {

       CGRect subViewImageFrame = CGRectMake(0,0, _panel.bounds.size.width, _panel.bounds.size.height);
       self.subViewImage = [[ExerciseSubViewImage alloc] initWithFrame:subViewImageFrame];

       [_panel addSubview:subViewImage];

   }
    return subViewImage;
}
-(ExerciseSubViewText *)subViewText
{
    if (!subViewText)
    {

        CGRect subViewTextFrame = CGRectMake(0,0, _panel.bounds.size.width, _panel.bounds.size.height);
        self.subViewText = [[ExerciseSubViewText alloc] initWithFrame:subViewTextFrame];

        self.subViewText.backgroundColor = [UIColor blueColor];
        [_panel addSubview:subViewText];
    }
    return subViewText;
}

-(void)exerciseSubViewImagePressed
{

    [UIView transitionWithView:_panel
                      duration:0.2
                      options:UIViewAnimationOptionTransitionFlipFromRight
                    animations:^{
                         [subViewImage removeFromSuperview];
                        [_panel addSubview:subViewText];
                    }
                      completion: nil];
//This is how I did it before I added the container
    /*[UIView transitionFromView:subViewImage
                        toView:subViewText
                      duration:0.2
                       options:UIViewAnimationOptionTransitionFlipFromRight
                    completion:nil];
    self.subViewText.delegate = self;*/
    NSLog(@"Ipushedtheimage");
}
-(void)exerciseSubViewTextPressed
{//I haven't updated this yet
   [UIView transitionFromView:subViewText
                        toView:subViewImage
                      duration:0.2
                       options:UIViewAnimationOptionTransitionFlipFromRight
                    completion:nil];
    self.subViewImage.delegate = self;



- (void)didReceiveMemoryWarning
{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

    subViewImage = nil;

    subViewText = nil;

}

@end

これはデリゲート #import のコードです

@protocol ExerciseSubViewDelegate <NSObject>

-(void) exerciseSubViewImagePressed;
-(void) exerciseSubViewTextPressed;

@end 最初のサブビューのコードも追加されています: #import #import "ExerciseSubViewDelegate.h"

@interface ExerciseSubViewImage : UIView

@property (nonatomic, strong) UIButton *button;
@property (nonatomic, assign) id<ExerciseSubViewDelegate>delegate;
@property (strong, nonatomic) UIImageView *exerciseImageView;


@end


#import "ExerciseSubViewImage.h"
#import "UIImage+animatedGIF.h"

@implementation ExerciseSubViewImage

@synthesize button;
@synthesize delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //Initialization code
        self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        CGRect buttonFrame = CGRectMake(50,200,100,35);
        self.button.frame = buttonFrame;
        [self.button setTitle:@"Image"forState:UIControlStateNormal];
        [self.button addTarget:self
                    action:@selector(buttonTouched)
                    forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.button];

        _exerciseImageView = [[UIImageView alloc] initWithFrame:CGRectMake(50,20,160,158)]; 

        NSURL *url = [[NSBundle mainBundle] URLForResource:@"AppleLogo" withExtension:@"gif"];
        _exerciseImageView.image = [UIImage animatedImageWithAnimatedGIFURL:url];
                             [self addSubview:self.exerciseImageView];


    }
    return self;
}
-(void)buttonTouched
{
    NSLog(@"imagebuttonpressed");
    [self.delegate exerciseSubViewImagePressed];
}

繰り返しますが、助けていただければ幸いです。私はおそらく単純なことを理解していないことを知っています。

4

1 に答える 1

1

Ok. This took me all weekend but I finally figured it out on my own. I thought I would shere the answer here in case anyone else ever has a similar problem. After trying several other approaches I finally went back to the approach I used here and started inserting a whole bunch of NSLogs to determine the order that every thing was executing in. What I finally ended up doing was changing this: (all in the top ViewController)

  self.subViewImage.delegate = self;

    _panel = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.view.bounds.size.width,    self.view.bounds.size.height/2)];
    _panel.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_panel];
   [_panel addSubview:subViewImage];

to this:

    //create panel
    _panel = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.view.bounds.size.width, s   self.view.bounds.size.height/2)];
    _panel.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_panel];
    [_panel addSubview:subViewImage];

    //Set the subview delegates
    self.subViewImage.delegate = self;
    self.subViewText.delegate = self;   
于 2013-05-06T18:19:17.840 に答える