简体   繁体   English

如何将数据从一个视图控制器传递到导航控制器中嵌入的另一个视图控制器

[英]How to pass data from one view controller to another view controller which is embedded in navigation controller

I was able to pass data from one view controller to another, but when I embedded viewpapersViewController in navigation controller, data is not passing, I want to pass data to second view controller when button is pressed 我能够将数据从一个视图控制器传递给另一个视图控制器,但是当我将viewpapersViewController嵌入导航控制器时,数据没有传递,我想在按下按钮时将数据传递给第二个视图控制器

originally when button is pressed a request is sent, then in connectionDidFinishLoading 最初在按下按钮时发送请求,然后在connectionDidFinishLoading中

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

  // NSLog(@"Finish Loading");
NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:self->_responseData options:NSJSONReadingMutableLeaves error:&myError];


Papers *papersViewController =
[self.storyboard instantiateViewControllerWithIdentifier:@"Papers"];
papers = [[NSMutableArray alloc]init];
urls=[[NSMutableArray alloc]init];
for(id key in res) {

    NSString* name = [key objectForKey:@"name"];
    NSString* url =[key objectForKey:@"url"];

    [papers addObject:name];
    [urls addObject:url];

}

viewpapersViewController.exam = exam;
viewpapersViewController.papersName=papers;
viewpapersViewController.urls=urls;

[self presentViewController:viewpapersViewController animated:YES completion:Nil];

}

Suppose you want to pass a string,You can simply pass your data by these steps:- 假设您要传递字符串,则可以通过以下步骤简单地传递数据:

1- Go on second view controller and take a -- NSString *str; 1-继续第二个视图控制器,并采取-NSString * str;

2- And when you are pressing a button from your first view controller directly pass the data 2-当您按下第一个视图控制器中的按钮时,直接传递数据
like : - secondClassObject.str = FirstClassString. 像:-secondClassObject.str = FirstClassString。 3- And then Push your Navigation simply. 3-然后简单地推动您的导航。

Normally, we will pass data two ways: 通常,我们将通过两种方式传递数据:

  1. We just instantiate the target UIViewController and then pass the data to its properties. 我们只是实例化目标UIViewController ,然后将数据传递给它的属性。
  2. Another way we use is segue. 我们使用的另一种方法是segue。 Essentially, it's the same with the first way. 本质上,第一种方法是相同的。

For your problem, what I could think of is you are not getting your UIViewController right. 对于您的问题,我能想到的是您没有正确使用UIViewController You can probably try to get the UINavigationController first. 您可能可以尝试首先获取UINavigationController Then, use this property to access the top UIViewController: topViewController 然后,使用此属性访问顶部的UIViewController: topViewController

If you have ViewController connected with same Navigation Controller . 如果您已将ViewController与相同的Navigation Controller连接。 You must have performed PUSH Segue from 1st View to 2nd View by Drag from 1st to 2nd View. 您必须已通过从1st视图拖到2nd视图执行了从1st视图到2nd视图的PUSH Segue。 Click Push option in that . 在其中单击“推送”选项。 You have set up the Push Segue by this . 您已经以此建立了Push Segue。

Give a name to identifier to segue by clicking on that link between the 2 Views . 单击2个视图之间的链接给标识符命名以进行筛选。

Now you can perform simple Segue by 现在,您可以通过以下方式执行简单的Segue

[self performSegueWithIdentifier:@"identifier for segue" sender:self];

Now in 1st View 现在处于第一视图

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([[segue identifier] isEqualToString:@"identifier segue"]){
    // Get the new view controller using [segue destinationViewController].
    2nd View *vc = [segue destinationViewController];
    2nd vc.variableName  = YES;
}}

In 2nd View in interface file add property 在界面文件的第二视图中添加属性

@property variable *variableName;

暂无
暂无

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

相关问题 如何将数据传递给嵌入在导航控制器中的集合视图控制器 - How to pass data to collection view controller embedded in navigation controller 将数据传递到导航控制器中的另一个视图控制器 - Pass data to another view controller in navigation controller 如何将数据从视图控制器传递到导航控制器,然后传递到另一个视图控制器? - How to pass data from view controller to a navigation controller and then to another view controller? 如何在IOS中将数据从一个视图传递到另一个视图控制器? - How to pass data from one view to another view controller in IOS? 如何从嵌入在导航控制器中的简单视图控制器调用标签栏视图控制器? - How to call tab bar view controller from simple view controller which is embedded in navigation controller? 如何在导航控制器中将数据从一个视图传递到另一个视图 - How to pass data from one view to the other in a navigation controller 如何使用委托将数据从一个视图控制器传递到另一个视图控制器? - how to use delegate to pass data to from one View Controller to another? 无法将值从一个View Controller传递到另一个View Controller - Not able to pass value from one View Controller to another View Controller 如何将数据对象传递给嵌入在不同故事板中的导航控制器中的视图控制器 - How to Pass Data Object to View Controller Embedded in Navigation Controller in Different Storyboard 在不同的导航中从一个视图 Controller 弹出到另一个视图 controller - Pop from one View Controller to another in a different navigation controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM