简体   繁体   English

以一种编程方式将数据从一个viewcontroller传递到现有的viewcontroller

[英]Pass data from one viewcontroller to existing viewcontroller programmatically

Recently I am stuck in a problem so that I cannot move on to the next coding part. 最近我陷入了一个问题,所以我无法继续下一个编码部分。 There are two view controllers(let's say the first.m and second.m) and I would like to pass parameter data from one to another view controller without using storyboard. 有两个视图控制器(比如first.m和second.m),我想在不使用故事板的情况下将参数数据从一个视图控制器传递到另一个视图控制器。 But the tricky part for me is the second view controller was already initiated in the AppDelegate.m file. 但对我来说棘手的部分是第二个视图控制器已经在AppDelegate.m文件中启动。

I can pass data if I have initiated the second view controller in the first view controller with some importing header file, for example.. 如果我在第一个视图控制器中启动了第二个视图控制器并带有一些导入头文件,我可以传递数据,例如..

@import "Second.h"

Second *secondView = [[Second alloc] initWithNibName:@"SecondNib" bundle:nil];
secondView.someStringValue = @"passThisString";

However, this coding makes a new second view controller, which is not what I want. 但是,这种编码会产生一个新的第二个视图控制器,这不是我想要的。 I found there is a something like 我发现有类似的东西

myAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate];

to connect to the AppDelegate.m file. 连接到AppDelegate.m文件。 But after a few days struggling with this my brain is getting blackout. 但经过几天的挣扎,我的大脑正在停电。 Please give me any advice.. Is there a way to pass data to existing view controller without using storyboard? 请给我任何建议。有没有办法将数据传递给现有的视图控制器而不使用故事板? or any way through AppDelegate to Second view controller. 或通过AppDelegate到第二个视图控制器的任何方式。 Any help will be appreciated 任何帮助将不胜感激

@jazzed28 you can do this without initiating your Second . @ jazzed28你可以在不启动你的Second情况下做到这一点。 You just need to access the already initiated Second . 您只需要访问已经启动的Second So declare Second in myAppDelegate.h as a property of it like this - 所以在myAppDelegate.h声明Second作为它的property ,就像这样 -

@property (nonatomic, strong) Second *svc;

then whenever you need second you can access like this - 那么无论什么时候你需要秒,你都可以这样访问 -

myAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate];

appDelegate.svc

crete two class methods in myAppDelegate for setting data and getting data from Appdelegate,you can access those methods by using APpdelegate class name. myteDelegate中用于设置数据和从Appdelegate获取数据的两个类方法,您可以使用APpdelegate类名访问这些方法。

 Setter method
 +(void)setGlobalString:(NSString *)stringgValue{
          [[NSUserDefaults standardUserDefaults] setObject:stringgValue forKey:@"globalValue"];
     // you can save string value here by using NSUserDefaults or any thing else

    }
Getter method
+(NSString *)getGlobalString{
        return [[NSUserDefaults standardUserDefaults] forKey:@"globalValue"];
    }

you can call methods like 你可以调用像这样的方法

[myAppDelegate setGlobalString:yourString];

NSString *str = [myAppDelegate getGlobalString];

This question has been asked many times before, including on StackOverflow. 之前已经多次询问过这个问题,包括StackOverflow。 If you had spent some more time looking for an answer, you might have found this answer for example. 如果你花了一些时间寻找答案,你可能已经找到了这个答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将数据从一个视图控制器传递到同一视图上的另一个视图控制器 - Pass data to from one viewcontroller to another viewcontroller on the same view 将数据从一个标签栏视图控制器传递到另一标签栏视图控制器 - pass data from one tabbar viewcontroller to another tabbar viewcontroller 将字符串变量数据从一个 Class/ViewController 传递给 Class/ViewController - Pass String Variable Data from one Class/ViewController to Class/ViewController Swift 3将数据从一个ViewController传递到另一个ViewController - Swift 3 pass data from one ViewController to another ViewController 将数据从ViewController传递回根ViewController? - Pass data from ViewController back to root ViewController? 将数据从最后一个 ViewController 传递到根 ViewController - Pass Data from last ViewController to root ViewController Swift:以编程方式导航到ViewController并传递数据 - Swift: Programmatically Navigate to ViewController and Pass Data 将数据从ViewController传递给类 - Pass data from ViewController to class 将数据从 AppDelegate 传递到 ViewController - Pass data from AppDelegate to ViewController 将数据从ViewController传递到TabBarController - Pass Data From ViewController to TabBarController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM