viewController出现时显示为黑色

发布于 2024-12-14 01:31:12 字数 887 浏览 0 评论 0原文

大家好,我是 iOS 开发新手,目前遇到了一些问题,我正在尝试在用户未登录的情况下以模态方式显示登录表单。

我正在使用 NSUserDefaults 来存储我的 id只是检查它是否存在。如果没有,那么我希望 UIViewController 出现。

到目前为止,我的 UINavigationController 中有以下代码,它位于 UITabViewController 内,我试图让 UIViewController 出现在第一个 UINavigationController (选定的那个)上方,此时动画正在发生,但只有一个即使登录屏幕已创建所有相关文本框等,也会出现黑屏。如果我将此登录屏幕设置为要加载的初始视图,它将正常加载。

这是出现在 UINavigationController 的第一个视图控制器中的代码。

 NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if (![prefs stringForKey:@"id"]){
    LoginViewController *loginController = [[LoginViewController alloc] init];
    [self presentModalViewController:loginController animated:YES];
} else {
    [self loadCoffeeUserOrders:[prefs stringForKey:@"id"]];  
}

模式视图正在加载,但目前它只是显示为黑色,我尝试创建一个新的登录屏幕,但发生了同样的事情:除了黑屏之外什么都没有。

希望那里有足够的信息让人们了解可能发生的情况,我对 iOS 开发非常陌生,所以如果我犯了一些错误,那么很高兴知道我哪里出错了。

谢谢

Hi All I'm new to iOS development and am currently having an issue with something, I'm trying to make a login form appear modally if a user is not logged in.

I'm using NSUserDefaults to store an id which I'm simply checking if it exists. If it doesnt then I would want the UIViewController to appear.

So far I have the following code in my UINavigationController which is within a UITabViewController , I'm trying to get the UIViewController to appear above the First UINavigationController (the one that is selected), at the moment the animation is happening but there is only a black screen even though the Login screen has all the relevant text boxes etc already created. If I set this Login screen as the initial view to load, it loads up fine.

This is the code that appears in the First viewcontroller of the UINavigationController.

 NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if (![prefs stringForKey:@"id"]){
    LoginViewController *loginController = [[LoginViewController alloc] init];
    [self presentModalViewController:loginController animated:YES];
} else {
    [self loadCoffeeUserOrders:[prefs stringForKey:@"id"]];  
}

the modal view is loading up but at the moment it just appears black, I've tried creating a new Login Screen and the same thing happens: nothing but a black screen.

hope there is enough information there for someone to understand what might be going on, I am very new to iOS development so if I've made some mistakes it would be nice to know where I'm going wrong.

thanks

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

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

发布评论

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

评论(3

酒与心事 2024-12-21 01:31:12

因此,您在此处创建的 LoginViewController 是与您在故事板中设置的实例不同的实例。我认为您想要导致故事板中的内容被加载和呈现。

最简单的方法是在情节提要中创建从第一个视图控制器到您在其中创建的登录视图控制器的 Segue。您可以按住 Ctrl 键并从一个视图控制器拖动到另一个视图控制器,然后选择“Modal”作为 Segue 类型。然后去检查员那里寻找该segue并给它一个标识符。假设您将其称为“segueToLogin”。

然后,要从代码中执行该 Segue,只需执行以下操作:

  if (![prefs stringForKey:@"id"]){

    [self performSegueWithIdentifier:@"segueToLogin" sender:self];

  }

希望有帮助。

编辑

所以为了回答你的问题,想象一下:

你有一个关于应用程序的好主意,所以你从笔记本上取下一张纸,然后勾勒出这个伟大的设计。然后你的同事过来了,你撕下一张新的白纸给他们看。他们不会留下深刻的印象。

同样的事情也发生在这里。您可以在故事板中使用所有视图设置视图控制器。但是当需要显示它时,您拿出一个新的完全空白的视图控制器并将其显示给用户。通过拉动并启动 segue,您最终会加载所需的实际视图控制器实例。

现在,在 segues 中,您可以在故事板中创建这些内容,以允许用户在场景中导航。有时,这些 Segues 直接连接到按钮或其他东西。但在这种情况下,您不想要求用户按下按钮或其他东西来显示登录 vc,因此您自己执行 segue。

希望这是有道理的。

So, the LoginViewController you are creating here is a different instance from the one that you set up in your storyboard. I think what you want to cause the one in the storyboard to be loaded and presented.

The easiest way is to create a segue in the storyboard from your first view controller to the login view controller that you created there. You can ctrl-drag from one view controller to the other and select "Modal" as the type of segue. Then go to the inspector for that segue and give it an identifier. Let's say you call it "segueToLogin".

Then to perform that segue from your code just do something like this:

  if (![prefs stringForKey:@"id"]){

    [self performSegueWithIdentifier:@"segueToLogin" sender:self];

  }

Hope that helps.

EDIT

So just to answer your question, imagine this:

You have this great idea for an app, so you take a sheet of paper off your pad and you sketch out this great design. Then your colleagues come by so you rip off a new blank sheet of paper and show them that instead. They won't be impressed.

Same thing happened here. You set up a view controller in your storyboard with all your views. But then when it came time to show it, you pulled out a new totally blank view controller and showed that to the user instead. By pulling and actuating the segue instead, you end up loading the actual view controller instance you wanted.

Now on segues, you create those in the storyboard to allow the user to navigate through the scenes. Sometimes those segues are directly attached to a button or something. But in this case you don't want to ask the user to press a button or something to present the login vc, so you are performing the segue yourself.

Hope that makes sense.

瀟灑尐姊 2024-12-21 01:31:12

我遇到了类似的问题,但解决方案不是上面列出的。问题在于实际的新视图控制器“.m”文件(我正在尝试推送/模态的文件)。里面有一个名为 loadView 的方法,如果你把它留在那里,XCode 将忽略故事板中的 xib 并期望你布置视图。删除该函数,它将改为查看 xib。

在这个愚蠢的错误上花了太长时间,希望这可以帮助挽救别人的理智。

I had a similar issue but the resolution was not the one listed above. The problem resided in the actual new view controller ".m" file (the one I'm trying to push/modal on). There is a method called loadView inside and if you leave it there XCode will ignore the xib you have in the storyboard and expect you to lay out the view. Remove that function and it will look at the xib instead.

Spent too long on this stupid bug, hope this can help save someone else's sanity.

星星的軌跡 2024-12-21 01:31:12
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
LoginViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:vc animated:YES];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
LoginViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:vc animated:YES];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文