Curtis Boylan
Curtis Boylan

Reputation: 827

iOS presentViewController Delegates Returning Black Screen

I have created two views within my project. I want to be able to click a button on my main view and the other view (ChooseCar) will pop up allowing the user to pick something then it will reopen the old view (ViewController) with the information entered. I have done the code for it but for some reason when I click the button the screen just goes black with nothing happening, i'm pretty sure it's something very simple I just can't get my head around it.

I will attach the code for the views below, thanks.

ViewController.h

//
//  ViewController.h
//
//  Created by Curtis Boylan on 24/11/2016.
//  Copyright © 2016. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "ChooseCar.h"

@interface ViewController : UIViewController <ChooseCarDelegate>
- (IBAction)chooselocation;
@property (strong, nonatomic) IBOutlet UILabel *wherelocation;

@end

ViewController.m

//
//  ViewController.m
//
//  Created by Curtis Boylan on 24/11/2016.
//  Copyright © 2016. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad]; 
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
- (IBAction)chooselocation {
    ChooseCar *acController = [[ChooseCar alloc] init];
   // acController.delegate = self;
    [self presentViewController:acController animated:YES completion:nil];}

- (void)addItemViewController:(ChooseCar *)controller didFinishEnteringItem:(NSString *)item
{
    NSLog(@"This was returned from ChooseCar %@",item);
}

@end

ChooseCar.h

//
//  ChooseCar.h
//
//  Created by Curtis Boylan on 24/11/2016.
//  Copyright © 2016. All rights reserved.
//

#import <UIKit/UIKit.h>
@class ChooseCar;

@protocol ChooseCarDelegate <NSObject>
- (void)addItemViewController:(ChooseCar *)controller didFinishEnteringItem:(NSString *)item;
@end

@interface ChooseCar : UIViewController

@end

ChooseCar.m

//
//  ChooseCar.m
//
//  Created by Curtis Boylan on 24/11/2016.
//  Copyright © 2016. All rights reserved.
//

#import "ChooseCar.h"

@interface ChooseCar ()
@property (nonatomic, weak) id <ChooseCarDelegate> delegate;
@end

@implementation ChooseCar

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    NSString *itemToPassBack = @"Pass this value back to ViewControllerA";
    [self.delegate addItemViewController:self didFinishEnteringItem:itemToPassBack];

}

- (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 {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

Upvotes: 1

Views: 614

Answers (2)

aircraft
aircraft

Reputation: 26886

Its easy to do:

1)I believe your ChooseCar vc is created in storyboard. if yes, you should set the Storyboard ID like this:

set storyboard id

2)In your ViewController.m, update your chooselocation method to this:

- (IBAction)chooselocation {
//ChooseCar *acController = [[ChooseCar alloc] init];

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];


ChooseCar *acController = [sb instantiateViewControllerWithIdentifier:@"ChooseCar"];

[self presentViewController:acController animated:YES completion:nil];

}

EDIT

If you want to pass a value by delegate:

at the base of my give.

1)cut this code @property (nonatomic, weak) id <ChooseCarDelegate> delegate; from your ChooseCar.m to ChooseCar.h , make sure the ChooseCar.h like this:

#import <UIKit/UIKit.h>

@class ChooseCar;

@protocol ChooseCarDelegate <NSObject>


- (void)addItemViewController:(ChooseCar *)controller didFinishEnteringItem:(NSString *)item;

@end


@interface ChooseCar : UIViewController

@property (nonatomic, weak) id <ChooseCarDelegate> delegate;

@end

2) In the ViewController.m, you should abide the protocal, and set caController's delegate.

#import "ChooseCar.h"
#import "ViewController.h"

@interface ViewController () <ChooseCarDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
- (IBAction)chooselocation {
    //ChooseCar *acController = [[ChooseCar alloc] init];

    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];


    ChooseCar *acController = [sb instantiateViewControllerWithIdentifier:@"ChooseCar"];

    acController.delegate = self;

    [self presentViewController:acController animated:YES completion:nil];

}

- (void)addItemViewController:(ChooseCar *)controller didFinishEnteringItem:(NSString *)item
{
    NSLog(@"This was returned from ChooseCar %@",item);
}

@end

3)If you want to pass the value, you should take this code to your action:

NSString *itemToPassBack = @"Pass this value back to ViewControllerA";
[self.delegate addItemViewController:self didFinishEnteringItem:itemToPassBack];

Upvotes: 2

User511
User511

Reputation: 1486

Replace your code with the following:

ViewController.h

#import <UIKit/UIKit.h>
#import "ChooseCar.h"

@interface ViewController : UIViewController <ChooseCarDelegate>
- (IBAction)chooselocation;
@property (strong, nonatomic) IBOutlet UILabel *wherelocation;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()
{
    ChooseCar *acController;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
- (IBAction)chooselocation {

    acController = [self.storyboard instantiateViewControllerWithIdentifier:@"ChooseCar"];
    acController.delegate = self;
    [self presentViewController:acController animated:YES completion:nil];

}

- (void)addItemViewController:(ChooseCar *)controller didFinishEnteringItem:(NSString *)item
{
    [acController dismissViewControllerAnimated:true completion:nil];
    NSLog(@"This was returned from ChooseCar %@",item);
}

@end

Also set the storyboard id in storyboard. For more details see the attached screenshot:

enter image description here

ChooseCar.h

#import <UIKit/UIKit.h>
@class ChooseCar;

@protocol ChooseCarDelegate <NSObject>
- (void)addItemViewController:(ChooseCar *)controller didFinishEnteringItem:(NSString *)item;
@end

@interface ChooseCar : UIViewController
@property (nonatomic, weak) id <ChooseCarDelegate> delegate;
@end

ChooseCar.m

#import "ChooseCar.h"

@interface ChooseCar ()
@end

@implementation ChooseCar

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

//    NSString *itemToPassBack = @"Pass this value back to ViewControllerA";
//    [self.delegate addItemViewController:self didFinishEnteringItem:itemToPassBack];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)backButtonClicked:(id)sender {
    NSString *itemToPassBack = @"Pass this value back to ViewControllerA";
   [self.delegate addItemViewController:self didFinishEnteringItem:itemToPassBack];
}

/*
 #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 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

Upvotes: 1

Related Questions