在Objective-C中将一个ViewController的值传递给另一个ViewController
我对Objective-C编程还很陌生。 有一件事,我现在正在把第一个ViewController
的值传递给下一个。
我已阅读此条目 ,并没有帮助我。 这很有趣,因为他的答案有近500票,所以我一定是这个问题。 我做的是以下。
我打开了我的PreviewViewController.m
并添加了以下行#import "MainViewController.h"
因为我想将PreviewViewController
的值传递给MainViewController。 然后,当我切换布局(成功的作品),我想传递一个值。
MainViewController *mainViewController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil]; mainViewController.userId = @"539897197";
正如你所看到的,我想通过userId。 为此,我还在MainViewController.h
创build了一个属性
@property ( nonatomic, strong ) NSString *userId;
现在,在我的MainViewController.m
我想访问userId。 但是当我login时,控制台告诉我它是null
。 但是,当我在NSLog
之前设置variables的时候,所以看起来像传递是问题。
另外在MainViewController.m
我有以下行
@synthesize userId = _userId;
但即使当我删除该行,并将NSLog
更改为NSLog(@"%@",self.userId);
发生同样的问题。
我怎样才能顺利通过variables? 我做错了哪一步?
编辑这是我如何切换布局
UIViewController *viewController = [[MainViewController alloc]init]; MainViewController *mainViewController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil]; mainViewController.userId = @"539897197"; [self presentViewController:viewController animated:YES completion:NULL];
为什么不创build一个自定义初始化器并以这种方式传递它? 就像是
- (id)initWithUserId:(NSString *)aUserId { self = [super initWithNibName:@"MainViewController" bundle:nil]; if (self) { self.userId = aUserId } return self; }
那么你可以做:
MainViewController *mvc = [[MainViewController alloc] initWithUserId:@"1234"] [self presentViewController:mvc animated:YES completion:nil];
如果您正在使用storyboard
那么您应该使用prepareForSegue:
方法在视图控制器之间传递数据。 首先创build从您的PreviewViewController
到MainViewController
的segue,只需控制从您的视图控制器拖到下一个viewcontroller创buildsegue。 如果您使用的是push segue,请使用UINavigationController
。 使用此方法来继续并传递数据
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ if([segue.identifier isEqualToString:@"yourSegueIndentifier"]){ MainViewController *mvc = (MainViewController *) segue.destinationViewController; mvc.userId = @"539897197";; } }
UIViewController *viewController = [[MainViewController alloc]init]; MainViewController *mainViewController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil]; mainViewController.userId = @"539897197"; [self presentViewController:mainViewController animated:YES completion:NULL];
在呈现视图控制器时使用mainViewController而不是viewController
这里我们有两种传递数据的方法。
首先是自定义代表
其次是NSNotification
这里我们有两个视图控制器。
ViewController和SecondViewController
在SecondViewController中
。H
#import <UIKit/UIKit.h> @class SecondViewController; @protocol SecondViewControllerDelegate <NSObject> - (void)secondViewController:(SecondViewController *)secondViewController didEnterText:(NSString *)text; @end @interface SecondViewController : UIViewController @property (nonatomic, assign)id<SecondViewControllerDelegate> delegate; @property (nonatomic, strong) IBOutlet UITextField *nameTextField;//It must connect as outlet connection - (IBAction)doneButtonTapped:(id)sender; @end
.M
#import "SecondViewController.h" @interface SecondViewController () @end @implementation SecondViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } //Either use NSNotification or Delegate - (IBAction)doneButtonTapped:(id)sender; { //Use Notification [[NSNotificationCenter defaultCenter] postNotificationName:@"passingDataFromSecondViewToFirstView" object:self.nameTextField.text]; //OR Custom Delegate [self.delegate secondViewController:self didEnterText:self.nameTextField.text]; [self.navigationController popViewControllerAnimated:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
在ViewController中
。H
#import <UIKit/UIKit.h> #import "SecondViewController.h" @interface ViewController : UIViewController<SecondViewControllerDelegate> @property (nonatomic, strong) IBOutlet UILabel *labelName; //You must connect the label with outlet connection - (IBAction)gotoNextView:(id)sender; @end
.M
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //addObserver here... [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFromPreviousViewControllerNotificationReceived:) name:@"passingDataFromSecondViewToFirstView" object:nil]; // Do any additional setup after loading the view, typically from a nib. } //addObserver Method here.... - (void)textFromPreviousViewControllerNotificationReceived:(NSNotification *)notification { // set text to label... NSString *string = [notification object]; self.labelName.text = string; } - (IBAction)gotoNextView:(id)sender; { //If you use storyboard SecondViewController *secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"]; //OR If you use XIB SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; secondViewController.delegate = self; [self.navigationController pushViewController:secondViewController animated:YES]; } //Calling custom delegate method - (void)secondViewController:(SecondViewController *)secondViewController didEnterText:(NSString *)text { self.labelName.text = text; //Getting the data and assign the data to label here. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
为了理解代码,我创build了一个简单的从第二个视图控制器到第一个视图控制器的传递数据。
首先我们浏览从第一个视图控制器到第二个视图控制器的视图。
之后,我们将数据从第二个视图控制器发送到第一个视图控制器。
注意:您可以使用NSNotification或自定义委托方法将数据从One View Controller发送到其他视图控制器
如果使用NSNotification,则需要设置postNotificationName以获取button操作方法中的数据。
接下来,您需要编写addObserver (将数据发送到您需要的View Controller)ViewController,并在同一个View Controller中调用addObserver方法。
如果您使用自定义委托,通常我们会使用自定义协议委托 ,也需要在此处指派委托。
非常重要的是,我们必须在第二个视图控制器中设置自定义委托方法 。因为我们在第二个视图控制器中单击完成button后,将数据发送到第一个视图控制器。
最后,我们必须在第一个视图控制器中调用自定义委托方法 ,在那里我们获取数据并将数据分配给标签。现在,您可以使用自定义委托来查看传递的数据。
同样,您可以使用自定义委托方法将数据发送到其他视图控制器
我试图得到解决scheme之间的viewcontroller传递的价值。
MainViewController *mainVC = [[self storyboard] instantiateViewControllerWithIdentifier:@"MainViewController"]; mainVC.userId = @"539897197"; [self presentViewController:mainVC animated:YES completion:Nil];
或者使用这种方式。 你不使用故事板使用下面的工作正常
MainViewController *mainVC = [[MainViewController alloc] init]; mainVC.userId = @"539897197"; [self presentViewController:mainVC animated:YES completion:Nil];