在视图控制器之间传递数据 - iOS Xcode - 什么是好方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11392473/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:49:24  来源:igfitidea点击:

Passing data between view controllers - iOS Xcode - what's a good way?

iosxcodensstringtextfield

提问by ipatch

Alright, I'll try and make this as simple as possible (I was seeming to get the run around on chat.stackoveflow.com when I tried asking this question). I want to pass the text from a textfield in one viewcontroller to another viewcontroller.

好吧,我会尽量让这件事变得简单(当我尝试问这个问题时,我似乎在 chat.stackoveflow.com 上跑来跑去)。我想将文本从一个视图控制器中的文本字段传递到另一个视图控制器。

Should I use a Modelclass and store the the textfield.text in a Model.[h/m] files and then have the second view controller access the data stored in the model?

我应该使用Model类并将 textfield.text 存储在 Model.[h/m] 文件中,然后让第二个视图控制器访问存储在模型中的数据吗?

Basically this is what I have,

基本上这就是我所拥有的

ViewControllerWelcome.h

ViewControllerWelcome.h

@interface ViewControllerWelcome : UIViewController { }

@property (weak, nonatomic) IBOutlet UITextField *textFieldUsername;

ViewControllerWelcome.m

ViewControllerWelcome.m

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
                ViewControllerHome *home = (ViewControllerHome *)[storyboard instantiateViewControllerWithIdentifier:@"Home"];
                NSString *username = _textFieldUsername.text;
                home.lblUSERNAME.text=username;
                [self presentModalViewController:home animated:YES];

ViewControllerHome.h

视图控制器主页.h

@interface ViewControllerHome : UIViewController {
NSString *username;
UILabel *lblUSERNAME;
}

@property (weak, nonatomic) IBOutlet UILabel *lblUSERNAME;

ViewControllerHome.m

视图控制器主页.m

- (void)changeUSERNAME {

 // get username from welcome tf
ViewControllerWelcome *welcome = [[ViewControllerWelcome alloc] init];
 welcome.username = [self lblUSERNAME.text];
 // _lblUSERNAME.text = welcome._textFieldUsername.text;
 //welcome.textFieldUsername.text = _username;
 // username = welcome.textFieldUsername.text;
 NSLog(@"username = %@",username);
// welcome.textFieldUsername.text = _lblUSERNAME.text;
// NSLog(@"username = %@",welcome.textFieldUsername.text);
}    

As you can see I tried several different things, but couldn't come up with a working solution :-l

如您所见,我尝试了几种不同的方法,但无法提出可行的解决方案:-l

采纳答案by ipatch

I refined this question with another question on SO that can be found hereBasically, I created a singleton to pass the data between the view controllers. I haven't had any problems with it so far (knocks on wood).

我用另一个关于 SO 的问题改进了这个问题,可以在这里找到 基本上,我创建了一个单例来在视图控制器之间传递数据。到目前为止,我还没有遇到任何问题(敲木头)。

回答by carl_h

Since you're using Storyboards, it makes more sense to be using Segues to perform the transition between your ViewControllers. If you're not familiar with segues you can have a look here:

由于您使用的是 Storyboard,因此使用 Segues 在您的 ViewController 之间执行转换更有意义。如果你不熟悉 segues,你可以看看这里:

http://developer.apple.com/library/IOs/#featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomSegues/CreatingCustomSegues.html

http://developer.apple.com/library/IOs/#featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomSegues/CreatingCustomSegues.html

It's very simple to create a segue in IB. Once you have that set up you implement
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)senderand configure your destination view controller. So you'll have something like the following.

在 IB 中创建 segue 非常简单。完成设置后,您就可以实现
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender和配置目标视图控制器。所以你会得到类似下面的东西。

-(void)prepareForSegue(UIStoryboardSegue *)segue sender:(id)sender{
    // Assume you have a viewHomeSegue defined that has the name of the segue you want to perform
    NSString * segueIdentifier = [segue identifier];
    if([segueIdentifier isEqualToString:viewHomeSegue]){
        ViewControllerHome * homeController = (ViewControllerHome *)[segue destinationViewController];
        homeController.lblUSERNAME.text = _textFieldUsername.text;
    }
}

回答by Norbert

You shouldn't keep strong references between those two ViewControllers in both directions. What you can do, for example, is to declare a protocol in your ViewControllerWelcomewith a delegate in it. You could let the ViewControllerHomeinstanciate the other controller and then be its delegate which gets informed as soon as the user finished entering the username.

您不应该在这两个 ViewController 之间在两个方向上保持强引用。例如,您可以做的是在您的协议中声明一个ViewControllerWelcome带有委托的协议。你可以让ViewControllerHome另一个控制器实例化,然后成为它的代表,一旦用户完成输入用户名,它就会得到通知。

@protocol ViewControllerWelcomeDelegate <NSObject>    
- (void) userNameEntered:(NSString *)userName;    
@end

@interface ViewControllerWelcome : UIViewController
@property (nonatomic, weak) id <ViewControllerWelcomeDelegate> delegate;
...
@end

In your @implementation:

在您的@implementation 中:

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField
{
   [theTextField resignFirstResponder];
   [delegate userNameEntered:theTextField.text];

   return YES;
}

回答by tomidelucca

if you have a .storyboard file you can layout your views in there like with interface builder.

如果你有一个 .storyboard 文件,你可以像使用界面构建器一样在那里布局你的视图。

This documents really helped me out the first time:

这份文件第一次真的帮到了我:

Part 1: http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1

第 1 部分:http: //www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1

Part 2: http://www.raywenderlich.com/5191/beginning-storyboards-in-ios-5-part-2

第 2 部分:http: //www.raywenderlich.com/5191/beginning-storyboards-in-ios-5-part-2

If you only want to check out the segues part, I'd recommend you to read part 2 only.

如果您只想查看 segues 部分,我建议您只阅读第 2 部分。