【发布时间】:2011-05-20 18:45:42
【问题描述】:
我需要一个视图 (infoView) 显示为覆盖在另一个视图之上。由于这个 infoView 应该可以从应用程序的每个视图(例如 introView)中调用,我希望代码位于 infoViews VC 中,并且只在 currentView(introView)的操作发生时调用它的方法。我不能使用 push 和 pop,因为我需要更改背景颜色 (infoView),尤其是调用视图 (introView) 的 alpha,所以我现在使用 insertSubview。
我现在的代码: introVC .h
- (IBAction) openInf:(id)sender;
IBOutlet InfoVC *infoScreenVC;
introVC .m
- (IBAction) openInf:(id)sender {
[infoScreenVC openInfoMethod];}
infoVC .h
- (IBAction) closeInfoPressed;
- (void) openInfoMethod;
- (void) closeInfoMethod;
infoVC.m
- (IBAction) closeInfoPressed {
[self closeInfoPressed];}
- (void) closeInfoMethod {
[self.view removeFromSuperview];
[self.xx.view setAlpha:1.0f];}
- (void) openInfoMethod {
self.view.backgroundColor = [UIColor clearColor];
[self.xx.view setAlpha:0.2f];
[((MyAppAppDelegate *)[UIApplication sharedApplication].delegate).window
insertSubview: self.infoScreenVC.view aboveSubview: self.xx.view];}
当我按下按钮显示 infoView 时,我的 NSLogs 告诉我该方法已被调用,但我可以看到未添加 Subview。我完全不知道在我的代码中现在它说 xx 的位置插入什么,因为来自 intro 的 VC 引用并没有向我显示屏幕。 如果我将该代码放入 introVC 并对其进行修改,它会显示 infoView,调用正确的方法关闭,但再次无法关闭(当我在 introVC 中时)。我不知道如何告诉我的应用程序谁是调用 VC 的人回到那里。 在某些时候,当所有代码都在 introVC 中时,我什至设法删除了 Subview,但无法将 introVC 的 Alpha 设置回 1。
这两天我确实在为此苦苦挣扎..-.- 或者有没有更简单的解决方案?
非常感谢!
//在sergios回答后编辑: 介绍.m
- (IBAction) openInf:(id)sender {
introViewController *introVC;
[infoScreenVC openInfoMethod:];}
信息.h
- (void) openInfoMethod:(introViewController *introVC);
信息.m
- (void) openInfoMethod:(introViewController *introVC) { //error occurs here
self.view.backgroundColor = [UIColor clearColor];
[self.introVC.view setAlpha:0.2f];
[((MyAppAppDelegate *)[UIApplication sharedApplication].delegate).window
insertSubview: self.infoScreenVC.view aboveSubview: self.introVC.view];}
并且发生的错误说
Expected ')' before 'introVC'
我不确定如何正确传递 VC 引用。 谢谢你的帮助!!
//编辑工作代码:
由于它现在有效,我想总结一下:
- 我将调用 VC (introVC) 提供给 Action openInf 上的 openInfoMethod,例如 [infoVC openInfoMethod:introVC]。
在 openInfoMethod 中,我将调用 VC “保存”在 introVC (?) 类型的局部变量中,并添加覆盖等。
当名为closeInfoPressed的infoViewController的Action发生时,它会像
self closeInfoMethod:introVC一样调用infoViewController的方法closeInfoMethod。在该方法中,我从 Superview 中删除 self.view,并将 introVC.view 的 Alpha 设置为 1,如 introVC.view setAlpha:1.0f
所以coden-ps是
介绍.h
IBOutlet InfoscreenViewController *infoScreenVC;
@property (nonatomic, retain) IBOutlet InfoscreenViewController *infoScreenVC;
- (IBAction) openInf:(id)sender;
介绍.m
@synthesize infoScreenVC;
- (IBAction) openInf:(id)sender {
UIViewController *introVC = self;
[infoScreenVC openInfoMethod:introVC];
}
info.h:
- (void) openInfoMethod:(UIViewController *)rootVC;
- (void) closeInfoMethod:(UIViewController *)callingVC;
信息.m
- (void) closeInfoMethod:(UIViewController *)callingVC;{
[self.view removeFromSuperview];
[callingVC.view setAlpha:1.0f];
}
- (IBAction) closeInfoPressed{
[self closeInfoMethod:introVC];
[self.view removeFromSuperview];
}
【问题讨论】:
标签: iphone objective-c viewcontroller