Ios 如何使用dismissModalViewControllerAnimated

Ios 如何使用dismissModalViewControllerAnimated,ios,xcode,modalviewcontroller,Ios,Xcode,Modalviewcontroller,当我在应用程序中使用午餐,并在横向旋转iPhone时,我会加载一个不同的视图,即帮助视图。现在,如果我通过主视图中的一个按钮转到另一个视图,然后旋转iPhone,会显示帮助视图……我想知道如何仅在第一个视图中加载帮助视图。这是我的.m代码: @implementation Home @synthesize ruota; @synthesize portraitView,landscapeView; - (id)initWithNibName:(NSString *)nibNameOrNil b

当我在应用程序中使用午餐,并在横向旋转iPhone时,我会加载一个不同的视图,即帮助视图。现在,如果我通过主视图中的一个按钮转到另一个视图,然后旋转iPhone,会显示帮助视图……我想知道如何仅在第一个视图中加载帮助视图。这是我的.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
编辑:


也许您可以通过在您的DirectionChanged:方法中检查第一个视图控制器是否可见,然后仅在该情况下显示模式帮助屏幕来实现这一点:

-(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
        }
    }
}

顺便说一句,我在这里找到了如何检查视图控制器是否可见的提示:

谢谢您的回答。我在上面编辑了我的代码,现在当我从第一个视图转到另一个视图时,我把它放在横向视图中,没有显示任何帮助。好的但当我回到家,把风景画放进去时,最后一次看到的景色就出现了。奇怪的行为
-(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
        }
    }
}