当调用内部委托时,不显示模态视图

时间:2011-06-08 22:02:37

标签: iphone objective-c ipad modalviewcontroller

我有一个名为MainViewController的UIViewController(它在navigationController中)。我有另一个名为OptionsViewController的UIViewController。在OptionsViewController里面我有一个注销按钮,点击它时会调用MainViewController中的一个委托:

- (IBAction) logout:(id)sender
{
    [self.delegate viewController:self loginSuccess:YES]; //calls this delegate

    NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
    NSString * username = [standardDefaults stringForKey:@"kApplicationUserNameKey"]; 
    NSError * error = nil;

    [standardDefaults removeObjectForKey:@"kApplicationUserNameKey"];
    [SFHFKeychainUtils deleteItemForUsername:username andServiceName:@"convore" error:&error];
    [self dismissModalViewControllerAnimated:YES];
}

在MainViewController上调用的委托是:

- (void) viewController:(OptionsViewController*)viewCon loginSuccess:(BOOL)loadFlag
{
    if (loadFlag){
        LoginViewController* lvc = [[LoginViewController alloc] init];
        lvc.delegate = self;
        [self.navigationController presentModalViewController:lvc animated:YES];
          //this same code works in the viewDidLoad (it presents the LoginViewController, but not here)
        [lvc release];

        [self.groups removeAllObjects];
        [self.table reloadData];

        Topic * topic = [Topic object];
        topic.tid = [NSNumber numberWithInt:-2];
        self.detailViewController.topic = topic;
        self.detailViewController.detailItem = topic.tid;
    }
}

问题在于,当调用此委托时,它应该呈现一个LoginViewController(从上面的代码可以看出),但事实并非如此。我试图将presentModalViewController代码放在MainViewController的viewDidLoad中的委托中,它显示出来,但是当试图在这个委托中显示它时它没有。为什么是这样?是的,我检查了委托被调用(尝试将NSLog放在委托中)

更新

OptionsViewController也显示为modalViewController,以及来自MainViewController的以下代码:

- (IBAction)showOptions:(id)sender
{
    if ([self.detailViewController.message isFirstResponder])
        [self.detailViewController setViewMovedUp:NO];


    OptionsViewController * opt = [[OptionsViewController alloc] init];
    opt.delegate = self;
    opt.mgvc = self;
    UINavigationController * uinc = [[UINavigationController alloc] initWithRootViewController:opt];
    uinc.navigationBar.tintColor = [UIColor blackColor];
    uinc.modalPresentationStyle = UIModalPresentationFormSheet;
    uinc.title = @"";
    uinc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;  //transition shouldn't matter
    [self presentModalViewController:opt animated:YES];

    float xCenter = 384;
    float yCenter = 512;
    if (self.splitViewController.interfaceOrientation == UIInterfaceOrientationPortrait || self.splitViewController.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
        xCenter = 384;
        yCenter = 512;
    } else if (self.splitViewController.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || self.splitViewController.interfaceOrientation == UIInterfaceOrientationLandscapeRight){
        xCenter = 512;
        yCenter = 384;
    }

    uinc.view.superview.frame = CGRectMake(0, 0 , 318, 209);//it's important to do this after presentModalViewController
    uinc.view.superview.center = CGPointMake(xCenter, yCenter);
    [opt release];

}

当我尝试只显示OptionsViewController本身时(没有UINavigationController,一切正常)。这是为什么?

1 个答案:

答案 0 :(得分:0)

您确定您的视图位于导航控制器内吗?如果没有,请将行更改为:

[self presentModalViewController:lvc animated:YES];