2011-05-20 27 views
0

我需要一個視圖(infoView)作爲覆蓋顯示在另一個視圖之上。由於這個infoView應該可以從應用程序的每個視圖中調用(例如introView),所以我希望代碼在infoViews VC中,並且在currentView(introView)發生操作時調用它的方法。我不能使用push和pop,因爲我需要改變背景顏色(infoView),特別是調用視圖(introView)的alpha,所以現在我用insertSubview來做。覆蓋視圖與子視圖,不知道調用VC

我的代碼現在: 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告訴我的方法被調用,但我可以看到沒有添加子視圖。我完全不知道要插入什麼,現在它在我的代碼中說xx,因爲來自intro的VC參考沒有向我顯示屏幕。 如果我把這段代碼放在introVC中修改它,它確實顯示了infoView,調用了正確的方法關閉,但是再次無法關閉(當我在introVC中)時。我無法弄清楚如何告訴我的應用程序誰是調用VC回到那裏。 在某些時候,當所有代碼都在introVC中時,我設法甚至刪除了子視圖,但無法將introVC的Alpha設置回一個。

我確實因爲這兩天而掙扎...... -.-或者有沒有更簡單的解決方案?

非常感謝!

//編輯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)到openInfoMethod的行動openInf像[infoVC openInfoMethod:introVC]

  • 在openInfoMethod中,我將「調用VC」保存在類型爲introVC(?)的局部變量中,並添加覆蓋等。

  • 當發生名爲closeInfoPressed的infoViewController的Action時,它會調用infoViewController的closeInfoMethod方法,如self closeInfoMethod:introVC

  • 在該方法中,我從上海華刪除self.view並設置introVC.view公司的Alpha 1像introVC.view setAlpha:1.0F

所以codesnippets是
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]; 
} 

回答

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。 introvc您傳遞參數以錯誤的方式(introViewController *):

+0

+1位更詳細能幫上忙。 – 2011-05-20 19:43:04

+0

@sergio感謝您的回覆,這聽起來像我需要的,但我不知道如何做到這一點..你能更具體一點嗎? (看來我無法在代碼中發佈代碼,請參閱我現在編碼的代碼,謝謝! – Nareille 2011-05-21 19:57:18

+0

@Nareille:您的語法不正確,請參閱我的編輯... – sergio 2011-05-22 06:38:56

0

這應該是這ILKE

  • (無效)openInfoMethod。
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}]; 
} 
+0

感謝您的回覆! infoView需要是UIViewController,因爲應該有一些按鈕,指向其他視圖。我有一個類似的方法,在開始時在調用VC中添加和刪除Subviews,但是由於必須添加更多的viewControllers,我必須將操作添加到每個視圖控制器,這似乎不是一個好主意,因爲我也許必須改變infoView中的某些內容,而不是必須檢查其他任何ViewController ..我會將動畫放在腦海中,並且在基本功能正常工作後立即添加它! – Nareille 2011-05-22 12:57:17