NSString始终传递空iOS
我有一个quick.vc,我将一个NSString从一个vc传递到另一个,它传递null。(我正在使用UITextView) 它在运行时总是记录为空?我已经尝试了很多方法! 关于如何传递字符串而不为null,有什么想法吗 谢谢你的错误NSString始终传递空iOS,ios,objective-c,null,nsstring,viewcontroller,Ios,Objective C,Null,Nsstring,Viewcontroller,我有一个quick.vc,我将一个NSString从一个vc传递到另一个,它传递null。(我正在使用UITextView) 它在运行时总是记录为空?我已经尝试了很多方法! 关于如何传递字符串而不为null,有什么想法吗 谢谢你的错误 selectFriendsViewController *sfvc = [[selectFriendsViewController alloc] init]; sfvc.string = self.userText; 这将创建一个新的selectFrie
selectFriendsViewController *sfvc = [[selectFriendsViewController alloc] init];
sfvc.string = self.userText;
这将创建一个新的selectFriendsViewController
实例,但您不使用它。该方法完成后,它将被解除分配。因此,您什么都没有得到
如果在iAction
中触发一个序列,请使用prepareforsgue
传递数据
编辑,
如果你在点击按钮时发射了一个segue
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.destinationViewController isKindOfClass:[selectFriendsViewController class]]) {
selectFriendsViewController * dvc = (selectFriendsViewController*)segue.destinationViewController;
dvc.string = self.textField.text;
}
}
我认为您无法导航并将值推送到下一个viewController 如果使用xib文件,请使用此代码
- (IBAction)next:(id)sender {
selectFriendsViewController *sfvc = [[selectFriendsViewController alloc] init];
sfvc.string = self.textField.text;
[self.navigationController pushViewController:sfvc animated:YES];
}
希望该代码对您有所帮助。如果您不想显示null,请使用下面的代码
#pragma mark - check string is empty or not
- (IBAction)next:(id)sender
{
self.userText = self.textField.text;
selectFriendsViewController *sfvc = [[selectFriendsViewController alloc] init];
sfvc.string = [self checkEmpty:self.userText];
}
- (void)checkEmpty:(NSString *)check
{
@try
{
if (check.length==0)
check = @" ";
if([check isEqual:[NSNull null]])
check = @" ";
}
@catch (NSException *exception)
{
check = @" ";
}
}
你“连接”了吗InterfaceBuilder中的textField outlet?是的,我看到了我对ios开发不熟悉的连接可能的副本,你能帮我一下吗?你在方法
next
中启动了一个segue吗?我以前没有使用seque方法发送数据谢谢leo,当我按下next时,我如何将此方法放入iAction以传递它按钮“在其他情况下”是什么意思?
#pragma mark - check string is empty or not
- (IBAction)next:(id)sender
{
self.userText = self.textField.text;
selectFriendsViewController *sfvc = [[selectFriendsViewController alloc] init];
sfvc.string = [self checkEmpty:self.userText];
}
- (void)checkEmpty:(NSString *)check
{
@try
{
if (check.length==0)
check = @" ";
if([check isEqual:[NSNull null]])
check = @" ";
}
@catch (NSException *exception)
{
check = @" ";
}
}