不能使用PrepareForSegue背靠背

时间:2015-06-23 03:16:23

标签: ios objective-c xcode uiviewcontroller segue

我一直试图弄清楚为什么这不起作用但没有运气:

我正在Xcode中创建一个IOS应用程序并将其设置为:我首先从 ViewControllerA (LoginViewController)转到 ViewControllerB (HomeViewController)。在此segue期间,我传递一个UIImage和一个UILabel,以便在ViewControllerB中显示。一旦发生这种情况,我想设置另一个prepareForSegue转到 ViewControllerC (ProfileViewController)并再次传递相同的数据。但是,每次我尝试这样做时,应用程序都会出现故障,崩溃或死机。我不太清楚为什么会这样。

我希望每次我想传递数据时都有一种方法我不必做一个prepareForSegue - 比如Global Variable或Singleton。 但是我试图避免使用这两个选项,因为我已经多次读过这些选项是危险的,不应该真正使用。 如果这些方式中的任何一种都适用于此,那么请务必告诉我,这将是它的结束。

同时,我想知道是否有人可以帮助我理解为什么会出现这种错误。这是我的ViewControllerA.m代码:

LoginViewController.m

#import "LoginViewController.h"
#import <QuartzCore/QuartzCore.h>


@interface LoginViewController ()

- (void)toggleHiddenState:(BOOL)shouldHide;

@end

@implementation LoginViewController

@synthesize profPicture;


- (void)viewDidLoad {
    [super viewDidLoad];

    [self toggleHiddenState:YES];
    self.lblLoginStatus.text = @"";

    self.loginButton.readPermissions = @[@"public_profile", @"email"];
    self.loginButton.delegate = self;

    // Do any additional setup after loading the view.
}

-(void)toggleHiddenState:(BOOL)shouldHide{
    self.lblUsername.hidden = shouldHide;
    self.lblEmail.hidden = shouldHide;
    self.profilePicture.hidden = shouldHide;
    self.loggedinwallpaper.hidden = shouldHide;
    self.FBlogout.hidden = shouldHide;
}


-(void)loginViewShowingLoggedInUser:(FBLoginView *)loginView{
    self.lblLoginStatus.text = @"";

    [self toggleHiddenState:NO];
}

-(void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView{
    self.lblLoginStatus.text = @"";

    [self toggleHiddenState:YES];
}

-(void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user{

    NSLog(@"%@", user);
    //dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void){


        self.profilePicture.profileID = user.objectID;
        self.lblUsername.text = user.name;
        self.lblEmail.text = [user objectForKey:@"email"];

        // Create Facebook Profile Picture from User ID url
        NSString *pic_link = [NSString stringWithFormat:@"http://graph.facebook.com/%@/picture?width=300&height=300", user.objectID];
        NSURL *pic_url = [NSURL URLWithString:pic_link];
        profPicture = [UIImage imageWithData: [NSData dataWithContentsOfURL:pic_url]];

        //dispatch_async(dispatch_get_main_queue(), ^(void){
            //Main Thread : UI Updates
//            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//            UIViewController *homeViewController = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:@"HomeViewController"];
//            [self performSelector:@selector(prepareForSegue:sender:) withObject:nil afterDelay:1.0 ];
//            [self presentViewController:homeViewController animated:YES completion:nil];

            //[_invisible                         sendActionsForControlEvents:UIControlEventTouchUpInside];
            [self performSegueWithIdentifier:@"showMap" sender:self];

       //});
    //});
}


-(void)loginView:(FBLoginView *)loginView handleError:(NSError *)error{
    NSLog(@"%@", [error localizedDescription]);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    HomeViewController *homeviewController = segue.destinationViewController;
    homeviewController.homepic = [[UIImage alloc] init];
    homeviewController.homepic = profPicture;
    homeviewController.username = self.lblUsername;
}


@end

现在这是我的ViewControllerB代码:

HomeViewController.m

#import "HomeViewController.h"
#import "LoginViewController.h"
#import "ProfileViewController.h"

@interface HomeViewController ()

@end

@implementation HomeViewController

@synthesize homeviewpic, homepic, sendpic, homeViewUsername, username;

- (void)viewDidLoad {
    [super viewDidLoad];


    //----------Profile Picture---------------------------------------
    // Determine the context of where to set image.
    UIGraphicsBeginImageContext(self.homeviewpic.frame.size);
    [homepic drawInRect:self.homeviewpic.bounds];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    // Create subview to be added
    UIImageView *backgroundView = [[UIImageView alloc] initWithImage:image];

    // Add subview
    [self.homeviewpic addSubview:backgroundView];
    [self.homeviewpic setContentMode:UIViewContentModeScaleAspectFill];

    sendpic = homepic;



    //----------Username-----------------------------------------------
    //homeViewUsername.text = username.text;



        // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear: animated];
    self.homeViewUsername.text = self.username.text;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//-----------------------------Screenshot of background void method--------------------------------
- (UIImage *) screenshot {
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);

    [self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;

}
//-------------------------------------------------------------------------------------------------


#pragma mark - Navigation

 //In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    ProfileViewController *PVC = segue.destinationViewController;
    //PVC.profPic = sendpic;
    //PVC.profileViewUsername = username;
}


@end

就目前而言,我已经在prepareForSegue中注释了这些行 - 这些行是问题的根源。如果有人可以帮助解释并尝试提供解决方案,我将非常感激。谢谢!

1 个答案:

答案 0 :(得分:0)

您尝试在dispatch_async中显示两个viewcontrollers。 presentViewController和performSegueWithIdentifier做同样的事情。您必须放置一些条件,以便在dispatch_async方法中调用其中一个条件。试图呈现两个viewcontrollers将崩溃。