2010-12-20 76 views
0

我是新開發的iphone,在閱讀了很多內容之後,我仍然試圖弄清楚UIViews如何正常運行。我一直在玩它,我這是我到目前爲止:Objective-c新手 - 需要UIViews的幫助

我已經創建了一個新的Xcode項目使用基於視圖的應用程序。我有我的MMAppViewController類,我創建了一個名爲「Level1View」的新的UIViewController子類。

有一個標題爲「Level 1」的按鈕,將我帶到「Level1View」viewController。在這個viewController中,有一個「下一個」按鈕,一個「主菜單」按鈕(返回到MMAppViewController),並且有一個標籤,當前標題爲「級別1」。

我的問題是我用來更改標籤標題的代碼不起作用!有人知道爲什麼嗎?這裏是我的代碼:

@class MMAppViewController; 

@interface MMAppAppDelegate : NSObject <UIApplicationDelegate> { 
    UIWindow *window; 
    MMAppViewController *viewController; 
} 

@property (nonatomic, retain) IBOutlet UIWindow *window; 
@property (nonatomic, retain) IBOutlet MMAppViewController *viewController; 

@end 

@implementation MMAppViewController 

-(IBAction)pushLevel1{ 

    Level1View *level1View = [[Level1View alloc] initWithNibName:nil bundle:nil]; 
    [self presentModalViewController:level1View animated:YES]; 
} 
... 

#import <UIKit/UIKit.h> 


@interface Level1View : UIViewController { 
    IBOutlet UILabel *labelTitle; 

} 
-(IBAction)pushBack; 
-(IBAction)pushNext; 
@end 

#import "Level1View.h" 
    #import "MMAppViewController.h" 


    @implementation Level1View 

    -(IBAction)pushBack{ 

     MMAppViewController *MainView = [[MMAppViewController alloc] initWithNibName:nil bundle:nil]; 
     [self presentModalViewController:MainView animated:YES]; 

    } 
    -(IBAction)pushNext{ 

     [labelTitle setText:(@"Thanks for playing :)")]; 

    } 

    - (void)didReceiveMemoryWarning { 
    ... 

目前的應用程序運行,但標籤不會改變時,我打了「下一個「按鈕。誰能幫忙?

回答

0

您是否將界面生成器中的標籤綁定到了Level1View的labelTitle插座?

如果您忘記了這一步,網點將無法使用。即使過了幾年,我仍然有時候會忘記這一步。

--Mike

+0

是的,我沒有連接Interface Builder中的標籤,這就是爲什麼它沒有工作!愚蠢的錯誤。謝謝!當問題在這裏得到解答時,是否有辦法將其標記爲回答?我猜你只是在最合適的答案上點擊勾號? – 2010-12-20 21:51:04

1

你確定UINavigationController不是你想要做的工作更好的工具嗎?這將使您輕鬆管理一堆UIView對象。

這就是說,你是否嘗試過添加日誌記錄以確保你的pushNext方法被調用? labelTitle在哪裏申報?你有沒有使用XIB?

+0

我正在創建一個2級測驗遊戲。主菜單將讓用戶選擇1級或2級。我嘗試過使用navigationController進行實驗,但感到困惑和困惑。這似乎對我有用,所以在此期間將堅持下去。 什麼是日誌記錄,是在頭文件中聲明UIButtons? labelTitle在Level1View頭文件中聲明。我使用界面生成器來添加按鈕/標籤。 – 2010-12-20 18:50:57

+0

如果有幫助,我在Level1View頭文件中添加。 這段代碼似乎工作時,我將它添加到「nextButton」代碼,它更改背景: self.view.backgroundColor = [UIColor redColor]; – 2010-12-20 18:54:03

+0

似乎我忘了在Interface Builder中連接標籤,這就是爲什麼它沒有工作! – 2010-12-20 21:49:06

0

你確定你連接了IB的標籤嗎?

,如果你在Level1View.h設置屬性「@property (nonatomic, retain) IBOutlet UILabel *labelTitle;」你可以從主視圖訪問:

Level1View *level1View = [[Level1View alloc] initWithNibName:nil bundle:nil]; 
[self presentModalViewController:level1View animated:YES]; 
level1View.labelTitle.text = @"something"; 
[level1View release]; 

你不應該再出現在主視圖控制器的另一件事情,而不是解聘Level1View有:

@implementation Level1View 
    -(IBAction)pushBack{ 
     [self dismissModalViewControllerAnimated:YES]; 
    } 

// ,也許問題是[[Level1View alloc] initWithNibName:nil bundle:nil]必須指定要加載如筆尖[[Level1View alloc] initWithNibName:@"Level1View" bundle:nil]

+0

似乎我忘了在Interface Builder中連接標籤,這就是爲什麼它不起作用!愚蠢的錯誤。謝謝! – 2010-12-20 21:49:29

+0

不要忘記[self dismissModalViewControllerAnimated:YES];意見 – 2010-12-20 22:01:30

+0

我補充說,作爲我的pushBack方法中的最後一行。林不知道爲什麼這是必要的,這是否與內存分配?再一次,即時通訊新的Objective-C和iPhone的發展,所以仍然在學習。 – 2010-12-21 01:04:39

0

聲明labelTitle在你的頭文件的屬性,並在您的m合成labelTitle - 只要labelTitle通過界面生成器迷上了你的代碼的其餘部分是好的。

.h 
@property (nonatomic, retain) IBOutlet UILabel *labelTitle; 

.m 
@synthesize labelTitle; 

然後你的setter調用將工作。 (也點標記適用於合成的性質,所以你不妨用它)

change 
[labelTitle setText:(@"Thanks for playing :)")]; 
to 
labelTitle.text = @"Thanks for playing :)"; 

合成屬性將在運行時創建setter和getter方法。閱讀:The Objective-C Programming Language

+0

似乎我忘了在Interface Builder中連接標籤,這就是爲什麼它不起作用!愚蠢的錯誤。謝謝! – 2010-12-20 21:50:06