简体   繁体   English

使用UITabBarController时从按钮强制更改视图

[英]Force a view change from a button when using UITabBarController

When using a UITabBarController, when the user enters some data on View1 and presses a button, I need to perform some calculations and present the results on View2. 当使用UITabBarController时,当用户在View1上输入一些数据并按下按钮时,我需要执行一些计算并将结果显示在View2上。

I have an AppDelegate, View1Controller, View2Controller, and View3Controller (View3 is a basically static view). 我有一个AppDelegate,View1Controller,View2Controller和View3Controller(View3基本是静态视图)。 My AppDelgate declares UITabBarController *rootController; 我的AppDelgate声明了UITabBarController * rootController;

On View1, I have the calculations being performed in an IBAction for buttonPressed; 在View1上,我在IBAction中为buttonPressed执行了计算; however, I can't seem to force the view to switch to View2 programmatically. 但是,我似乎无法强迫视图以编程方式切换到View2。

I have done a lot of searching for similar problems, and think I should be doing something like "self.rootController.selectedIndex = 1"; 我已经做了很多寻找类似问题的工作,并且认为我应该做类似“ self.rootController.selectedIndex = 1”的事情; however,when I do this from within buttonPressed on my View1Controller, I get an error "request for member rootController in something not in a structure or union". 但是,当我从View1Controller上的buttonPressed内部执行此操作时,出现错误“请求成员rootController不在结构或联合中的某些内容”。

I think I'm missing something basic here... probably need do do something with my AppDelegate, but I'm banging my head against the wall. 我想我在这里缺少一些基本的东西……可能需要用AppDelegate做一些事情,但我的头撞墙了。

Can anyone provide some guidance in this situation...like key things I should do in View1Controller header and implementation with reference to my AppDelgate? 任何人都可以在这种情况下提供一些指导...例如我应该在View1Controller标头中执行的关键操作以及参照我的AppDelgate的实现? Thank you! 谢谢!

For anyone searching how to switch from one tab to another, there is an answer provided here . 对于任何搜索如何切换从一个标签到另一个,提供了一种答案在这里

This sounds like a strange UI - but lets accept that you are trying to do the right thing. 这听起来像一个奇怪的UI,但请接受您正在尝试做正确的事情。

iPhone development is about Model View Controller - MVC. iPhone开发与Model View Controller-MVC有关。 So in principle what you want is for view1 to update your Model, and View2 to perform calculations based on your model. 因此,原则上您想要的是view1更新模型,View2根据模型执行计算。

If you are using a tabbar controller, then you probably want to know when you have switched tabs inorder to do these things. 如果您使用的是标签栏控制器,那么您可能想知道何时切换标签页才能执行这些操作。 In view1, you might want to write your code to update your model in viewWillDisappear - this will be run just as the tab is replaced with the next one. 在view1中,您可能需要编写代码以更新viewWillDisappear中的模型-这将在选项卡被下一个替换时运行。 The complimentary function (you might want to use this in view2) is viewWillAppear - this could be used to update view2 with the data from your model. 补充功能(您可能希望在view2中使用它)是viewWillAppear-可用于用模型中的数据更新view2。

Now you are left with figuring out where to put your model. 现在,剩下的工作就是弄清楚放置模型的位置。 You might want to create a class with @parameters based on the data you need to store in your model. 您可能需要根据需要存储在模型中的数据使用@parameters创建一个类。 You could then instantiate your model in the AppDelegate, and pass the model to both of the views as a parameter. 然后,您可以在AppDelegate中实例化模型,并将模型作为参数传递给两个视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UITabBarController切换视图时强制旋转 - UITabBarController force rotate when switching view 更改UITabBarController的moreNavigationController中的“编辑”按钮颜色? - Change 'edit' Button Color from moreNavigationController of UITabBarController? 使用 uitabbarcontroller 动画视图更改 - animating view change with uitabbarcontroller 如何强制 UITabBarController 内部视图的水平方向? - How to force horizontal orientation for a view inside of a UITabBarController? 使用UITabBarController时覆盖整个屏幕的视图? - Overlay a view over whole screen, when using UITabBarController? 关于UITabBarController,是否有一种方法可以在选择新选项卡时强制将视图完全释放 - Regarding the UITabBarController, is there a way to force a view to be fully released when a new tab is selected 如何使用Objective-C从UITabbarcontroller视图中删除子视图 - How to remove subviews from UITabbarcontroller view using objective-c 如何更改UITabBarController更多按钮的标题? - How to change UITabBarController More button's title? 使用 UITabBarController 时出现 NSInvalidArgumentException 错误 - NSInvalidArgumentException Error When Using UITabBarController 更改所选索引时重新加载UITabbarController - Reload UITabbarController when change selected index
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM