UIPageControl 加载新视图或不同控制器

发布于 2024-12-20 04:18:17 字数 347 浏览 2 评论 0原文

我刚刚“尝试”浏览苹果的 PageControl 教程。现在我应该指出,我并没有完全理解这一点,它看起来很复杂,所以如果这个问题非常明显,我深表歉意。

我注意到苹果从 .plist 加载了其内容。现在,如果您只有一个 UILabel 和一个 UIImageView ,那么一切都很好很容易,但如果我想做一些更复杂的事情怎么办?如果我希望每个“页面”都有 14 个不同的变量,每个“页面”上有一个按钮,根据您所在的页面执行其他操作...

所以我的问题是这样的(也许这样做并不明智)首先): 有没有办法对其进行编码,以便当用户切换页面时,它会加载一个不同的控制器,该控制器恰好有自己的 .Xib 文件和已在界面生成器中创建的视图?

谢谢

I have just "attempted" to go over apples tutorial of PageControl. Now I should point out that I did not fully understand this, it seemed complex so I apologize if this question is very obvious.

I noticed that apple loaded its content from a .plist. Now thats all nice and easy if all you have is one UILabel and an UIImageView but what if I wana do something more complex? What if I want to each "page" to have like 14 different variables, a button on each "page" that does something else depending on what page you are...

So my question is this (perhaps this would not be smart to do in the first place):
Is there someway to code it so when the page is switched by the user, it loads a different controller that happens to have its own .Xib file and view already created in interface builder?

Thank you

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

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

发布评论

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

评论(1

天涯沦落人 2024-12-27 04:18:17

是的,有。您将使用UIPageViewControllerUIPageViewController 具有数据源和委托方法,这些方法根据用户向左还是向右滑动而调用。它基本上是说“嘿,给我一个 UIViewController,我应该在这个 UIViewController 之前或之后显示它。”

这是一个示例:

MyPageViewController.h

@interface MyPageViewController : UIPageViewController <UIPageViewControllerDataSource, UIPageViewControllerDelegate>

@end

MyPageViewController.m

#import "MyPageViewController.h"

@implementation MyPageViewController 

- (id)init
{
    self = [self initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll
                   navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
                                 options:nil];

    if (self) {
        self.dataSource = self;
        self.delegate = self;
        self.title = @"Some title";

        // set the initial view controller
        [self setViewControllers:@[[[SomeViewController alloc] init]]
                       direction:UIPageViewControllerNavigationDirectionForward
                        animated:NO
                      completion:NULL];
    }

    return self;
}

#pragma mark - UIPageViewController DataSource methods
- (UIViewController *)pageViewController:(UIPageViewController *)pvc
      viewControllerBeforeViewController:(UIViewController *)vc
{
    // here you put some logic to determine which view controller to return.
    // You either init the view controller here or return one that you are holding on to
    // in a variable or array or something.
    // When you are "at the end", return nil

    return nil;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pvc
       viewControllerAfterViewController:(UIViewController *)vc
{
    // here you put some logic to determine which view controller to return.
    // You either init the view controller here or return one that you are holding on to
    // in a variable or array or something.
    // When you are "at the end", return nil

    return nil;
}

@end

就是这样!

Yes there is. You would use a UIPageViewController. The UIPageViewController has data source and delegate methods that get called depending on whether the user swipes to the left or to the right. It basically says "Hey, give me the UIViewController that I should display before or after this UIViewController."

Here's a sample:

MyPageViewController.h:

@interface MyPageViewController : UIPageViewController <UIPageViewControllerDataSource, UIPageViewControllerDelegate>

@end

MyPageViewController.m:

#import "MyPageViewController.h"

@implementation MyPageViewController 

- (id)init
{
    self = [self initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll
                   navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
                                 options:nil];

    if (self) {
        self.dataSource = self;
        self.delegate = self;
        self.title = @"Some title";

        // set the initial view controller
        [self setViewControllers:@[[[SomeViewController alloc] init]]
                       direction:UIPageViewControllerNavigationDirectionForward
                        animated:NO
                      completion:NULL];
    }

    return self;
}

#pragma mark - UIPageViewController DataSource methods
- (UIViewController *)pageViewController:(UIPageViewController *)pvc
      viewControllerBeforeViewController:(UIViewController *)vc
{
    // here you put some logic to determine which view controller to return.
    // You either init the view controller here or return one that you are holding on to
    // in a variable or array or something.
    // When you are "at the end", return nil

    return nil;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pvc
       viewControllerAfterViewController:(UIViewController *)vc
{
    // here you put some logic to determine which view controller to return.
    // You either init the view controller here or return one that you are holding on to
    // in a variable or array or something.
    // When you are "at the end", return nil

    return nil;
}

@end

That's it!

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