如何将 NSString 值导入到不同的视图控制器中?

发布于 2024-12-08 17:14:31 字数 2199 浏览 1 评论 0原文

我有一个应用程序,我在一个视图的 UITextField 中设置邮政编码值。当我切换视图时,我希望该值显示为 UILabel。我知道如何在一个视图控制器内执行此操作,但不知道如何在两个视图控制器之间执行此操作。我尝试将“邮政编码值”设置为名为“globals.h”的头文件中的常量字符串,然后调用它,但它总是崩溃。以下是一些代码示例:

这是 globals.h 文件:

#import <Foundation/Foundation.h>

@interface globals 

extern NSString *zipCodeValue;

@end

这是 globals.m 文件:

#import "globals.h"

@implementation globals

NSString *zipCodeValue = @"default value";

@end

这是第一个视图(在文本字段中输入邮政编码。接口和实现):

#import <UIKit/UIKit.h>


@interface ZipCodeEntryViewController : UIViewController <UITextFieldDelegate> {
    UITextField *zipCode;
    UIButton *doneButton;
}
@property (nonatomic, retain) IBOutlet UITextField *zipCode;
@property (nonatomic, retain) IBOutlet UIButton *doneButton;

-(IBAction) tapBackground:(id)sender;
-(IBAction) doneButtonClick:(id)doneButton;

@end

#import "ZipCodeEntryViewController.h"
#import "MillersAppAppDelegate.h"
#import "AddressViewController.h"
#import "globals.h"
#define MAX_LENGTH 5

@implementation ZipCodeEntryViewController
@synthesize zipCode, doneButton;

-(IBAction) doneButtonClick:(id)doneButton{
    MillersAppAppDelegate *delegate = (MillersAppAppDelegate *)[[UIApplication     sharedApplication] delegate];
    AddressViewController *addressView = [[AddressViewController     alloc]initWithNibName:@"AddressViewController" bundle:nil];
    [delegate switchViews:self.view toView:addressView.view];
    //using the global string to store the text field content
    zipCodeValue=zipCode.text;
    //the button is clicked and the view changes to addressView while NSLogging the correct value. So far, so good.
    NSLog(@"%@", zipCodeValue); 
    [addressView release];
}

-(IBAction) tapBackground:(id)sender{
    [zipCode resignFirstResponder];
}

所以,一切都正常此时(它正确地记录下来)。我假设常量变量“zipCodeValue”现在将继续保留其设置值,即使视图控制器已更改。但是当我尝试将它应用到新的视图控制器(AddressViewController)中时,它对标签没有任何作用。这是我使用的方法(我在头文件中命名它;'zipCodeEntry'是我命名的UILabel,在Interface Builder中初始化并连接到目标):

-(void)setZipCode{
zipCodeEntry.text=zipCodeValue;
}

我现在彻底困惑了,我有理由相信我的大脑丢失了因为我对此很陌生。抱歉,如果这很啰嗦,我不想遗漏任何内容。我显然是一个初学者。哎呀!!!

I have an application where I am setting a zip code value in a UITextField on one view. When I switch views, I would like that value to be displayed as a UILabel. I know how to do this within one view controller but not between two. I have attempted to set the 'zip code value' to a constant string in a header file called 'globals.h' and then recall it but it always crashes. Here are some code samples:

This is the globals.h file:

#import <Foundation/Foundation.h>

@interface globals 

extern NSString *zipCodeValue;

@end

This is the globals.m file:

#import "globals.h"

@implementation globals

NSString *zipCodeValue = @"default value";

@end

This is the first view (where the zip code is entered into the text field. Both interface and implementation):

#import <UIKit/UIKit.h>


@interface ZipCodeEntryViewController : UIViewController <UITextFieldDelegate> {
    UITextField *zipCode;
    UIButton *doneButton;
}
@property (nonatomic, retain) IBOutlet UITextField *zipCode;
@property (nonatomic, retain) IBOutlet UIButton *doneButton;

-(IBAction) tapBackground:(id)sender;
-(IBAction) doneButtonClick:(id)doneButton;

@end

#import "ZipCodeEntryViewController.h"
#import "MillersAppAppDelegate.h"
#import "AddressViewController.h"
#import "globals.h"
#define MAX_LENGTH 5

@implementation ZipCodeEntryViewController
@synthesize zipCode, doneButton;

-(IBAction) doneButtonClick:(id)doneButton{
    MillersAppAppDelegate *delegate = (MillersAppAppDelegate *)[[UIApplication     sharedApplication] delegate];
    AddressViewController *addressView = [[AddressViewController     alloc]initWithNibName:@"AddressViewController" bundle:nil];
    [delegate switchViews:self.view toView:addressView.view];
    //using the global string to store the text field content
    zipCodeValue=zipCode.text;
    //the button is clicked and the view changes to addressView while NSLogging the correct value. So far, so good.
    NSLog(@"%@", zipCodeValue); 
    [addressView release];
}

-(IBAction) tapBackground:(id)sender{
    [zipCode resignFirstResponder];
}

So, everything appears ok at this point (it logs it out correctly). I'm assuming the constant variable 'zipCodeValue' will continue to retain it's set value now even though the view controller has changed. BUT when I attempt to apply it in the new view controller (AddressViewController) it does nothing to the label. This is the method I used (I named it in the header file; 'zipCodeEntry' is the UILabel that I named initialized and connected to the target in Interface Builder):

-(void)setZipCode{
zipCodeEntry.text=zipCodeValue;
}

I am now thoroughly confused, I have reason to believe my brain is missing something since I'm new to this. Sorry if this is long-winded, I didn't want to leave anything out. I'm clearly a beginner. AGGGG!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

好听的两个字的网名 2024-12-15 17:14:31

尝试使用 NSUserDefaults:

NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
[ud setObject:zipCodeValue forKey:@"myKey"];
[ud synchronize];

并加载它:

NSString *a = [ud objectForKey:@"myKey"];

您可以在应用程序中的任何位置加载和保存它

Try using NSUserDefaults:

NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
[ud setObject:zipCodeValue forKey:@"myKey"];
[ud synchronize];

And to load it:

NSString *a = [ud objectForKey:@"myKey"];

You can load and save it everywhere in your app

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