在启动时通过 PresentModalViewController 显示一次性 UIViewController
问候!我有一个正在运行的 iPhone 应用程序(huzzah!),它使用 MainView.xib
,其中包含一个完整的 UITabBar 以及多个 UINavigationController 对象和视图,位于-准备好了。
现在,我被要求向该组合添加一次性注册视图。此视图将在应用程序启动时出现在 UITabBar 之前,从用户处获取一些信息,向服务器注册 - 或检查现有注册,然后保存一些数据在钥匙串中。 (如果钥匙串已经显示了注册证明,那么我们将跳过显示此特定视图。)
我已经控制了注册和钥匙串部分(感谢 Erica Sadun 的后者!),但显示事实证明,最初的一次性观点比我预期的要棘手。
我怀疑我太接近问题而看不出问题所在。我真的希望这是飞行员的失误,不需要任何东西,鲁布·戈德堡!
场景如下:
应用程序首先加载 MainView.xib,其中包含前面提到的 UITabBar 控制器等。等人。为了便于讨论,假设我们必须显示该注册视图。另外,我们希望它具有模态外观,因此它会从下向上飞入。然后,当我们完成后,我们可以关闭它,调用委托(最有可能是应用程序委托)并告诉它继续执行原始的 UITabBar。
// Normally, the Tab Bar Controller's view is added to the window ... still do this?
[window addSubview:tabBarController.view];
// We could now set up a VC like so. Mostly harmless. (I know, "mvc" is an unfortunate abbreviation in this case.)
RegistrationVC *mvc = [[RegistrationVC alloc] initWithNibName:@"RegistrationView" bundle:nil];
请注意,RegistrationView.xib
内部有一个 UIView,但没有导航控制器。我们希望保持它的解耦性,以便它可以被重用,例如,作为选项卡栏项目的导航控制器的一部分(例如,检查您的注册信息)。
继续前进。我们创建一个导航控制器,旨在以模态方式呈现事物:
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:mvc];
然后,我们使用选项卡栏控制器作为基础来呈现我们的模态 VC,并释放分配的位。
[tabBarController presentModalViewController:nc animated:YES];
[nc release];
[mvc release];
第一次观察。有些东西告诉我这实在是太草率了。您可以看到第一个选项卡栏项目的导航栏和视图在模态视图突然出现时出现。是啊!此外,尝试预先将选定的 VC 设置为 nil 没有任何效果:
tabBarController.selectedViewController = nil;
我们真的不想/不需要使用选项卡栏,直到模态 VC 完成之后(并且我们有委托来帮助当发生这种情况时请告诉我们)。
为什么我还要为标签栏烦恼?好吧,看来我需要一些东西来为模态 VC 戴上帽子,而且我不知道还有什么可以使用。
这是唯一的方法吗?它似乎不必要地束缚了注册 VC 和选项卡栏,而且它只是闻起来......错误。
欢迎提供线索/赞赏!
Greetings! I have a working iPhone app (huzzah!) that uses a MainView.xib
containing a fully-stocked UITabBar with several UINavigationController objects and views at-the-ready.
I've now been asked to add a one-time registration view to this mix. This view would appear before the UITabBar at app-launch, get some info from the user, register with a server - or check for an existing registration, then squirrel some data away in the keychain. (If the keychain already shows proof of registration, then we skip showing this particular view.)
The registration and keychain part I've got under control (thank you Erica Sadun for the latter!), but showing that initial one-time view is proving to be trickier than I expected.
I suspect I'm too close to the problem to see what's wrong. I really hope it's pilot error and doesn't require anything too Rube Goldberg!
Here's the scenario:
The app starts by loading MainView.xib, in which lies the aforementioned UITabBar controller, et. al. For the sake of argument, let's say we must show that registration view. Also, we'd like it to have a modal appearance, so it will fly in from the bottom up. Then, when we're done, we can dismiss it, call a delegate (most likely the App Delegate) and tell it to carry on with the original UITabBar.
// Normally, the Tab Bar Controller's view is added to the window ... still do this?
[window addSubview:tabBarController.view];
// We could now set up a VC like so. Mostly harmless. (I know, "mvc" is an unfortunate abbreviation in this case.)
RegistrationVC *mvc = [[RegistrationVC alloc] initWithNibName:@"RegistrationView" bundle:nil];
Note that RegistrationView.xib
has a UIView inside, but no nav controller. We want to keep it decoupled so that it can be reused, say, as part of a tab bar item's nav controller (to review your registration info, for instance).
Moving on. We create a nav controller with the intent of presenting things modally:
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:mvc];
We then present our modal VC, using the tab bar controller as the basis, and release the alloc'ed bits.
[tabBarController presentModalViewController:nc animated:YES];
[nc release];
[mvc release];
First observation. Something tells me this is just plain sloppy. You can see the first tab bar item's nav bar and view appear just as the modal view swoops in. Yeccch! Moreover, trying to set the selected VC to nil beforehand has no effect:
tabBarController.selectedViewController = nil;
We really don't want/need to use the tab bar until after the modal VC is done (and we have the delegate to help let us know when that happens).
Why am I even bothering with the Tab Bar? Well, it looks like I need something to hang that modal VC's hat on, and I don't know what else there is to use.
Is this the only way? It just seems to tether the Registration VC and the Tab Bar unnecessarily, and it just smells ... wrong.
Clues welcome/appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果不知道 Default.png 显示的内容,就很难回答这个问题。假设您遵循 HIG 并且它显示一个空的 tabBarController,我建议使用稍微复杂的分层:
底视图:tabBarController.view
中间视图:UIImageView:Default.png
顶视图:位于屏幕底部下方的注册视图
启动时,如果需要显示注册视图,请手动将其向上动画化,动画完成后删除其下方的 UIImageView。注册完成后,手动向下设置注册视图的动画以显示 tabBarController。如果启动时不需要注册视图,只需将 UIImageView 设置为淡出动画(或将其删除)即可。
OTOH 手,如果你不遵循 HIG 而是显示某种启动屏幕,事情就会变得容易一些。像这样分层:
底视图:tabBarController.view
顶视图:UIImageView:Default.png
如果您需要显示注册,请使用animated:NO 呈现ModalViewController,然后淡出UIImageView。如果没有,则淡出 UIImageView。
这是一个很长的解释,没有图片,希望它有意义。重点是,我建议添加 UIImageView:Default.png 作为应用程序启动时看到的第一个内容,并使用它来指导您适当地过渡到注册或 tabBarController。
It's hard to answer this without knowing what your Default.png shows. Assuming you're following the HIG and it displays an empty tabBarController, I'd suggest a somewhat complicated layering:
bottom view: tabBarController.view
middle view: UIImageView: Default.png
top view: registration view positioned below the bottom of the screen
On startup, if you need to show the registration view, manually animate it upward, and once the animation is done remove the UIImageView below it. When registration is complete, manually animate the registration view downward to reveal the tabBarController. If on startup you don't need the registration view, just animate the UIImageView to fade away (or just remove it).
OTOH hand, if you're not following the HIG and instead showing some kind of splash screen, things get a bit easier. Layer like this:
bottom view: tabBarController.view
top view: UIImageView: Default.png
If you need to show registration, do presentModalViewController with animated:NO and then fade out the UIImageView. If not, just fade out the UIImageView.
That's a lengthy explanation w/o pictures, hope it makes sense. The salient point is that I'm suggesting adding a UIImageView:Default.png to be the first thing that is seen when the app starts, and use that to guide your transition into registration or tabBarController as appropriate.