0

我有一个包含两个子视图的视图。我让它工作,以便一次只显示一个子视图,每个子视图都有一个按钮,当单击按钮时,子视图会翻转并出现下一个子视图。问题是它看起来好像整个视图都在翻转。在本网站上阅读了有关如何解决问题的信息后,我尝试将子视图添加到容器中并翻转它。然而现在,虽然我的第一个子视图在我按下按钮时出现,但它不再翻转。它什么也没做。我在翻转子视图的方法中放置了一个日志语句,以及一个断点,据我所知,它不再被调用。我对 xcode、objective c 和 delegates 很陌生,我不知道如何继续。任何帮助,将不胜感激。谢谢。

我在此处包含了相关代码: 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

好的。这花了我整个周末,但我终于自己弄清楚了。我想我会在这里给出答案,以防其他人遇到类似的问题。在尝试了其他几种方法后,我终于回到了我在这里使用的方法并开始插入一大堆 NSLog 以确定每件事情的执行顺序。我最终做的是改变这个:(都在顶部的 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];

对此:

    //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 回答