我已经为这个问题苦苦挣扎了一段时间,所以非常感谢任何帮助。
情况如下:我的应用程序有一个名为 InitialViewController 的 UIViewController 子类。这个 View Controller 有一个 UIButton,当按下该按钮时,它会创建一个名为 MyEngine 的 NSObject 子类。像这样:
@interface InitialViewController : UIViewController <MyEngineDelegate>
...
@end
@implementation InitialViewController
...
-(IBAction)pressedButton:(id)sender {
MyEngine *engine = [[MyEngine alloc] init];
[engine start];
}
在 start 中,我以模态方式呈现一个 ViewController (ConflictViewController) 以获得用户的选择:
@interface MyEngine : NSObject <ConflictViewControllerDelegate>
...
-(void) start;
@end
@implementation MyEngine
...
-(void) start {
ConflictViewcontroller *cvc = [[ConflictViewController alloc] initWithNibName:@"ConflictViewController" bundle:nil];
cvc.modalPresentationStyle = UIModalPresentationFormSheet;
cvc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
cvc.delegate = self;
UIWindow *window = [(MyAppDelegate *) [[UIApplication sharedApplication] delegate] window];
[[window rootViewController] presentModalViewController:cvc animated:YES];
}
@end
ConflictViewController 非常简单。它只是等待用户做出决定,当用户按下按钮时,它会将消息发送给 delegate,然后自行解散。
-(IBAction)didSelectConflict:(id)sender {
UISegmentedControl *seg = (UISegmentedControl*) sender;
[self.delegate didResolveConflictChoice:seg.selectedSegmentIndex];
[self dismissModalViewControllerAnimated:YES];
}
我检查了每一个连接,所有的委托(delegate)都在正常工作。
出了什么问题是:
当 MyEngine 在它的 didSelectConflict: 实现中收到用户的选择时,它无法正常继续,因为它的所有属性都已变为 null。
当 MyEngine 显示 ConflictViewController 时,程序继续执行,当 start 完成时,它返回到 pressedButton: 并且当此方法关闭时,MyEngine 对象被释放。
我想知道是否有办法解决这个问题?有没有人以另一种方式做过这样的事情?
The question here is: How to get the user's choice properly when the choice is too complex to use UIAlertView.
抱歉这个问题太长了,我已经尽可能地简化了它。感谢您抽出宝贵时间,非常感谢任何链接、评论或任何类型的帮助
最佳答案
为什么要在 IBAction 中初始化 MyEngine *engine,如果您希望使用 MyEngine 对象,为什么不在 InitialViewController 中进行全局声明,而只是在 IBaction 中调用 [engine start]。然后,当委托(delegate)方法返回选定的索引时,您可以将其应用于初始 View Controller 中的全局 int 并继续您的工作。希望这是有道理的
关于objective-c - How to get the user's choice properly when the choice is too complex to use UIAlertView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11472751/