標籤:
1 // 2 // ViewController.m 3 // HelloWorld 4 // 5 // 6 // 7 #import "ViewController.h" 8 @interface ViewController () 9 @end10 @implementation ViewController11 @synthesize userName=_userName;12 @synthesize textField=_textField;13 @synthesize label=_label;14 - (void)viewDidLoad {15 //[self setTextField:nil];一初始化為空白,注釋掉後,label可以正常擷取使用者輸入的值。16 //[self setLabel:nil];文檔45頁說明加上了這些可能是為了說有什麼作用,記得刪除,不然會影響後面的label輸出顯示內容。17 //[super viewDidLoad];18 // Do any additional setup after loading the view, typically from a nib.19 }20 - (void)didReceiveMemoryWarning {21 [super didReceiveMemoryWarning];22 // Dispose of any resources that can be recreated.23 }24 - (IBAction)changeGreeting:(id)sender {25 self.userName = self.textField.text;26 NSString *nameString = self.userName;27 NSLog(@"%@",nameString);28 if ([nameString length] == 0) {29 nameString = @"World";30 NSLog(@"111");31 }32 NSLog(@"222");33 NSString *greeting = [[NSString alloc]initWithFormat:@"Hello,%@!",nameString];34 NSLog(@"%@",greeting);35 self.label.text = greeting;36 NSLog(@"333");37 }38 - (BOOL)textFieldShouldReturn:(UITextField *)theTextField{39 if (theTextField == self.textField) {40 [theTextField resignFirstResponder];41 }42 return YES;43 44 }45 @end
15、16行是Your First ios App 第45頁上面加上去的,用的有道翻譯軟體也沒有提示說,這兩句會影響最終案例的效果顯示。代碼中加了很多nslog輸出,是為了做測試,這個方法屢試不爽。第二張中右下角有代碼輸出提示。在27行加上nslog後,輸出竟然是(null),所以判斷,使用者輸入的內容沒有被擷取。這下好判斷問題出現在哪了,因為對Objective文法不熟悉,先注釋了,一運行,結果成了,這兩句語句我得仔細查查。
Your First iOS App--蘋果官方iOS文檔學習