2010-12-20 78 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