iOS:字符串值在从一个 class 传递到另一个时显示为 null?
iOS: String value displaying as null while passing from one class to another?
我已经从一个 class 获取了一个 NSString 到另一个
我有一个测验视图控制器,用户在 UITextView 中输入问题,然后按下一步转到 select 朋友视图控制器,在那里他们 select 用户然后发送关于 parse.com
的问题
quiz.h
@property (nonatomic,strong) IBOutlet UITextView *textField;
@property (nonatomic, strong) NSString *text;
quiz.m
- (IBAction)next:(id)sender {
NSString *text = self.textField.text;
if ([text length] == 0) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error!"
message:@"Enter some text"
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
} else {
selectFriendsViewController *sfvc = [[selectFriendsViewController alloc] init];
sfvc.string = text;
}
}
selectfriendsviewcontroller.h
@property (nonatomic, retain) NSString *string;
selectfriendsviewcontroller.m
@synthezise string;
- (void)viewDidLoad {
[super viewDidLoad];
quizViewController *qvc = [[quizViewController alloc] init];
qvc.text = string;
UITextView *textfield = [[UITextView alloc] init];
string = textfield.text;
}
为什么字符串显示为null??在 quiz.m 中,当我按下下一步时,字符串传递为 null,关于如何修复的任何想法?
在下一个按钮操作下的 quizViewController 中
- (IBAction)next:(id)sender {
if ([text length] == 0) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error!"
message:@"Enter some text"
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
} else {
selectFriendsViewController *sfvc = [[selectFriendsViewController alloc] init];
sfvc.string = self.textField.text;
}
}
然后在selectFriendsViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"string %@",self.string);
}
还要检查 textView 的出口是否连接到 UITextView
。如果没有,则连接它。然后 selectFriendsViewController.h ,创建 属性 @property (nonatomic, strong) NSString *string;
希望对你有帮助。
selectfriendsviewcontroller.m
您可以从 string
属性 访问字符串的值(顺便避免像 string
这样的通用名称)如果您使用 Xcode
> 4.4,您也可以跳过 @synthezise
在selectfriendsviewcontroller.m
-(void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"%@",self.string);
}
我已经从一个 class 获取了一个 NSString 到另一个
我有一个测验视图控制器,用户在 UITextView 中输入问题,然后按下一步转到 select 朋友视图控制器,在那里他们 select 用户然后发送关于 parse.com
的问题quiz.h
@property (nonatomic,strong) IBOutlet UITextView *textField;
@property (nonatomic, strong) NSString *text;
quiz.m
- (IBAction)next:(id)sender {
NSString *text = self.textField.text;
if ([text length] == 0) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error!"
message:@"Enter some text"
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
} else {
selectFriendsViewController *sfvc = [[selectFriendsViewController alloc] init];
sfvc.string = text;
}
}
selectfriendsviewcontroller.h
@property (nonatomic, retain) NSString *string;
selectfriendsviewcontroller.m
@synthezise string;
- (void)viewDidLoad {
[super viewDidLoad];
quizViewController *qvc = [[quizViewController alloc] init];
qvc.text = string;
UITextView *textfield = [[UITextView alloc] init];
string = textfield.text;
}
为什么字符串显示为null??在 quiz.m 中,当我按下下一步时,字符串传递为 null,关于如何修复的任何想法?
在下一个按钮操作下的 quizViewController 中
- (IBAction)next:(id)sender {
if ([text length] == 0) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error!"
message:@"Enter some text"
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
} else {
selectFriendsViewController *sfvc = [[selectFriendsViewController alloc] init];
sfvc.string = self.textField.text;
}
}
然后在selectFriendsViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"string %@",self.string);
}
还要检查 textView 的出口是否连接到 UITextView
。如果没有,则连接它。然后 selectFriendsViewController.h ,创建 属性 @property (nonatomic, strong) NSString *string;
希望对你有帮助。
selectfriendsviewcontroller.m
您可以从 string
属性 访问字符串的值(顺便避免像 string
这样的通用名称)如果您使用 Xcode
> 4.4,您也可以跳过 @synthezise
在selectfriendsviewcontroller.m
-(void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"%@",self.string);
}