无法在 UINavigationController 上显示 UILabel 的文本

发布于 2024-11-28 22:30:20 字数 1223 浏览 1 评论 0原文

我无法将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

难得心□动 2024-12-05 22:30:20

alertView:clickedButtonAtIndex: 方法中,您将创建一个新的 FirstViewController 对象,该对象与(大概)导航堆栈上的对象无关。
您必须在视图控制器上调用 updateCurrentLocationLabel 方法,该视图控制器实际上是视图层次结构的一部分。仅仅因为您实例化同一个类并不意味着您将获得相同的对象。

In the alertView:clickedButtonAtIndex: method, you're creating a new FirstViewController 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.

情话难免假 2024-12-05 22:30:20

您需要首先调用[[NSUserDefaults standardUserDefaults]同步]。您的默认值不会保存。

You need to call [[NSUserDefaults standardUserDefaults] synchronize] first. Your defaults are not saved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文