无法在 UINavigationController 上显示 UILabel 的文本
我无法将 NSString 显示到 UILabel 中。下面是显示的程序的一部分,当按下按钮时,将调用这些函数。
这是当按钮被按下时 UIAlert 将调用的函数。它位于导航控制器的第二个视图中。 “位置”是一个 NSString。
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0)
{
NSString *newString = [[NSString alloc] initWithString:location];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:newString forKey:@"keyForLocation"];
FirstViewController *newObject = [[FirstViewController alloc] init];
[newObject updateCurrentLocationLabel:nil];
}
else {
}
这
是在基本视图的视图控制器的头文件和实现文件中。这是与 UILabel 一起的。
头文件
@interface FirstViewController : UIViewController {
IBOutlet UILabel *currentLocationLabel;
}
@property (nonatomic, retain) UILabel *currentLocationLabel;
-(IBAction) updateCurrentLocationLabel:(id) sender;
实现文件
- (IBAction) updateCurrentLocationLabel:(id) sender {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *myString1 = [prefs stringForKey:@"keyForLocation"];
currentLocationLabel.text = [NSString stringWithFormat:@"%@", myString1];
}
I am unable to display a NSString into the UILabel. Below is part of the program shown, when the button is hit, those functions will be called.
This is the function that will be called by the UIAlert when the button is hit. It is in the Second View of a Navigation Controller. "location" is a NSString.
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0)
{
NSString *newString = [[NSString alloc] initWithString:location];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:newString forKey:@"keyForLocation"];
FirstViewController *newObject = [[FirstViewController alloc] init];
[newObject updateCurrentLocationLabel:nil];
}
else {
}
}
This is in the header and implementation file of the View Controller of the Base View. Which is with the UILabel.
Header file
@interface FirstViewController : UIViewController {
IBOutlet UILabel *currentLocationLabel;
}
@property (nonatomic, retain) UILabel *currentLocationLabel;
-(IBAction) updateCurrentLocationLabel:(id) sender;
Implementation file
- (IBAction) updateCurrentLocationLabel:(id) sender {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *myString1 = [prefs stringForKey:@"keyForLocation"];
currentLocationLabel.text = [NSString stringWithFormat:@"%@", myString1];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
alertView:clickedButtonAtIndex:
方法中,您将创建一个新的FirstViewController
对象,该对象与(大概)导航堆栈上的对象无关。您必须在视图控制器上调用 updateCurrentLocationLabel 方法,该视图控制器实际上是视图层次结构的一部分。仅仅因为您实例化同一个类并不意味着您将获得相同的对象。
In the
alertView:clickedButtonAtIndex:
method, you're creating a newFirstViewController
object that has nothing to do with the one that is (presumably) on your navigation stack.You have to call the
updateCurrentLocationLabel
method on a view controller that is actually part of your view hierarchy. Just because you instantiate the same class doesn't mean you'll get the same object.您需要首先调用
[[NSUserDefaults standardUserDefaults]同步]
。您的默认值不会保存。You need to call
[[NSUserDefaults standardUserDefaults] synchronize]
first. Your defaults are not saved.