2009-07-11 6 views
1

私のhello worldアプリケーションでは、ボタンとテキストフィールドが設定されています。テキストフィールドに自分の名前を入力してボタンを押すと、「Hello、[name]!」と表示されます。しかし、私が得るのは "こんにちは、世界!"です。 (テキストボックスに文字列がないときのデフォルト)。名前を入力しても。要求されたファイルは次のとおりです。iPhoneアプリケーションの異常なバグ

// 
// MyViewController.m 
// HelloWorld 
// 
// Created by RCIX on 7/10/09. 
// Copyright 2009 __MyCompanyName__. All rights reserved. 
// 

#import "MyViewController.h" 

@implementation MyViewController 

@synthesize textField; 
@synthesize label; 
@synthesize string; 

/* 
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { 
     // Custom initialization 
    } 
    return self; 
} 
*/ 
/* 
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
} 
*/ 
/* 
// Override to allow orientations other than the default portrait orientation. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 
*/ 
- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 
- (void)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (void)dealloc { 
    [textField release]; 
    [label release]; 
    [string release]; 
    [super dealloc]; 
} 
- (IBAction)changeGreeting:(id)sender { 
    self.string = textField.text; 
    NSString *nameString = self.string; 
    if ([nameString length] == 0) { 
     nameString = @"World"; 
    } 
    NSString *greeting = [[NSString alloc] initWithFormat:@"Hello, %@!", nameString]; 
    label.text = greeting; 
    [greeting release]; 
} 

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField { 
     [theTextField resignFirstResponder]; 
    return YES; 
} 

@end 
+0

ファイルの残りのコードは表示できますか?具体的には、self.string、textField、labelのプロパティ宣言と初期化、およびgetterとsetter(または@synthesized相当のもの)に興味があります。 – Tim

+0

ヘッダーファイルも表示できますか? – Tim

答えて

1

Interface Builderで接続を再確認してください。 textFieldが正しく接続されていないと、メソッドはフィールドの右のtextプロパティを読み取ることができなくなり、長さが0になる可能性があります。

+0

私の接続はすべて正しく接続されているように見えるので、それは問題ではないと思います。 – RCIX

+0

実際には私は接続が見逃しているようです。私は、ファイル所有者のtextFieldプロパティを実際のテキストフィールドにフックアップするとは思わなかった。 – RCIX

1

nameString = string 、ちょうど[self string]に直接作​​業してください!ここで

作業例:

string = textField.text; 
if ([string length] == 0) { 
    string = @"World"; 
} 

NSString *greeting = [NSString stringWithFormat:@"Hello, %@!", string]; 
[label setStringValue:greeting]; 

あなたは、私はstringWithFormatにinitWithFormatを変更し表示されます。 stringWithFormatを使用すると、NSStringオブジェクトは自動解放されるため、解放することを心配する必要はありません。

最後に一つ...私は本当にそれが単なる一時的に使用される「文字列」を有するための必要性は、インスタンス変数も表示されていない...ちょうど

NSString *string = textField.text; 
を行く方が良いかもしれません

それは@interfaceで宣言していない...単なる考え。

+0

これはコードを改善するすべての方法ですが、基本的には何も変わらないと思いますので、あなたの質問には本当に答えません。 コード自体は機能しますが、問題の内容はわかりません。 – micmoo

0

Interface Builderでテキストフィールドが正しくフックアップされていますか?

+0

うん、それは(15文字) – RCIX

関連する問題