2

我想切换到另一个视图控制器。我的视图上有一个 UIButton,UIButton 通过使用以下代码有一个 UILongPressGestureRecognizer:

UILongPressGestureRecognizer *buttonLongPressRecognizer;
buttonLongPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(LoadButtonSettings:)];

buttonLongPressRecognizer.numberOfTouchesRequired = 1;
buttonLongPressRecognizer.minimumPressDuration = 2.0;

[NewButton addGestureRecognizer:buttonLongPressRecognizer];

我用来切换 viewControllers 的操作是这样的:

- (IBAction)LoadButtonSettings:(id)sender {

[ButtonSettingsViewController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];

[self presentViewController:ButtonSettingsViewController animated:YES completion:NULL];

}

问题是当我长按按钮时,我的应用程序崩溃并给我一个 SIGABRT 错误。奇怪的是,它只发生在我的 iPhone 上,而不是模拟器上。

我也尝试过使用

    [self presentModalViewController:ButtonSettingsViewController animated:YES];

并遇到了同样的问题。据我所知,SIGABRT 意味着存在内存问题,我不明白,因为自动引用计数器已打开。

有想法该怎么解决这个吗?

提前致谢 :)

4

2 回答 2

3

如果 ButtonSettingsViewController 是视图控制器的类型,则需要先对其进行初始化:

- (IBAction)LoadButtonSettings:(id)sender {
    // init & alloc - Replace with your custom view controllers initialization method (if applicabale)
    ButtonSettingsViewController *viewController = [[ButtonSettingsViewController alloc] initWithNibNamed:@"ButtonSettingsViewController" bundle:nil];

    [viewController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [self presentViewController:viewController animated:YES completion:NULL];
}
于 2011-09-18T22:21:21.893 回答
2

presentModalViewController:animated: 需要一个视图控制器对象。您正在向它传递一个类(ButtonSettingsViewController)。首先实例化视图控制器对象:

ButtonSettingsViewController *viewControllerObject = [[ButtonSettingsViewController alloc] initWithNibName:@"ButtonSettingsViewController" bundle:nil];

然后设置该视图控制器对象的 modalTransitionStyle 属性:

viewControllerObject.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

然后呈现它:

[self presentModalViewController:viewControllerObject animated:YES];
于 2011-09-18T22:22:54.533 回答