简体   繁体   English

Segue公司不会将数据

[英]Segue Won't Pass on Data

So I set up a segue from one UIViewController to another (MenuViewController to AgendaViewController), but the variable on the target UIViewController still shows up as null, even though I updated it during the segue. 因此,我设置了从一个UIViewController到另一个UIViewController(从MenuViewController到AgendaViewController)的segue,但是目标UIViewController上的变量仍然显示为null,即使我在segue期间进行了更新。

Am I doing something wrong in the segue method? 我在segue方法中做错了吗?

This is the code for MenuViewController, the view I am trying to segue FROM: 这是MenuViewController的代码,我正在尝试从中寻找视图:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"toAgenda"]){
    AgendaViewController *controller = (AgendaViewController *)segue.destinationViewController;

    // Load agenda url based on event's name
    PFQuery *event_query = [PFQuery queryWithClassName:@"Event"];
    [event_query whereKey:@"event_name" equalTo:event_name];
    [event_query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            for (PFObject *object in objects) {
                controller.agenda_url = (NSString*)[object objectForKey:@"event_agenda_url"];
                NSLog(@"%@", [object objectForKey:@"event_agenda_url"]);
            }
        } else {
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];
    }
}

This is the code I have for the target view controller, AgendaViewController: 这是我为目标视图控制器AgendaViewController编写的代码:

#import "AgendaViewController.h"
#import "Parse/Parse.h"

@interface AgendaViewController ()

@end

@implementation AgendaViewController

@synthesize agenda_url;
@synthesize webView;

- (void)viewDidLoad
{
[super viewDidLoad];

// Load web view with agenda url, which was just retrieved
NSURL *url =[NSURL URLWithString:agenda_url];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
[webView loadRequest:request];

NSLog(@"Data recieved. Agenda_url = %@", agenda_url);
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) { }
return self;
}

@end

Thanks! 谢谢!

For me it looks like it is null because you are assigning agenda_url value on the second thread. 对我来说,它似乎为空,因为您要在第二个线程上分配schedule_url值。 So basically AgendaViewController viewdidload method is triggered before completion block from findObjectsInBackgroundWithBlock: is executed. 所以基本上AgendaViewController viewdidload方法是从完工块之前触发findObjectsInBackgroundWithBlock:被执行。 To fix that you should either assign agenda_url on the main thread or call loadRequest: in completion block 为了解决这个问题,你应该通过主线程分配agenda_url或拨打loadRequest:在完成块

 [event_query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        for (PFObject *object in objects) {
            controller.agenda_url = (NSString*)[object objectForKey:@"event_agenda_url"];
            NSLog(@"%@", [object objectForKey:@"event_agenda_url"]);
        }
        NSURL *url =[NSURL URLWithString:controller.agenda_url];
        NSURLRequest *request=[NSURLRequest requestWithURL:url];
        [controller.webView performSelectorOnMainThread:@selector(loadRequest:) withObject:request waitUntilDone:NO];
    } else {
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }
}];

Just edited code above to perform loadRequest on main thread. 刚刚编辑了上面的代码以在主线程上执行loadRequest As suggested in comment below it is not safe to call UIKit element on background thread. 如以下注释中所建议,在后台线程上调用UIKit元素并不安全。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM