如何将字符串值从一个视图控制器传递到另一个视图控制器
嗨,我是新的目标c。我有一个登录视图控制器与.h,.m,.xib文件。 和成功登录后,我需要转到第二页。如何将字符串值从一个视图控制器传递到另一个视图控制器
这种情况是,我正在访问Web服务。为了验证用户,我将用户名和密码发送到Web服务,并作为回报,我得到一个字符串值。 基于字符串值的结果,我需要显示第二个屏幕。 请帮忙
您可以创建一个类来存储一些共享数据,并在您的应用程序委托中创建它的一个实例。通过这种方式,您可以在任何应用程序类中使用此共享数据。
你能否给我提供示例代码.. – shreedevi 2009-11-06 12:06:23
如果您只需要传递几个值,则可以将它们的参数设置为init方法。例如:
-(id)initWithUserName:(NSString *)name andPassword:(NSString *)password {
self = [super init];
if (nil == self) {
return nil;
}
// display or store login info somewhere
[someLabel setText:name];
return self
}
否则,如果你有很多你想在未来的视图中使用去莫里恩的意见,并进行单独的阶级的价值观。
通过查看一些发布的代码,你可以用额外的NSString参数来扩展你的SecondView的init方法,就像上面的例子。 – ihuk 2009-11-06 08:19:20
我仍然没有得到你.. 我有第一个视图controller.m,它从NSMutable String中的Web服务获取结果值。现在我想在第二个视图中显示 – shreedevi 2009-11-06 12:09:31
我想你的代码中有[[SecondView alloc] init]。如果哟向我描述的init方法中添加另一个参数,则可以使用[[SecondView alloc] initWithSomething:string]将字符串值传递给第二个视图。 – ihuk 2009-11-06 12:58:01
OPNe方法是在应用程序委托中创建属性。每个控制器都可以通过以下方式访问appdelegate:
[[UIApplication sharedApplication] delegate]
这是你的问题;当你从服务器获得响应时,你需要创建一个视图控制器并将该字符串放入其中?
尝试这样的事情在你的第一个视图控制器
// Get the string from the server
NSString *string = [get string from server];
// Create the second controller
SecondViewController *controller = [[SecondViewController alloc] initWithNibName:@"nibname" bundle:nil];
// Set the text property of a label in the controller
controller.myLabel.text=string;
// Add the view to the window so we can see it
[[[[UIApplication sharedApplication] delegate] window] addSubview:controller.view];
这将创建一个新的控制器,设置一个字符串,并在窗口中显示出来。
您将需要包含一个UILabel一个XIB文件,并将其附加到第二控制器名为myLabel欢迎使用属性即
@interface SecondViewController {
UILabel *myLabel;
}
@property (nonatomic, retain) IBOutlet UILabel *myLabel;
@end
希望这有助于
山姆
它不工作... – 2010-09-02 12:42:25
您可能需要提供更多的信息,而不是'它不工作',否则它会帮助你:) – deanWombourne 2010-09-02 14:10:33
它的工作,谢谢 – 2012-07-16 11:26:00
NSString *str1 = @"Apple";
NSString *str2 = @"Orange";
if([str1 isEqualToString: str2]) {
}else{
}
它会帮你
我试着创建一个新的共享类。它为我创建了2个文件shared.h和shared.m现在我将我的字符串值放在shared.h文件中并保存该文件。 shared.h文件有NSMutableString * soapresult; 现在在我的登录视图控制器中看起来像。 @class SecondView #import @inteface LoginView:UIViewController { // username textfield; //密码 //登录按钮 Shared * sharedObject; } 现在在登录view.m文件我尝试使用该字符串作为sharedObject.soapResults。但我得到错误。请帮忙 –
shreedevi
2009-11-06 08:04:08