开发者

How to get back from subview to previous view

I am working on an app, which consists of different modules. Through a starting page it is possible to navigate to those different modules.

One of them is a kind of image picker, a view which shows several image thumbnails as buttons.

After clicking on this thumbnail, the whole image, which is also a button is shown on a subview.

To get back to the thumbnail view it should be possible to just click again on the image (button).

The problem is, that I am always directed back to the starting page, where I can choose between the different modules.

Here you find the Implementation in my 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:[NSStr开发者_开发问答ing 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];  

}

What am I missing here?


-back:, which seems to be called when you want to dismiss the displayed image, sets self.view to be hidden. From your code, self.view is the superview of the image you're displaying. So you're hiding the entire view, leaving only the previous menu view visible.

So what you'd want to do instead is store the imageView to an instance variable or property, then do:

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

Then be sure to remove the imageView from its superview whenever appropriate.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜