【发布时间】:2014-07-15 16:38:08
【问题描述】:
这里是 iOS 程序员新手,如果我遗漏了一些简单的东西,非常抱歉,但是...
我有一个名为 LVSTSPMasterViewController 的 UIViewController 类,它的视图是在 IB 中构建的。该视图包含一个 UIScrollView(在 IB 中添加),并且该滚动视图具有 LVSTSPView 类型的子视图。 LVSTSPView 有一个 LVSTSPViewController 类型的控制器。
我想响应 LVSTSPView 中的触摸,所以我在 LVSTSPViewController.m 中添加了手势识别器。当我执行手势(例如,长按)时,代码崩溃,并在 main.m 中显示消息“EXC_BAD_ACCESS (code=1, address=...)”。
相关代码:
在 LVSAppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
// Get pointer to app bundle
NSBundle *appBundle = [NSBundle mainBundle];
// Get xib file
LVSTSPMasterViewController *tspmvc = [[LVSTSPMasterViewController alloc] initWithNibName:@"LVSTSPMasterViewController" bundle:appBundle];
self.window.rootViewController = tspmvc;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
在 LVSMasterViewController.m:
@interface LVSTSPMasterViewController () <UIScrollViewDelegate>
// IBOutlet declarations
@property (nonatomic, weak) IBOutlet UIScrollView *TSPScrollView;
// Pointers for convenience
@property (strong, nonatomic) LVSTSPView *TSPView;
@end
@implementation LVSTSPMasterViewController
- (void)viewDidLoad
{
// Create LVSTSPViewController
LVSTSPViewController *tspvc = [[LVSTSPViewController alloc] init];
// Set up pointer to LVSTSPView
self.TSPView = (LVSTSPView *)tspvc.view;
// Set frame of LVSTSPView
self.TSPView.frame = self.TSPScrollView.bounds;
// Set tspvc's view as subview of TSPScrollView
[self.TSPScrollView addSubview:tspvc.view];
// Set up scroll view
self.TSPScrollView.pagingEnabled = NO;
self.TSPScrollView.contentSize = self.TSPView.frame.size;
self.TSPScrollView.minimumZoomScale = 1.0;
self.TSPScrollView.maximumZoomScale = 3.0;
// Set scroll view's delegate property
self.TSPScrollView.delegate = self;
}
在 LVSTSPViewController.m:
- (void)loadView
{
// Create view
LVSTSPView *view = [[LVSTSPView alloc] initWithFrame:CGRectZero];
self.view = view;
// Long-press recognizer
UILongPressGestureRecognizer *pressRecognizer =
[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self.view addGestureRecognizer:pressRecognizer];
}
- (void)longPress:(UIGestureRecognizer *)gr
{
NSLog(@"longPress:");
}
另一个注意事项:如果我在 LVSTSPMasterViewController.m 中的viewDidLoad: 中设置手势识别器,就像这样 --
UILongPressGestureRecognizer *pressRecognizer =
[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self.TSPView addGestureRecognizer:pressRecognizer];
--(当然也可以在 LVSTSPMasterViewController.m 中添加longPress:),然后就可以了。但这似乎不是正确的方法,因为 LVSTSPMasterViewController 不是 LVSTSPView 的视图控制器。
任何帮助将不胜感激!
【问题讨论】:
标签: ios uiscrollview uigesturerecognizer