UIPageControl 中的问题

发布于 2024-11-06 12:38:20 字数 531 浏览 5 评论 0原文

pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(153,356,38,36) ]; 

pageControl.userInteractionEnabled =YES;
pageControl.numberOfPages = 2; 
pageControl.currentPage = 1;
pageControl.enabled = TRUE;
[pageControl setHighlighted:YES];

[pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:pageControl];
}
- (IBAction) changePage:(id)sender 
{


}

我正在以编程方式创建页面控件,并且我想在单击页面控件时显示新的视图控制器。我需要如何实现这个changePage方法?有人可以帮忙吗?

pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(153,356,38,36) ]; 

pageControl.userInteractionEnabled =YES;
pageControl.numberOfPages = 2; 
pageControl.currentPage = 1;
pageControl.enabled = TRUE;
[pageControl setHighlighted:YES];

[pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:pageControl];
}
- (IBAction) changePage:(id)sender 
{


}

I'm programmatically creating page control and i want to display new view controllers on click of page control. How i need to implement this changePage method? Can anyone help?

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

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

发布评论

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

评论(2

雨落星ぅ辰 2024-11-13 12:38:20

您可以显示两个视图,而不是显示两个不同的视图控制器。您可以保持选中第一个点并显示第一个视图,并在屏幕右侧显示下一个视图。当用户点击第二个点时,使 UIView 动画类似于 UINavigationController 中的推送。因此,您可以使用 UIView 动画进行推送和弹出。

如果要显示视图控制器,则页面控件需要在两个视图控制器中显示,以便用户可以从一个视图控制器切换到另一个视图控制器。在这种情况下,您需要将页面控件添加到主窗口中的视图中,以便其在任何地方都可见。

You can show two views instead of showing two different view controller. You can keep first dot selected and show first view and have next view out of screen, to its right. When user taps second dot, make UIView animation similar to pushing in UINavigationController. Thus, you do push and pop with UIView animation.

If you want to show view controllers, then the page control needs to be shown in both the view controller, so that user can switch from one to another. In such case, you need to have the page control in a view, added in the main window, so that its visible everywhere.

晨曦慕雪 2024-11-13 12:38:20

编写更改页面的方法的最简单方法如下:

- (IBAction)changePage:(id)sender {
    CGrect frame;
    frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;
    [self.scrollView scrollRectToVisible:frame animated:YES];
}

编辑:如果您尝试通过单击点来简单地更改视图控制器,则需要设置页面,以便主视图在底部和上面的另一个 UIView(我们将称之为控制器视图)占据了大部分屏幕,但不覆盖页面控件。

您还需要在头文件中添加 PageOne *pageOneController;PageTwo *pageTwoController; 。这将有助于防止内存泄漏。

因此,当您选择另一个页面时,您将调用 changePage 方法,

- (IBAction)changePage:(id)sender {
    if (sender.currentPage == 1) {
        // make sure only one instance exists at a time so there aren't any memory leaks;
        if (pageOneController != nil) {
            pageOneController = nil;
            [pageOneController release];
        }
        // load up page one;
        pageOneController = [[PageOne alloc] initWithNibName:@"PageOneNib" bundle:nil];
        // set this as the primary view;
        controllerView = viewController.view;
    } else {
        // do the same for your other page;
    }
}

这应该可以满足您的需要

The easiest way to program a method to change pages would be the following:

- (IBAction)changePage:(id)sender {
    CGrect frame;
    frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;
    [self.scrollView scrollRectToVisible:frame animated:YES];
}

EDIT: if you are trying to simply change the view controller by clicking the dots, you will need to set your page up so that the main view has a UIPageControl at the bottom and another UIView (we will call this controllerView) above it taking up most of the screen, but not overlaying the page control.

You will also want PageOne *pageOneController; and PageTwo *pageTwoController; in your header file. This will help prevent memory leaks.

So when you select another page, you'll call your changePage method

- (IBAction)changePage:(id)sender {
    if (sender.currentPage == 1) {
        // make sure only one instance exists at a time so there aren't any memory leaks;
        if (pageOneController != nil) {
            pageOneController = nil;
            [pageOneController release];
        }
        // load up page one;
        pageOneController = [[PageOne alloc] initWithNibName:@"PageOneNib" bundle:nil];
        // set this as the primary view;
        controllerView = viewController.view;
    } else {
        // do the same for your other page;
    }
}

This should do the trick for you

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