【发布时间】:2013-07-29 20:01:53
【问题描述】:
我有一个非常基本的应用程序,如果“if”语句的条件为真,则在其中实例化第二个 ViewController。加载第二个 ViewController 后,第一个 ViewController 的方法仍然运行。我需要停止所有以前的方法以使应用程序正确运行。
//在FirstViewController.h中
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController
{
NSTimeInterval beginTouchTime;
NSTimeInterval endTouchTime;
NSTimeInterval touchTimeInterval;
}
@property (nonatomic, readonly) NSTimeInterval touchTimeInterval;
- (void) testMethod;
@end
//在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
第二个屏幕成功加载,但日志消息仍然存在,这意味着 FirstViewController 的方法仍然存在,尽管在 SecondViewController 的视图中。我做错了什么?
【问题讨论】:
-
当你的第一个视图控制器不在屏幕上时,它不应该响应任何触摸。你说的就是这种情况吗?
-
这正是正在发生的事情。我觉得这很奇怪,而且它也使应用程序运行得非常慢。
-
这就是你所有的代码吗?当我尝试它时,它永远不会进入第二个视图控制器,因为 testMethod 永远不会再次调用,并且第一次调用时,touchTimeInterval 为 0。
-
很抱歉。当我输入代码时,我的 Xcode 项目没有打开。我的意思是在 touchesEnded 中调用该方法。请进行此更改,看看您是否遇到同样的问题。谢谢
标签: ios objective-c