Modifying a UITextField declared in Interface Builder
In interface builder I declared a UITextField which has a tag of 22. When I click a button I want to change the text. I've tried this two ways. The first way is.
Main.m
((UITextField*)[HomeController.view viewWithTag:22]).text = [NSString stringWithFormat:@"%d mins",minutes];
HomeController is a UIViewController that I've seen working in other parts of the开发者_如何学C same file. The result of this code is that nothing is updated.
The other way that I have tried to implement this is by adding a IBOutlet for the text field. I have the IBOutlet in Home.h shown below.
@interface Home : UIViewController {
IBOutlet UITextField *safeTime;
}
@property (nonatomic,retain) UITextField *safeTime;
@end
My main view controller has an instance of this class defined in Main.h
In the method to update the text field, I have this code.
self.HomeController.safeTime.text = [NSString stringWithFormat:@"%d mins",minutes];
Using this code I get the same result as the previous method. Nothing is updated on the screen.
If anyone has any suggestions on how to get either of these methods working it would be much appreciated. If additional clarification is needed I can provide. Thanks in advance for the help!
First of all, use:
[self.HomeController.safeTime setText:[NSString stringWithFormat:@"%d mins",minutes]];
If that still didn't fix the problem for you, check your IB if the IBOutlet is connected to the (right) textfield
精彩评论