在委托内部调用时,presentModalView 未显示

发布于 2024-11-14 07:34:51 字数 3353 浏览 3 评论 0原文

我有一个名为 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,一切正常)。这是为什么呢?

I have a UIViewController called MainViewController (it is inside a navigationController). I have another UIViewController called OptionsViewController. Inside OptionsViewController I have a logout button and when clicked it calls a delegate in the 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];
}

The delegate called at MainViewController is:

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

The issue is that when this delegate is called, it should present a LoginViewController (as can be seen from the code above), however it doesn't. I tried to put the presentModalViewController code in the delegate inside the viewDidLoad of MainViewController and it shows up, but when trying to show it in this delegate it doesn't. Why is this? And yes I checked the delegate is getting called (tried putting a NSLog inside the delegate)

UPDATE:

The OptionsViewController is shown as a modalViewController as well with the following code from 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];

}

When I try to just show OptionsViewController itself (without the UINavigationController, everything works fine). Why is this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的鱼塘能养鲲 2024-11-21 07:34:51

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

[self presentModalViewController:lvc animated:YES];

Are you sure your view is inside a navigation controller? If not, change the line to:

[self presentModalViewController:lvc animated:YES];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文