[英]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: 通常,我们将通过两种方式传递数据:
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.