用子视图覆盖视图,不知道调用VC

时间:2011-05-20 18:45:42

标签: iphone objective-c viewcontroller

我需要一个视图(infoView)作为叠加显示在另一个视图的顶部。由于这个infoView应该可以从应用程序的每个视图调用(例如introView),我希望代码在infoViews VC中,并且当currentView(introView)中的动作发生时调用它的方法。我不能使用push和pop,因为我需要更改背景颜色(infoView),特别是调用视图的alpha(introView),所以现在我使用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回答后编辑: intro.m

- (IBAction) openInf:(id)sender {
introViewController *introVC; 
[infoScreenVC openInfoMethod:];}

info.h

- (void) openInfoMethod:(introViewController *introVC);

info.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发生时,它调用infoViewController的方法closeInfoMethod,如self closeInfoMethod:introVC

  • 在该方法中,我从Superview中删除了self.view并将introVC.view的Alpha设置为1,如introVC.view setAlpha:1.0f

所以代码片段是
intro.h

IBOutlet InfoscreenViewController *infoScreenVC;

@property (nonatomic, retain) IBOutlet InfoscreenViewController *infoScreenVC;
- (IBAction) openInf:(id)sender;

intro.m

@synthesize infoScreenVC;
- (IBAction) openInf:(id)sender {
UIViewController *introVC = self;
[infoScreenVC openInfoMethod:introVC];
}

info.h:

- (void) openInfoMethod:(UIViewController *)rootVC;
- (void) closeInfoMethod:(UIViewController *)callingVC;

info.m

- (void) closeInfoMethod:(UIViewController *)callingVC;{
[self.view removeFromSuperview];
[callingVC.view setAlpha:1.0f];
}

- (IBAction) closeInfoPressed{
[self closeInfoMethod:introVC];
[self.view removeFromSuperview];
}

3 个答案:

答案 0 :(得分:1)

如果您的问题是“弄清楚如何告诉我的应用程序谁是调用VC回到那里”,为什么不向openInfo选择器添加一个参数,如下所示:

info.h

- (void) openInfoMethod:(introViewController *)introVC;

info.m

- (void) openInfoMethod:(introViewController *)introVC {
    <your implementation here>
}

这对你有用吗?

编辑:来自intro.m的代码遇到了一个小问题,

- (IBAction) openInf:(id)sender {
    introViewController *introVC; 
    [infoScreenVC openInfoMethod:];
}

的确,您没有初始化introVC变量,因此当您将其传递给-openInfoMethod:时,它会有一些奇怪的值并导致崩溃。

就我的代码而言,intro.m应该是introViewController的实施文件,因此您只需致电:

    [infoScreenVC openInfoMethod:self];

但请在执行此操作之前,确认self实际上是您的introViewController

答案 1 :(得分:0)

这应该是这个

  • (void)openInfoMethod:(introViewController *)introvc你以错误的方式传递参数。

答案 2 :(得分:0)

如果我理解你的问题,你的infoView应该是UIView的子类,并使用以下任何一个视图控制器实例化:

InfoView *infoView = [[InfoView alloc] initWithFrame:CGRectMake(originX,originY,width,height)];

然后,您只需将其添加为视图控制器视图的子视图:

[self.view addSubView:infoView];
[infoView release]; // It's safe to release it here as your view controller's view is retaining it

完成后,只需致电

即可
[infoView removeFromSuperview];

另外,您可以在infoView中创建一些简单的方法,包括在显示或删除视图时引入动画。这是淡入和淡出的示例,假设您在创建视图时最初将alpha设置为零:

- (void)fadeIn {
    [UIView animateWithDuration:1.0f animations:^{self.alpha = 1.0f}];
}

淡出

- (void)fadeOut {
    [UIView animateWithDuration:1.0f animations:^{self.alpha = 0f} 
                                     completion:^(BOOL finished){self removeFromSuperView}];
}