如何在 xcode 中没有 xib 的情况下将字符串值从一个视图控制器传递到另一个视图控制器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15447608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i pass string value from one viewcontroller to another without xib in xcode?
提问by Naveen
How can i pass one string variable from my firstviewcontroller class to secondviewcontroller class? here secondviewcontroller class have only ".m" and ".h" files; currently in secondviewcontroller ".h" file
如何将一个字符串变量从我的 firstviewcontroller 类传递给 secondviewcontroller 类?这里 secondviewcontroller 类只有“.m”和“.h”文件;当前在 secondviewcontroller ".h" 文件中
@property(nonatomic,retain) NSString *value2;
in secondviewcontroller.m
file i am using
在secondviewcontroller.m
我正在使用的文件中
NSString *str=value2;
NSLog(@"%@",str);
// it prints a null
in my firstviewcontroller.m
file am assigning like
在我的firstviewcontroller.m
文件中分配像
secondviewcontroller *second=[[secondviewcontroller alloc]init];
second.value2=value1;
value1 is declared in firstviewcontroller as string ..please tell me one solution
value1 在 firstviewcontroller 中声明为字符串 ..请告诉我一个解决方案
回答by βhargav?
Try to assign your value1 like.
尝试分配您的 value1 喜欢。
firstviewcontroller.m
firstviewcontroller.m
NSString *value1 = [[NSString alloc] initWithString:@"my value1"];
and then assign vc.value2 = value1;
然后分配 vc.value2 = value1;
回答by iPatel
Generally logic is :
一般逻辑是:
NOTE :Data type of both value1and value2mustbe same.
注意:value1和value2 的数据类型必须相同。
First put in secondVC.h
file
首先放入secondVC.h
文件
NSString *value2;
give its @property
and @synthesize
properly.
给予其@property
和@synthesize
正常。
In your firstVC.m
在你的 firstVC.m
NSString *value1 = @"this is string"; // in your case check it is, proper or not (i mean nil/not nill)??
and at create Object of secondVC
并在创建对象 secondVC
secondVC *vc = [[secondVC alloc] init];
vc.value2 = value1;
[self presentModalViewController:vc animated:YES];
and write in secondVC.m
file
并写入secondVC.m
文件
NSLog(@"%@",value2);
and check out put in console.
并检查 put in console。
回答by Murtuza Kabul
Most probable reason appears here is you have not synthesized the property in second view controller's .m file. Unless you synthesize the property it will not initiate and will be nil. Please post the entire code.
这里出现的最可能的原因是您没有在第二个视图控制器的 .m 文件中合成该属性。除非您合成该属性,否则它不会启动并且为零。请贴出完整的代码。
You need to declare a variable for the property and synthesize the property so that it assigns the value to the variable.
您需要为该属性声明一个变量并合成该属性,以便将值分配给该变量。
Let me know if it is not so.
如果不是这样,请告诉我。
回答by Yashesh
synthesized your propery and used it with self. value2.
合成你的财产并将其用于自我。值2。
Hope this will solve your problem.
希望这能解决您的问题。
All the best !!!
祝一切顺利 !!!
回答by meth
ok this is my First View Controller it has xib file
好的,这是我的第一个视图控制器,它有 xib 文件
.h
。H
#import "SecondViewController.h"
@interface ViewController : UIViewController
@property(strong, nonatomic) SecondViewController * svc;
@end
and second with no xib.
第二个没有xib。
.h
。H
#import <UIKit/UIKit.h>
@class ViewController;
@interface SecondViewController : UIViewController
@property (strong, nonatomic) ViewController * vc;
@property (strong, nonatomic) NSMutableString * passValue;
@end
in implementation
执行中
first .m is viewDidLoad
第一个 .m 是 viewDidLoad
- (void)viewDidLoad
{
NSLog(@"viewDidLoad");
[super viewDidLoad];
svc=[[SecondViewController alloc] init];
svc.vc=self;
NSString * str= svc.passValue;
NSLog(@"%@",str);
svc.passValue =[@"StringFromFirstView" mutableCopy];
[svc viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
and in Second .m
并在第二个.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
passValue = @"mySValue";
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"passValue %@",passValue);
// Do any additional setup after loading the view.
}
and when i Run the code
当我运行代码时
2013-03-16 13:11:40.178 ali[787:c07] viewDidLoad
2013-03-16 13:11:40.179 ali[787:c07] mySValue
2013-03-16 13:11:40.180 ali[787:c07] passValue StringFromFirstView