我一直在努力解决这个问题,所以任何帮助都会非常感激。
情况如下:我的应用程序有一个名为UIViewController
的{{1}}子类。此视图控制器具有UIButton,当按下该按钮时,它会创建一个名为InitialViewController
的{{1}}子类。像这样:
NSObject
在MyEngine
内,我以模态方式呈现ViewController(@interface InitialViewController : UIViewController <MyEngineDelegate>
...
@end
@implementation InitialViewController
...
-(IBAction)pressedButton:(id)sender {
MyEngine *engine = [[MyEngine alloc] init];
[engine start];
}
)以获得用户的选择:
start
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
我检查了每个连接,所有代表都正常工作。
出了什么问题是:
当MyEngine在delegate
的实现中收到用户的选择时,它无法正常继续,因为它的所有属性已经消失-(IBAction)didSelectConflict:(id)sender {
UISegmentedControl *seg = (UISegmentedControl*) sender;
[self.delegate didResolveConflictChoice:seg.selectedSegmentIndex];
[self dismissModalViewControllerAnimated:YES];
}
。
当didSelectConflict:
显示null
时,程序会继续执行,当MyEngine
完成后,它会返回ConflictViewController
,当此方法关闭时,{ {1}}对象被释放。
我想知道的是,如果有办法解决这个问题?有没有人用另一种方式做过这样的事情?
这里的问题是:当选择过于复杂而无法使用start
时,如何正确选择用户。
很抱歉这个长期的问题,我尽可能地简化了它。感谢您的时间,非常感谢任何链接,评论或任何类型的帮助
答案 0 :(得分:2)
为什么要在IBAction中初始化MyEngine *engine
,如果您希望使用MyEngine对象,为什么不在InitialViewController
中进行全局声明,只需在[engine start]
中调用{{1}} IBAction为。然后,当委托方法返回所选索引时,您可以将其应用于初始视图控制器中的全局int并继续。希望有意义
答案 1 :(得分:0)
让您的方法以
开头-(void) startWithDelegate:(id)del {
ConflictViewcontroller *cvc = [[ConflictViewController alloc] initWithNibName:@"ConflictViewController" bundle:nil];
cvc.modalPresentationStyle = UIModalPresentationFormSheet;
cvc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
cvc.delegate = del;
UIWindow *window = [(MyAppDelegate *) [[UIApplication sharedApplication] delegate] window];
[[window rootViewController] presentModalViewController:cvc animated:YES];
}
和
-(IBAction)pressedButton:(id)sender {
MyEngine *engine = [[MyEngine alloc] init];
[engine startWithDelegate:self];
}
在didResolveConflictChoice:
中实施InitialViewController
并在那里接到代表电话。
或如果合适,您可以使用UIActionSheet。