如何在加速时从视图控制器外包视图更改操作?

发布于 2024-07-18 10:31:58 字数 682 浏览 7 评论 0原文

我有一个视图控制器类。 当加速度计检测到运动并调用委托对象中的 accelerometer:didAccelerate: 方法时,我想在我看来做一些事情。

该委托对象是我大脑中的问题。 目前,我的大脑已经冻结,我不知道什么会更好。 让我知道你的想法!

解决方案1) 在我的视图控制器类中,我遵循 UIAccelerometerDelegate 协议,并实现该 accelerometer:didAccelerate: 方法。 在 AppDelegate 类的 -applicationDidFinishLaunching: 方法中,我将该视图控制器对象设置为用于接收加速时的方法调用的委托。 我认为这不太好。

解决方案2) 我创建一个名为 AccelerationDelegate 的空白新对象,符合 UIAccelerometerDelegate 协议,实现该 accelerometer:didAccelerate: 方法,并在 AppDelegate 类的 -applicationDidFinishLaunching: 方法中将该视图控制器对象设置为用于接收加速时的方法调用的委托。

但对于解决方案 2,我的大脑有点卡住了! 我如何从该对象内的视图控制器访问视图对象?

这里的问题是,我周围有多个视图控制器。 我使用标签栏控制器在它们之间切换。

有什么建议我可以如何做到这一点吗?

I have an view controller class. I want to do some things in my view when the accelerometer detects movement and calls the accelerometer:didAccelerate: method in the delegate object.

That delegate object is the problem here in my brain. Currently, my brain is freezed and I don't get it what would be better. Let me know what you think!

Solution 1)
In my view controller class I conform to the UIAccelerometerDelegate protocol, and implement that accelerometer:didAccelerate: method.
In the -applicationDidFinishLaunching: method of my AppDelegate class I set that view controller object up as the delegate for receiving method calls upon accelerations. I think that's not really good.

Solution 2)
I create a blank new object called AccelerationDelegate, conform to that UIAccelerometerDelegate protocol, implement that accelerometer:didAccelerate: method and in the -applicationDidFinishLaunching: method of my AppDelegate class I set that view controller object up as the delegate for receiving method calls upon accelerations.

But for solution 2 my brain got stuck a little bit! How would I access the view objects from my view controller inside that object?

The problem here is, that I have more than one view controller around. I use a tab bar controller to switch between them.

Any suggestions how I could get that right?

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

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

发布评论

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

评论(2

末蓝 2024-07-25 10:31:58

我同意第二种方法更好。 您想要仅访问当前选定的选项卡视图,还是仅访问应用程序中的特定视图。

无论如何,我要做的就是在 UIApplicationDelegate 中为您的 UITabViewController 设置属性,以便您可以从委托访问它(您可以通过以下方式获取应用程序委托)调用[[UIApplication共享应用程序]委托])。 例如:

YourApplicationDelegate *appDelegate = (YourApplicationDelegate *)[[UIApplication sharedApplication] delegate];
FirstUIViewController *firstViewController = appDelegate.firstViewController;
[firstViewController doStuff];

其中 firstViewController 是委托的一个属性。

I agree that the second method is better. Are you looking to access just the currently selected tab view, or just a specific view in your app.

In any case, what I would do is to set up properties for your UITabViewController in your UIApplicationDelegate so that you can access it from the delegate (you can get the app delegate by calling [[UIApplication sharedApplication] delegate]). For example:

YourApplicationDelegate *appDelegate = (YourApplicationDelegate *)[[UIApplication sharedApplication] delegate];
FirstUIViewController *firstViewController = appDelegate.firstViewController;
[firstViewController doStuff];

where firstViewController is a property on your delegate.

逆夏时光 2024-07-25 10:31:58

如果您的加速特定于一个视图控制器,那么让视图控制器接收更改其自己的子视图所需的信息是有意义的。 但是,最好在视图出现时将视图控制器设置为委托,并在视图消失时将委托设置为 null。 (具体来说,- (void) viewWillAppear:- (void) viewWillDisappear:

If your acceleration is specific to one view controller, then it makes sense to have the view controller receive the information necessary to alter its own subviews. However, it might be better to set your view controller to be the delegate when the view appears, and set the delegate to null when it disappears. (Specifically, - (void) viewWillAppear: and - (void) viewWillDisappear:)

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