0

I am using Xcode 6 and doing a small project on iOS 8, and I need to render some text onto the View.

My method is to create a UITextField on a UIView and as long as people type onit, the app redraw the View:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.renderTextView.textToRender = @"Please type something…";
    UITextField* textField = [[UITextField alloc] initWithFrame:self.renderTextView.frame];
    textField.opaque = NO;
    [self.renderTextView addSubview:textField];
    [textField addTarget:self action:@selector(updateLabelAndRefresh:) forControlEvents:UIControlEventEditingChanged];  
}

- (void) updateLabelAndRefresh: (id)sender{
    self.renderTextView.textToRender = @"Hello World";

    // if text's length > 0 …
    if (self.textField.text.length > 0) {
        self.renderTextView.textToRender = self.textField.text;
    }

    [self.renderTextView setNeedsDisplay];

}

But the problem is: no matter how I try, I can not get the actual text I type on my phone. and the console showed me that the text is null. I kept googling it but I stil can not work it out.

Do you guys have any solution? Thank you ;)

Tony Chol
  • 3
  • 1
  • 4
  • Well for one, you've locally defined the UITextField in viewDidLoad, so you shouldn't be able to get info from it in a different function. – Albert Renshaw Sep 16 '14 at 07:07
  • In your .h file put `UITextField *textField;` then in the .m file in viewDidLoad omit the `UITextField *` part just leaving `textField = ...` – Albert Renshaw Sep 16 '14 at 07:08
  • @AlbertRenshaw Yes you are right! Thanks and it's one of the most stupid mistakes for me now… – Tony Chol Sep 16 '14 at 09:13

1 Answers1

0

In below some error is there, give a look.

- (void)viewDidLoad {
 [super viewDidLoad];

 //Here textField is local variable
 UITextField* textField = [[UITextField alloc] initWithFrame:self.renderTextView.frame];
 textField.opaque = NO;


}

- (void) updateLabelAndRefresh: (id)sender{
  self.renderTextView.textToRender = @"Hello World";

   // if text's length > 0 …
  if (self.textField.text.length > 0) {   //Then how come u getting self.textField here and using it as global one.
   //So you won't get access to that locally defined textField in viewDidLoad method.
     self.renderTextView.textToRender = self.textField.text;
 }
}

Please check your code and update me about it.

nikhil84
  • 3,235
  • 4
  • 22
  • 43