objective-c - 从 ViewController 登录到 SplitViewController

标签 objective-c ios authentication uisplitviewcontroller segue

我正在编写一个小应用程序来使用网络服务的结果。结果显示在 SplitViewController 中,左侧是结果,右侧是详细信息。在获得结果之前,我要求用户通过登录屏幕登录,该屏幕会在应用程序启动时首先出现。

我在登录成功后通过更改应用程序的 RootViewController 来管理登录过程:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

if([[segue identifier] isEqualToString:@"LoginControllerSeque"] && [self doLogin]){
    TMAppDelegate *appDelegate = (TMAppDelegate *)[[UIApplication sharedApplication]delegate];
    // IPad 
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)  {
        UISplitViewController *cvc = (UISplitViewController *)[segue destinationViewController];
        [appDelegate.window setRootViewController:cvc];  
    } 
    // Other device
    else {
        UINavigationController *cvc = (UINavigationController *)[segue destinationViewController];
        [appDelegate.window setRootViewController:cvc];  
    }  
    [appDelegate switchToMainView];
}
else{
    alertView = [[UIAlertView alloc] initWithTitle:@"access denied" message:@"access denied" delegate:self cancelButtonTitle:@"back" otherButtonTitles:nil];
    [alertView show];
}

我正在使用 Storyboard segue 切换到 SplitViewController,但如果没有进一步的操作,它不会执行任何操作。

在我的 AppDelegate 中有以下部分:

- (void)switchToMainView{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
    UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
    UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
    splitViewController.delegate = (id)navigationController.topViewController;

    UINavigationController *masterNavigationController = [splitViewController.viewControllers objectAtIndex:0];
    TMMasterViewController *controller = (TMMasterViewController *)masterNavigationController.topViewController;
    controller.managedObjectContext = self.managedObjectContext;
} else {
    UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
    TMMasterViewController *controller = (TMMasterViewController *)navigationController.topViewController;
    controller.managedObjectContext = self.managedObjectContext;
}  

[self.window reloadInputViews]; 
[self.window makeKeyAndVisible];}

到目前为止一切正常,但现在我在 SplitView 的 detailViewController 上的 PopoverView 中有一个注销按钮,它位于登录屏幕之后。我认为我可以用同样的方法来做,所以我做了:

- (IBAction)logout:(id)sender {
[self.currentPopover dismissPopoverAnimated:NO];
TMAppDelegate *appDelegate = (TMAppDelegate *)[[UIApplication sharedApplication]delegate];
[appDelegate.window setRootViewController:[appDelegate loginViewController]];  
[appDelegate switchToLoginView];
}

在 AppDelegate 中:

- (void)switchToLoginView
{
[self.window reloadInputViews]; 
[self.window makeKeyAndVisible];
}

现在,如果我第二次尝试登录时出现错误:原因:'-[UIPopoverController presentPopoverFromRect:inView:permittedArrowDirections:animated:]: Popovers cannot be presented from a view which does not have a window .'

我不知道为什么这是第一次工作,而第二次我遇到了这些问题。

任何人都可以帮助我或给我提示吗?也许这是处理登录的错误概念?

更新:

问题恰好出现在这部分:

  // Beim IPad müssen wir uns anders verhalten als beim Phone
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) 
    {
        UISplitViewController *cvc = (UISplitViewController *)[segue destinationViewController];
        [appDelegate.window setRootViewController:cvc];  
    } 

进入线路: [appDelegate.window setRootViewController:cvc];

最佳答案

我使用自定义 segue 实现了相同的功能。这似乎已经奏效并且简单得多。

@implementation LoginSegue
- (void) perform {
    NSLog(@"Do the segue you way");
    UIViewController *src = self.sourceViewController;
    UIWindow *window = src.view.window;
    [window addSubview:[self.destinationViewController view]];
    window.rootViewController = self.destinationViewController;
}
@end

关于objective-c - 从 ViewController 登录到 SplitViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8566234/

相关文章:

objective-c - 设置其他类变量

objective-c - iOS:创建 UINavigationController 的副本

ios - Objective-C instantiateViewControllerWithIdentifier 返回 nil

iphone - 如何在 iPhone 应用程序中集成 Salesforce?

ios - 接收远程通知时打开/关闭振动和声音 ios swift

ios - 如何检查 UIView 是否超出其父 View 范围

android - 使用 ANDROID_ID 作为用户凭证是否安全?

c++ - 哪个 Windows API 用于 Windows 安全代理身份验证对话框?

c# - ASP.NET 身份验证问题

ios - 使用附加参数初始化隐式解包选项?