如何为 IOS iPhone 应用程序循环多个视图控制器?

发布于 2025-01-08 04:51:11 字数 581 浏览 2 评论 0原文

我有一个带有主视图控制器的应用程序,它加载“第一页”视图控制器。一旦用户单击了几个按钮,我就以这种方式将“第一页:视图控制器”交换为第二页:

        [UIView transitionFromView:self.view toView:self.secondPageVC.view duration:1.0 options:UIViewAnimationOptionTransitionFlipFromRight completion:^(BOOL done ) {
            [self.view removeFromSuperview];
        }];

为了做到这一点,我包含了“下一页”的头文件,它将加载并创建一个实例当前视图控制器类中的下一页。

所有这些工作正常,直到我到达最后一页。但是,当我尝试包含该头文件时。在最后一页的第一页中,我得到了错误,我认为是。发生的情况是,每个视图控制器类在其定义中都有一个下一页的实例,当我们到达最后一页时,添加第一页的实例会导致编译器不喜欢的循环,有人可以向我解释一下我的情况吗?做错了吗?如果我不应该使用这样的视图控制器,我应该如何设置一个有 10 个页面可以循环回到第一页的游戏?

I have a app with a main viewcontroller that loads a "first page" viewcontroller. once the user has clicked a few buttons, I exchange the "first page: view controller for the second this way:

        [UIView transitionFromView:self.view toView:self.secondPageVC.view duration:1.0 options:UIViewAnimationOptionTransitionFlipFromRight completion:^(BOOL done ) {
            [self.view removeFromSuperview];
        }];

In order to do this I include the header file of the "next page" that is going to load and create a instance of that next page in the class of the current View controller.

All of this works fine until i get to the last page. One of the button combinations sends the user back to the first page. However when I try to include the header file of the first page in the last page, I get errors. What I think is happening is that each view controller class has an instance of the next page within its definition and when we get to the last page, adding an instance of the first page causes a loop that the compiler does not like. Can someone explain to me what I am doing wrong? If I am not supposed to use view controllers like this, how should I set up a game that has 10 pages that can loop back to the first page?

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

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

发布评论

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

评论(2

又怨 2025-01-15 04:51:11
  1. 包含在你的 .m 而不是你的 .h 中将解决你的问题
  2. 看看 UINavigationController
  1. Including in your .m instead of in your .h will solve your problem
  2. Look at UINavigationController
蓝色星空 2025-01-15 04:51:11

我认为正在发生的是每个视图控制器类都有一个
其定义中的下一页的实例,当我们到达
最后一页,添加第一页的实例会导致循环
编译器不喜欢。

我不认为是这样。在 Objective-C 中,对象不能被其他对象包含,只能被其他对象引用。也就是说,你永远不会在变量中存储整个对象,你只在变量中存储指向对象的指针,因此一个对象不能包含另一个对象,就像 C++ 中的对象可以包含另一个对象一样。包含另一个对象。

我不太确定您的代码发生了什么,因为您没有向我们展示代码或错误,但以下是如何让多个视图控制器相互引用:

@interface MyViewController : UIViewController

@property MyViewController *next;

@end


// ...somewhere in your code...

MyViewController *vc1 = [[MyViewController alloc] initWithNibName:... bundle:...];
MyViewController *vc2 = [[MyViewController alloc] initWithNibName:... bundle:...];
MyViewController *vc3 = [[MyViewController alloc] initWithNibName:... bundle:...];

vc1.next = vc2;
vc2.next = vc3;
vc3.next = vc1;

现在,我正在使用三个视图控制器这里使用完全相同的类型只是为了保持代码简短,但您不必这样做。它们甚至不一定都必须对 next 属性使用相同的名称。如果可以的话,我鼓励您避免视图控制器之间的依赖关系 - 您的每个视图控制器类真的需要知道下一个是什么类型吗?如果不这样做,您的代码将更易于管理,以便您可以根据需要轻松更改控制器的顺序。

What I think is happening is that each view controller class has an
instance of the next page within its definition and when we get to the
last page, adding an instance of the first page causes a loop that the
compiler does not like.

I don't think that's it. In Objective-C, objects can't be contained in other objects, only referred to by other objects. That is, you never store an entire object in a variable, you only store a pointer to an object in a variable, so an object can't contain another object in the sense that an object in C++ can contain another object.

I'm not exactly sure what's going on with your code since you haven't shown us either the code or the errors, but here's how you can have several view controllers refer to each other:

@interface MyViewController : UIViewController

@property MyViewController *next;

@end


// ...somewhere in your code...

MyViewController *vc1 = [[MyViewController alloc] initWithNibName:... bundle:...];
MyViewController *vc2 = [[MyViewController alloc] initWithNibName:... bundle:...];
MyViewController *vc3 = [[MyViewController alloc] initWithNibName:... bundle:...];

vc1.next = vc2;
vc2.next = vc3;
vc3.next = vc1;

Now, I'm using three view controllers of exactly the same type here just to keep the code short, but you don't have to do that. They don't even necessarily all have to use the same name for the next property. I would encourage you to avoid dependencies between view controllers if you can -- does each of your view controller classes really need to know what type the next one is? Your code will be easier to manage if they don't, so that you can easily change the order of the controllers if you want to.

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