如何发送一个NSString到另一个视图控制器
问题描述:
我有一个IBAction,当触发时调用另一个视图控制器(APICallsViewController)中的另一个方法。我正在寻找也发送该方法一个NSString(消息)如何发送一个NSString到另一个视图控制器
这里是我的IBAction推到APICallsViewController和NSString消息。我的问题可能是如何在其他视图控制器的方法中获取该NSString的内容。
感谢您的帮助
-(IBAction) someMethod{
APICallsViewController *apiViewController = [[APICallsViewController alloc] init];
[self.navigationController pushViewController:apiViewController animated:YES];
NSString *message = [NSString stringWithFormat:@"Check out %@", nameLb.text];
[apiViewController apiGraphUserCheckins];
[apiViewController release];
}
答
做到这一点的代码APICallsViewController.h
@interface APICallsViewController : UIViewController{
NSString *strMessage;
}
@property(nonatomic,retain) NSString *strMessage;
@end
APICallsViewController.m
@synthesize strMessage;
- (void)viewDidLoad {
Nslog(@"%@",strMessage);
}
-(IBAction) someMethod{
APICallsViewController *apiViewController = [[APICallsViewController alloc] init];
[self.navigationController pushViewController:apiViewController animated:YES];
NSString *message = [NSString stringWithFormat:@"Check out %@", nameLb.text];
apiViewController.strMessage=message;
[apiViewController apiGraphUserCheckins];
[apiViewController release];
}
答
声明中的ViewController到您有一个字符串传递string.And从中你必须通过字符串,你的情况来看,设置像 apiViewController.stringintheotherview =消息;
在APICallsViewController字符串必须合成
NSString *message = [NSString stringWithFormat:@"Check out %@", nameLb.text];
APICallsViewController *apiViewController = [[APICallsViewController alloc] init];
apiViewController.stringintheotherview=message;
[self.navigationController pushViewController:apiViewController animated:YES];
答
为什么要以编程方式传递字符串,而不是声明函数参数?您可以将功能更改为类似
- (void) apiGraphUserCheckins:(NSString *)message;
调用它
[apiViewController apiGraphUserCheckins:[NSString stringWithFormat:@"Check out %@", nameLb.text]];
答
声明中SecondViewController的属性。然后,您可以在2ways中的SecondViewController中获取字符串。
-
在FirstViewController的someMethod为secondviewController创建对象之后可以分配的值直接
second.string2 = [NSString的stringWithFormat:@ “%@”,[文本框的文本]];
在SecondViewController中创建一个方法并通过它进行分配。