2012-08-15 45 views
4

我目前正在爲我的第一個iPhone遊戲設計結構並遇到問題。目前,我有一個'MenuViewController',它允許你選擇要玩的級別和一個LevelViewController。解除模態Segue後更新UIViewController

「MenuViewController」上的觸發了一個模式的Segue到'LevelViewController'。

在「LevelViewController」 A UIButton觸發以下方法返回到「MenuViewController」:

-(IBAction)back:(id)sender //complete 
{ 
    [self dismissModalViewControllerAnimated:YES]; 
} 

的問題是,我有一個UILabel,打印總點的數量的菜單頁玩家有。每當我回到層面的菜單時,我都希望這個標籤自動更新。目前,該標籤在「MenuViewController」編程定義:

-(void)viewDidLoad { 
    [super viewDidLoad]; 
    CGRect pointsFrame = CGRectMake(100,45,120,20); 
    UILabel *pointsLabel = [[UILabel alloc] initWithFrame:pointsFrame]; 
    [pointsLabel setText:[NSString stringWithFormat:@"Points: %i", self.playerPoints]]; 
    [self.pointsLabel setTag:-100]; //pointsLabel tag is -100 for id purposes 
} 

self.playerPoints是MenuViewController

的整數屬性有沒有一種方法,我可以更新標籤?提前致謝!

回答

0

使屬性self.pointsLabel指向的UILabel,那麼你可以調用像[self.pointsLabel setText:[NSString stringWithFormat:@"Points: %i", self.playerPoints]];更新使用新的標籤得分

+0

聽起來像它會工作。但我在哪裏打電話?現在,如果我將NSLog(@「菜單加載」)放入我的菜單視圖控制器的viewDidLoad中,它不會在調用dismissModalViewController之後從級別返回視圖控制器之後重新記錄「菜單加載」,這意味着viewDidLoad方法根本沒有被調用。如果我在viewDidAppear或viewWillAppear中調用它,它會起作用嗎?謝謝。 – 2012-08-15 20:59:04

+0

首先,你確定你正在從你的LevelViewController更新MenuViewController的pointsValue屬性嗎?你是否像LevelViewController上的'delegate'屬性指向MenuViewController? – Ariel 2012-08-15 21:05:23

+0

我不認爲我有LevelViewController上的委託屬性。這是創建一個的方式嗎? '@property(nonatomic,assign)id delegate;' – 2012-08-15 21:12:10

8

這是代表團完美的情況。當LevelViewController完成時,它需要觸發一個在MenuViewController中處理的委託方法。這種委託方法應該解除模態VC,然後做任何你需要它做的事情。提交風險投資者通常應該處理對其提出的模態觀點的解僱。

這裏是如何實現這一個基本的例子:

LevelViewController.h(以上接口聲明):

@protocol LevelViewControllerDelegate 
    -(void)finishedDoingMyThing:(NSString *)labelString; 
@end 

相同的文件伊娃段內:

__unsafe_unretained id <LevelViewControllerDelegate> _delegate; 

同檔案下面伊娃部分:

@property (nonatomic, assign) id <LevelViewControllerDelegate> delegate; 

在LevelViewController.m文件:

@synthesize delegate = _delegate; 
在MenuViewController.h

現在,#import "LevelViewController.h",並宣佈自己作爲LevelViewControllerDelegate委託:

@interface MenuViewController : UIViewController <LevelViewControllerDelegate> 

現在裏面MenuViewController。米實現委託方法:

-(void)finishedDoingMyThing:(NSString *)labelString { 
    [self dismissModalViewControllerAnimated:YES]; 
    self.pointsLabel.text = labelString; 
} 

然後呈現模式的VC之前,請務必將自己設置爲代表的LevelViewController:

lvc.delegate = self; // Or whatever you have called your instance of LevelViewController 

最後,當你做什麼,你需要在LevelViewController裏面調用這個:

[_delegate finishedDoingMyThing:@"MyStringToPassBack"]; 

如果這沒有意義,我可以試着幫助你理解。

+0

你能否給我一個寫一個委託方法的開端?我有一個名爲AppDelegate.m的類,AppDelegate.h被導入到MenuViewController和LevelViewController中。而且這個方法會在MenuViewController文件內被調用,因爲這是提交VC?謝謝。 – 2012-08-15 21:10:16

+0

與你將要做的事沒有關係。 AppDelegate就是這樣一個處理應用程序委託方法的類。你需要做的是爲特定的協議聲明一個委託。我上面提供的例子是你需要的(你可能需要稍微改變它來匹配你的實現 – 2012-08-15 21:27:13

+1

它的工作原理!非常感謝!:) – 2012-08-16 00:18:59

0

在你的模式視圖的頭文件,添加屬性:

@property (nonatomic,assign) BOOL updated; 

然後在您的主視圖控制器,使用didViewAppear的東西,如:

-(void)viewDidAppear:(BOOL)animated{ 
    if (modalView.updated == YES) { 
     // Do stuff 
     modalView.updated = NO; 
    } 
} 

其中「modalView」是的名稱那你可能在那裏分配/ init的UIViewController。

如果您想要傳遞更多信息(例如用戶選擇的級別),請添加更多屬性。

相關問題