如何使用dismissModalViewControllerAnimated

时间:2012-03-14 19:14:28

标签: ios xcode modalviewcontroller

当我在我的应用程序中休息并且我在横向上旋转iPhone时,我加载了一个不同的视图(帮助视图)。现在,如果我通过主视图中的一个按钮转到另一个视图并且我旋转iPhone,则会显示帮助视图...我想知道如何在我加载帮助视图时m在第一个视图中。这是我的.m代码:

@implementation Home
@synthesize ruota;
@synthesize portraitView,landscapeView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

- (void)viewDidLoad
{
[super viewDidLoad];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(orientationChanged:)name:@"UIDeviceOrientationDidChangeNotification" 
object:nil];
[self performSelector:@selector (ritardo) withObject:nil afterDelay:5.0f];
}


-(void)orientationChanged:(NSNotification *)object{
UIDeviceOrientation deviceOrientation = [[object object] orientation];
if (deviceOrientation == UIInterfaceOrientationPortrait) 
{
    [UIView beginAnimations:@"View Flip" context:nil];
    [UIView setAnimationDuration:0.5f];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    self.view = self.portraitView;
    [UIView commitAnimations];
    [self dismissModalViewControllerAnimated:YES];
} else  if (deviceOrientation == UIInterfaceOrientationLandscapeLeft || 
deviceOrientation == UIInterfaceOrientationLandscapeRight) {
    [UIView beginAnimations:@"View Flip" context:nil];
    [UIView setAnimationDuration:0.5f];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    self.view = self.landscapeView;
    [UIView commitAnimations];
    [self dismissModalViewControllerAnimated:YES];
}
}

-(void) ritardo {
ruota.image = [UIImage imageNamed:@"RuotaPerAiuto.png"];    
}

- (void)viewDidUnload
{
[self setPortraitView:nil]; 
[self setLandscapeView:nil];
[self setRuota:nil];
[super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
    return YES;
}
}
@end

编辑:

-(void)orientationChanged:(NSNotification *)object{
UIDeviceOrientation deviceOrientation = [[object object] orientation];
if (deviceOrientation == UIInterfaceOrientationPortrait)
 { 
    if (self.isViewLoaded && self.view.window)
     {
    [UIView beginAnimations:@"View Flip" context:nil];
    [UIView setAnimationDuration:0.5f];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    self.view = self.portraitView;
    [UIView commitAnimations];
    [self dismissModalViewControllerAnimated:YES];
     }    
} 

else  if (deviceOrientation == UIInterfaceOrientationLandscapeLeft || deviceOrientation 
== UIInterfaceOrientationLandscapeRight) 
{   
    if (self.isViewLoaded && self.view.window)
    {   
     [UIView beginAnimations:@"View Flip" context:nil];
     [UIView setAnimationDuration:0.5f];
     [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
     self.view = self.landscapeView;
     [UIView commitAnimations];
     [self dismissModalViewControllerAnimated:YES];
    }
}
}

1 个答案:

答案 0 :(得分:0)

也许您可以通过在orientationChanged:方法中检查您的第一个视图控制器是否可见,然后仅在显示您的模态帮助屏幕时完成此操作:

-(void)orientationChanged:(NSNotification *)object
{
    UIDeviceOrientation deviceOrientation = [[object object] orientation];
    if (deviceOrientation == UIInterfaceOrientationPortrait) 
    {
        // check if this view controller is visible
        if (self.isViewLoaded && self.view.window) {

            // then show modal help view controller
        }
    }
}

BTW我找到了如何检查视图控制器是否在此处可见的提示:How to tell if UIViewController's view is visible