ios - 如何正确切换 View

标签 ios objective-c view

我正在尝试使用滑动手势来切换 View 。我的滑动工作正常并且正在被处理,但不知何故 View 开关无法正常工作。

以下滑动 handle 代码:

-(void)handleSwipeLeft:(UISwipeGestureRecognizer *)gestureRecognizer{  
    CGPoint location = [gestureRecognizer locationInView:tableView];  
    NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:location];  
    if(indexPath){  
        userCell *cell = [tableView cellForRowAtIndexPath:indexPath]; //userCell inherits from UITableViewCell so why complain?  
        StatisticsViewController *statisticsView = [StatisticsViewController new];  
        [self.view removeFromSuperview];  
        [self.view addSubview:statisticsView.view];  
        //[self.view insertSubview:statisticsView.view atIndex:0];  
    }  
}  `

我之前用过insertSubview: atIndex:0,我已经注释掉了。 我的问题是我尝试添加的新 View 没有被添加。 removeFromSuperview 在我得到黑屏(因此删除 View )的意义上起作用。但我不明白为什么我不能添加其他 View (我创建实例的 statisticsView;我正在使用 Storyboard 并有一个带有一些标签和颜色的 StatisticsViewController 只是为了测试),我只是在删除看法。 顺便说一句,我正在使用名为 FrontPageViewController 的 Root View Controller 工作。 我不知道在 frontpageviewcontroller 中加载一个干净的 View Controller (root) 是否更“正确”,然后在我需要加载另一个 View 时将其切换出去。 (但我不明白为什么我目前的方法行不通)。 提前致谢

最佳答案

您的主要问题是因为您将新 View 添加到刚刚删除的同一 View 中。您应该将其添加到其父 View 。

遗憾的是,关于您的方法,我无法使用评论来澄清一些要点,但您不应该那样进行 View 切换。

你不应该从 Controller 中替换 View ,你应该在 Controller 之间切换。

您可以通过在 Storyboard 中创建一个带有标识符的 segue 并将以下内容添加到您的手势处理程序方法来做到这一点:

[self performSegueWithIdentifier: @"MySegue" sender: self];

如果您的唯一目的是替换同一个 Controller 中的 View ,您应该以编程方式创建它(只是 View 而不是 Controller ),而不是通过将 View 从一个 Controller 添加到另一个 Controller 。

如果您需要有关 segue 的任何帮助,我建议您先阅读 https://developer.apple.com/library/ios/referencelibrary/GettingStarted/RoadMapiOS/SecondTutorial.html 中的“添加一个 Segue 以向前导航”

要手动执行转场,您需要为您创建的转场选择一个标识符。

关于ios - 如何正确切换 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27126995/

相关文章:

ios - 录音机无法在装有 iOS 7 的 iPhone 中使用

ios - xcodebuild - 如何使用 xcode 的build设置

optimization - 提高 Oracle View 的性能

ios - 为什么 UIBezierPath 比 CAShapeLayer 快

iphone - 在 iOS 中使用 3 个选项进行切换

ios - 当点击 NSSetUncaughtExceptionHandler 时,使用 CoreGraphics 在屏幕上绘制

ios - 如何在单元格 TableView 上右对齐 UILabel?

view - 如何从 SQL View 创建 Prisma 模型

MySQL:在 FROM 子句限制中使用子查询查看

ios - 仅将圆角添加到 UITableView 的顶部?