【问题标题】:How to get back from subview to previous view如何从子视图返回到上一个视图
【发布时间】:2011-07-18 17:32:14
【问题描述】:

我正在开发一个由不同模块组成的应用程序。 通过起始页面,可以导航到这些不同的模块。

其中一个是一种图像选择器,一种将多个图像缩略图显示为按钮的视图。

单击此缩略图后,整个图像(也是一个按钮)显示在子视图上。

要返回缩略图视图,只需再次单击图像(按钮)即可。

问题是,我总是被引导回到起始页面,在那里我可以在不同的模块之间进行选择。

在这里你可以在我的 ViewController 中找到实现:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
int counter;
int x = 30;
int y = 10;

for (counter = 1; counter < 9 ; counter++) {

UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

CGRect rect = CGRectMake(x, y, 307, 230);
imageButton.frame = rect;

[imageButton addTarget:self action:@selector(showImage:) forControlEvents:UIControlEventTouchUpInside];
[imageButton setImage:[UIImage imageNamed:[NSString stringWithFormat:@"tn%i.jpg",counter]] forState:UIControlStateNormal];
    imageButton.tag = counter;
[self.view addSubview:imageButton];
    x = x + 327;
    if (counter == 3){
        x = 30;
        y = 260;
    }
    if (counter ==6){
        x = 30;
        y = 510;
    }
}
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

-(void)showImage:(id)sender
{

int imageCounter = [sender tag];
UIButton *imageLarge = [UIButton buttonWithType:UIButtonTypeRoundedRect];

CGRect largeRect = CGRectMake(0,0, 1024, 768);
imageLarge.frame = largeRect;

[imageLarge addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
[imageLarge setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%i.jpg",imageCounter]] forState:UIControlStateNormal];
imageLarge.tag = imageCounter;
[self.view addSubview:imageLarge];

}

-(void)back:(id)sender
{
[UIView beginAnimations:@"flipview" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view.superview cache:YES];

[self.view setHidden:YES];

[UIView commitAnimations];  

}

我在这里错过了什么?

【问题讨论】:

  • 您能否编辑您的标签以添加您使用的语言?此外,您的特定技术可能会提供更多相关标签; “按钮”不是描述您在这里所做的事情的最佳标签。谢谢。

标签: objective-c ios4 uiimage addsubview


【解决方案1】:

-back:,似乎在您想要关闭显示的图像时调用,将self.view 设置为隐藏。从您的代码中,self.view 是您正在显示的图像的 superview。所以你隐藏了整个视图,只留下前一个菜单视图可见。

所以你想要做的是将 imageView 存储到实例变量或属性中,然后执行:

imageView.alpha = 0.0; //Alpha animates, hidden doesn't

然后确保在适当的时候从其父视图中删除 imageView。

【讨论】:

    猜你喜欢
    • 2017-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    • 1970-01-01
    • 2011-07-16
    • 2015-01-15
    相关资源
    最近更新 更多