我已经为这个问题苦苦挣扎了一段时间,因此我们将不胜感激。
情况如下:我的应用程序有一个名为 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
对象将被释放。
我想知道是否有办法解决这个问题?有没有人以另一种方式做过类似的事情?
这里的问题是:当选择过于复杂而无法使用UIAlertView
时,如何正确获取用户的选择。
抱歉这个问题太长了,我已尽可能简化了它。感谢您的宝贵时间,非常感谢任何链接、评论或任何类型的帮助
最佳答案
为什么要在 IBAction 中初始化 MyEngine *engine
,如果您想使用 MyEngine 对象,为什么不在 InitialViewController
中进行全局声明,然后在 IBaction 中调用[engine start]
。然后,当委托(delegate)方法返回选定的索引时,您可以将其应用于初始 View Controller 中的全局 int 并继续您的方式。希望这是有道理的
关于objective-c - 当选择过于复杂而无法使用 UIAlertView 时如何正确获取用户的选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11472751/