我有一个驻留在弹出窗口上的分段控制器(两个段)。我的prepareForSeque方法工作正常,返回正确的默认selectedSegmentIndex。但是当我选择其他段并退出弹出窗口时,我的应用程序在执行popoverControllerDidDismissPopover方法时崩溃,如下面的代码片段所示。
我做错了什么?我希望分段控制器保留新选择的段,我认为这就是popoverControllerDidDismissPopover方法所做的。显然我的理解在这里是错误的,但在哪里?提前感谢您提供的任何帮助。
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UIStoryboardPopoverSegue *popoverSegue;
popoverSegue = (UIStoryboardPopoverSegue *)segue;
UIPopoverController *popoverController;
popoverController = popoverSegue.popoverController;
popoverController.delegate = self;
SettingsEditorViewController *editorVC;
editorVC = (SettingsEditorViewController *)popoverController.contentViewController;
audioMessageChoiceIndex = editorVC.audioMessageChoice.selectedSegmentIndex;
NSLog(@"audioMessageChoiceIndex = %d", audioMessageChoiceIndex);
}
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
NSInteger *newAudioMessageChoiceIndex = (NSInteger *)((SettingsEditorViewController *)
popoverController.contentViewController).audioMessageChoice.selectedSegmentIndex;
popoverController.delegate = self;
SettingsEditorViewController *editorVC;
editorVC = (SettingsEditorViewController *)popoverController.contentViewController;
editorVC.audioMessageChoice.selectedSegmentIndex = *(newAudioMessageChoiceIndex);
}
答案 0 :(得分:0)
我不确定你在这做什么:
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
NSInteger *newAudioMessageChoiceIndex = (NSInteger *)((SettingsEditorViewController *)
popoverController.contentViewController).audioMessageChoice.selectedSegmentIndex;
popoverController.delegate = self;
SettingsEditorViewController *editorVC;
editorVC = (SettingsEditorViewController *)popoverController.contentViewController;
editorVC.audioMessageChoice.selectedSegmentIndex = *(newAudioMessageChoiceIndex);
}
我猜应用程序崩溃是因为您尝试将小NSInteger
取消引用作为指针。
重写:
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
SettingsEditorViewController * editorVC =
(SettingsEditorViewController *)popoverController.contentViewController;
NSInteger newAudioMessageChoiceIndex = editorVC.audioMessageChoice.selectedSegmentIndex;
editorVC.audioMessageChoice.selectedSegmentIndex = newAudioMessageChoiceIndex;
}
您是否尝试使用自己的值设置selectedSegmentIndex
?在委托方法中设置popover的委托没有意义。
如果您希望在弹出窗口会话之间保持UISegmentedControl
状态,只需将其保留在调用者属性中并在显示之前设置selectedSegmentIndex
:
@property NSInteger audioMessageChoiceIndex;
@synthesize audioMessageChoiceIndex;
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
/* ... */
SettingsEditorViewController *editorVC = (SettingsEditorViewController *)popoverController.contentViewController;
editorVC.audioMessageChoice.selectedSegmentIndex = self.audioMessageChoiceIndex;
}
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
SettingsEditorViewController * editorVC =
(SettingsEditorViewController *)popoverController.contentViewController;
self.audioMessageChoiceIndex = editorVC.audioMessageChoice.selectedSegmentIndex;
}