【问题标题】:Navigating from ScrollViewController Content view to another view on stack从 ScrollViewController 内容视图导航到堆栈上的另一个视图
【发布时间】:2012-10-05 22:57:05
【问题描述】:

我正在尝试使用 UIButton 从 UIScrollView 的内容视图导航到另一个视图。我不想以模态方式显示这个其他视图,并且将视图推送到堆栈上对我不起作用。

示例: 我的 Viewcontroller 包含一个具有 2 个内容视图的 Scrollview。在这些内容视图中,我想将另一个视图推送到堆栈中。

我正在尝试做的事情可能吗?

【问题讨论】:

  • 哎呀。谢谢。我已经编辑了我的问题,希望更有意义。
  • 您是尝试使用 UINavigationController 进行简单推送,还是需要具有不同子视图作为“页面”的滚动视图?
  • 我有一个导航控制器,其中包含一个滚动视图(具有子视图)我想从其中一个子视图中导航到一个视图.. 非模态的。
  • 是您正在寻找的导航,例如邮件和音乐应用程序之一吗?(我问这个是为了了解您真正想要的是滚动视图还是导航控制器不同的视图控制器,每个都有自己的视图,就足够了)
  • 我需要从我的一个滚动视图内容视图中导航到自定义视图。我所拥有的是一个有 2 个玩家的游戏。每个玩家都在滚动视图的一个内容视图上。在那个内容视图中,我需要定期导航到另一个视图。但如果可能的话,不要使用模式演示。

标签: iphone ios uiviewcontroller navigation


【解决方案1】:

好的,让我们看看我是否足够了解您的问题。

@interface MyViewController ()
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *firstPlayerView;
@property (nonatomic, strong) UIView *secondPlayerView;
@end

@implementation MyViewController

-(UIView *)firstPlayerView
{
    if (!_firstPlayerView) {
        _firstPlayerView = [[UIView alloc] initWithFrame:self.view.bounds];
        // set up your view as you like and place the button to go to the second player
        // view when you need to.
        // let's suppose that you called that button "goToSecondPlayerViewButton"
        [goToSecondPlayerViewButton addTarget:self
                                       action:@selector(switchPlayer) 
                             forControlEvents:UIControlEventTouchUpInside];
    }
    return _firstPlayerView;
}

-(UIView *)secondPlayerView
{
    if (!_secondPlayerView) {
        _secondPlayerView = [[UIView alloc] initWithFrame:CGRectOffset(self.view.bounds, self.view.bounds.size.width, 0)];
        // set up your view as you like and place the button to go to the first player
        // view when you need to.
        // let's suppose that you called that button "goToFirstPlayerViewButton"
        [goToFirstPlayerViewButton addTarget:self
                                      action:@selector(switchPlayer) 
                            forControlEvents:UIControlEventTouchUpInside];
    }
    return _secondPlayerView;
}

-(UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        _scrollView.autoresizingMask = UIViewAutoresizingNone;
        [_scrollView addSubview:self.firstPlayerView];
        [_scrollView addSubview:self.secondPlayerView];
        _scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, self.view.frame.size.height);
        _scrollView.pagingEnabled = YES;
        _scrollView.scrollEnabled = NO;
        _scrollView.bounces = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
    }
    return _scrollView;
}

-(void)switchPlayer
{
    if(self.scrollView.contentOffset.x == 0) {
        [self.scrollView scrollRectToVisible:CGRectMake(self.scrollView.bounds.size.width, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height) animated:YES];
        self.title = @"Second Player";
    } else {
        [self.scrollView scrollRectToVisible:self.scrollView.bounds animated:YES];
        self.title = @"First Player";
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.scrollView];
    self.title = @"First Player";
}

@end

希望能解决你的问题!我还没有测试过代码,所以如果您遇到问题,请发表评论,我会尽力提供帮助。

已编辑:要添加视图,您只需放大滚动视图 contentSize 并将该视图添加为其子视图

-(UIView *)thirdView
{
    if (!_thirdView) {
        _thirdView = [[UIView alloc] initWithFrame:CGRectOffset(self.view.bounds * 2, self.view.bounds.size.width, 0)];
        // set up your view as you like 
    }
    return _thirdView;
}

-(void)addThirdView
{
    self.scrollView.contentSize = CGSizeMake(self.scrollView.bounds.size.width * 3, self.scrollView.bounds.size.height);
    [self.scrollView addSubview:self.thirdView];
    [self.scrollView scrollRectToVisible:CGRectMake(self.scrollView.bounds.size.width * 2, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height) animated:YES];
}

您可以概括这一点并首先设置您的内容大小,然后使用一个将索引作为参数的方法。

-(void)scrollToViewAtIndex:(NSInteger)index
{
    [self.scrollView scrollRectToVisible:CGRectMake(self.scrollView.bounds.size.width * index, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height) 
                                animated:YES]; 
}

【讨论】:

  • 老兄.. 感谢您花这么多时间帮助我。对此,我真的非常感激。这几乎就在那里..我想弄清楚的是如何从您的示例中的“firstPlayerView”或“secondPlayerView”中将另一个视图推送到堆栈中。
猜你喜欢
  • 2014-09-17
  • 2023-03-25
  • 2016-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-28
  • 2013-08-08
相关资源
最近更新 更多