简体   繁体   English

如何实例化第二个ViewController并停止第一个ViewController的方法

[英]How to instantiate a second ViewController and stop the methods of the first ViewController

I have a very basic application in which a second ViewController is instantiated if the conditions of an 'if' statement are true. 我有一个非常基本的应用程序,如果'if'语句的条件为真,则实例化第二个ViewController。 Upon loading of the second ViewController, the methods of the first ViewController still run. 加载第二个ViewController后,第一个ViewController的方法仍然运行。 I need all previous methods to stop for the application to run correctly. 我需要所有以前的方法来停止应用程序正确运行。

// In FirstViewController.h //在FirstViewController.h中

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController
{
    NSTimeInterval beginTouchTime;
    NSTimeInterval endTouchTime;
    NSTimeInterval touchTimeInterval;
}

@property (nonatomic, readonly) NSTimeInterval touchTimeInterval;

- (void) testMethod;

@end

// In FirstViewController.m //在FirstViewController.m中

#import "FirstViewController.h"
#import "SecondViewController.h"

@implementation FirstViewController

@synthesize touchTimeInterval;

- (void)viewDidLoad
{
    [super viewDidLoad]; 
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void) testMethod
{
if (touchTimeInterval >= 3)
{
NSLog(@"Go to VC2");
SecondViewController *secondBViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
             [self presentViewController:secondViewController animated:YES completion:nil];
}
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    beginTouchTime = [event timestamp];
    NSLog(@"Touch began");
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    endTouchTime = [event timestamp];
    NSLog(@"Touch ended");

    touchTimeInterval = endTouchTime - beginTouchTime;
    NSLog(@"Time interval: %f", touchTimeInterval);

    [self testMethod]; // EDIT: USED TO BE IN viewDidLoad

}

@end

The second screen successfully loads but the log messages persist, meaning that the methods of FirstViewController still occur, although in the view of the SecondViewController. 第二个屏幕成功加载但日志消息仍然存在,这意味着FirstViewController的方法仍然出现,尽管在SecondViewController的视图中。 What am I doing wrong? 我究竟做错了什么?

看看- (void)viewWillDisappear:(BOOL)animated- (void)viewDidDisappear:(BOOL)animated您可以在第一个视图控制器上实现这些方法来停止/禁用任何活动或触摸检测。

What you're seeing is the result of the way events are handled in UIKit (check out the "Event Handling Guide for iOS", especially the "Event Delivery: The Responder Chain" section). 您所看到的是UIKit中事件处理方式的结果(请查看“iOS事件处理指南”,尤其是“事件传递:响应者链”部分)。 So what's happening is that since SecondViewController's view doesn't override touchesBegan or touchesEnded, the touch is passed up the responder chain, first to SecondViewController, and then to FirstViewController, which finally does handle those events (FirstViewController is still the window's root view controller after the modal presentation). 所以正在发生的事情是,由于SecondViewController的视图不会覆盖touchesBegan或touchesEnded,因此触摸将向上传递响应链,首先传递给SecondViewController,然后传递给FirstViewController,它最终会处理这些事件(FirstViewController仍然是窗口的根视图控制器之后模态演示)。

Two ways to fix this. 解决这个问题的两种方法。 You can override touchesBegan and touchesEnded in SecondViewController (or its view I guess), and just have empty methods. 您可以在SecondViewController(或我认为它的视图)中覆盖touchesBegan和touchesEnded,并且只有空方法。

Another way would be to subclass FirstViewController's view, and override the methods there, rather than in the controller. 另一种方法是将FirstViewController的视图子类化,并覆盖那里的方法,而不是在控制器中。 You would still have to do the presentation of SecondViewController from the controller -- you could call a method to do that from the view with [self.nextResponder someMethod]. 您仍然需要从控制器进行SecondViewController的演示 - 您可以使用[self.nextResponder someMethod]从视图中调用方法来执行此操作。

Is SecondViewController a subclass of FirstViewController? SecondViewController是FirstViewController的子类吗? If so the touch events will escalate through the inheritance chain until they are handled. 如果是这样,触摸事件将通过继承链升级,直到它们被处理。 You could override these methods in SecondViewController and have them do nothing (or whatever else you want). 你可以在SecondViewController中覆盖这些方法,让它们什么都不做(或者你想要的任何其他东西)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 首次启动后如何停止重新加载ViewController - How To Stop Reloading ViewController after First Launch 从第一个ViewController到第二个ViewController的图像 - Image from first ViewController to second ViewController 从第二个ViewController返回第一个ViewController - Going back to first ViewController from second ViewController 如何在动画结束时实例化viewController? - how to instantiate a viewController at the end of an animation? 在iOS7中,如何停止First viewcontroller自动旋转? - in iOS7 how do you stop the First viewcontroller autorotating? scrollview不会在第二个viewcontroller中滚动,而只能在第一个主viewcontroller中滚动 - scrollview does not scroll in second viewcontroller , but only in first home viewcontroller 单击第一个ViewController中的按钮后更改第二个ViewController的标签文本 - Changing label text of second ViewController upon clicking button in first ViewController 如何使用给定的根ViewController和初始ViewController实例化情节提要? - How do I instantiate a storyboard with a given root viewcontroller and initial viewcontroller? 如何停止其他视图控制器中的计时器? - How to stop the timer in other viewcontroller? 如何从SWIFT中的UIView实例化ViewController - how to instantiate ViewController from UIView in SWIFT
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM