ios 如何在不使用 segue 的情况下在视图控制器之间传递数据?

How to pas data between view controller without using segue in ios?

大家好,

我正在使用以下代码从一个视图控制器移动到另一个视图控制器。

UIStoryboard  *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"RoloBusinessCard"];
[self.navigationController pushViewController:vc animated:YES];

现在我想将一些数据从一个视图控制器传递到另一个视图控制器,例如某些 variable.I 已经有了这段代码来完成这个任务,但我没有创建另一个视图的实例 controller.I 正在开始另一个视图使用故事板查看控制器。

SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
secondViewController.str1 = str;
[self.navigationController pushViewController:secondViewController animated:YES];

请告诉我如何以我的方式在视图控制器之间传递数据?

解决方案无效.h 文件中贴花 NSString

@property (nonatomic, strong) NSString *UserId;

在.m文件中合成

@synthesize UserId;

导航代码

 UIStoryboard  *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"RoloBusinessCard"];
  vc.UserId=@"abc";
 [self.navigationController pushViewController:vc animated:YES];

但是vc.UserId无法识别。

像这样尝试,希望它会起作用。

UIStoryboard  *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
SecondViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"RoloBusinessCard"];
vc.str1 = str;
[self.navigationController pushViewController:vc animated:YES];

您可以借助属性在两个 ViewControler 之间传递数据。 首先在 RoloBusinessCard.h ViewControler 中创建一个 属性 这样的

@property (nonatomic, strong) NSString *str1;

在这个class的.m文件中这样合成

@synthesize str2;

现在你可以像这样传递值

UIStoryboard  *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"RoloBusinessCard"];
vc.str2=str2
[self.navigationController pushViewController:vc animated:YES];

如果您无法在 RoloBusinessCard 中获取 str2

首先用你的 Viewcontroller 名称替换 UIViewController

RoloBusinessCard *vc = [storyboard instantiateViewControllerWithIdentifier:@"RoloBusinessCard"];

像这样输入 UIViewController

UIStoryboard  *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"RoloBusinessCard"];

(RoloBusinessCard *)vc1=vc
    vc1.str2=str2
    [self.navigationController pushViewController:vc1 animated:YES];

希望对您有所帮助。

这里我们只是使用导航堆栈将数据传递给下一个控制器

在第二视图控制器中创建一个 属性

@property (nonatomic, strong) NSString *yourString;

在 firstView 控制器操作方法中 您将在 push 方法上创建的视图控制器对象 do

-(void)btnAtion{
object.yourstring = txtyourfirstvcObject.text
}

在swift中,我们也可以这样做

假设我们想将布尔值传递给下一个控制器,这样我们就可以这样做:

Class ToViewController : UIViewController {
      public var isSelected : Bool? 
}

Class FromViewController : UIViewController {

    @IBAction func onClickFlyingFrom(_ sender: UIButton) {
         let tvc = self.storyboard?.instantiateViewController(withIdentifier: "ToViewController") as! ToViewController
         tvc.isSelected = true
         self.navigationController?.present(vc, animated: true, completion: nil)
    }
}